commandserver: enable logging when server process started
authorYuya Nishihara <yuya@tcha.org>
Sat, 10 Nov 2018 18:19:34 +0900
changeset 53588 368ecbf734af109b936f309e7feec788d5597290
parent 53587 6a75363f834a299b85ad88dd2e96ca61804fc232
child 53589 82210d88d814172b0317e5e5a1737b695b125a0e
push id1079
push usergszorc@mozilla.com
push dateMon, 10 Dec 2018 19:44:59 +0000
commandserver: enable logging when server process started This allows us to keep track of server events before client connects to the server. Tests will be added later. Currently there's no log() call to check if things are working well.
mercurial/commandserver.py
mercurial/server.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -203,25 +203,20 @@ def _selectmessageencoder(ui):
 class server(object):
     """
     Listens for commands on fin, runs them and writes the output on a channel
     based stream to fout.
     """
     def __init__(self, ui, repo, fin, fout):
         self.cwd = encoding.getcwd()
 
-        # developer config: cmdserver.log
-        logpath = ui.config("cmdserver", "log")
-        if logpath:
+        if ui.config("cmdserver", "log") == '-':
             global logfile
-            if logpath == '-':
-                # write log on a special 'd' (debug) channel
-                logfile = channeledoutput(fout, 'd')
-            else:
-                logfile = open(logpath, 'a')
+            # switch log stream to the 'd' (debug) channel
+            logfile = channeledoutput(fout, 'd')
 
         if repo:
             # the ui here is really the repo ui so take its baseui so we don't
             # end up with its local configuration
             self.ui = repo.baseui
             self.repo = repo
             self.repoui = repo.ui
         else:
@@ -356,16 +351,34 @@ class server(object):
                 pass
         except EOFError:
             # we'll get here if the client disconnected while we were reading
             # its request
             return 1
 
         return 0
 
+def setuplogging(ui):
+    """Set up server logging facility
+
+    If cmdserver.log is '-', log messages will be sent to the 'd' channel
+    while a client is connected. Otherwise, messages will be written to
+    the stderr of the server process.
+    """
+    # developer config: cmdserver.log
+    logpath = ui.config(b'cmdserver', b'log')
+    if not logpath:
+        return
+
+    global logfile
+    if logpath == b'-':
+        logfile = ui.ferr
+    else:
+        logfile = open(logpath, 'ab')
+
 class pipeservice(object):
     def __init__(self, ui, repo, opts):
         self.ui = ui
         self.repo = repo
 
     def init(self):
         pass
 
--- a/mercurial/server.py
+++ b/mercurial/server.py
@@ -150,19 +150,21 @@ def runservice(opts, parentfn=None, init
     'chgunix': chgserver.chgunixservice,
     'pipe': commandserver.pipeservice,
     'unix': commandserver.unixforkingservice,
 }
 
 def _createcmdservice(ui, repo, opts):
     mode = opts['cmdserver']
     try:
-        return _cmdservicemap[mode](ui, repo, opts)
+        servicefn = _cmdservicemap[mode]
     except KeyError:
         raise error.Abort(_('unknown mode %s') % mode)
+    commandserver.setuplogging(ui)
+    return servicefn(ui, repo, opts)
 
 def _createhgwebservice(ui, repo, opts):
     # this way we can check if something was given in the command-line
     if opts.get('port'):
         opts['port'] = util.getport(opts.get('port'))
 
     alluis = {ui}
     if repo: