Patchwork [3,of,8] commandserver: enable logging when server process started

login
register
mail settings
Submitter Yuya Nishihara
Date Dec. 4, 2018, 1:24 p.m.
Message ID <a784cb45d89bdf7f995a.1543929857@mimosa>
Download mbox | patch
Permalink /patch/36950/
State Accepted
Headers show

Comments

Yuya Nishihara - Dec. 4, 2018, 1:24 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1541841574 -32400
#      Sat Nov 10 18:19:34 2018 +0900
# Node ID a784cb45d89bdf7f995a918cd75c3bb3f9e56ded
# Parent  168b9226865275da15d00ac3aee11463381f427e
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.

Patch

diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -208,15 +208,10 @@  class server(object):
     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
@@ -361,6 +356,24 @@  class server(object):
 
         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
diff --git a/mercurial/server.py b/mercurial/server.py
--- a/mercurial/server.py
+++ b/mercurial/server.py
@@ -155,9 +155,11 @@  def runservice(opts, parentfn=None, init
 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