mercurial/commandserver.py
changeset 40824 82210d88d814
parent 40823 368ecbf734af
child 40825 eaabcb689747
--- a/mercurial/commandserver.py	Sat Nov 10 18:19:34 2018 +0900
+++ b/mercurial/commandserver.py	Sat Nov 10 19:00:17 2018 +0900
@@ -26,8 +26,10 @@
 from . import (
     encoding,
     error,
+    loggingutil,
     pycompat,
     util,
+    vfs as vfsmod,
 )
 from .utils import (
     cborutil,
@@ -223,11 +225,18 @@
             self.ui = ui
             self.repo = self.repoui = None
 
+        self.cdebug = logfile
         self.cerr = channeledoutput(fout, 'e')
         self.cout = channeledoutput(fout, 'o')
         self.cin = channeledinput(fin, fout, 'I')
         self.cresult = channeledoutput(fout, 'r')
 
+        if self.ui.config(b'cmdserver', b'log') == b'-':
+            # switch log stream of server's ui to the 'd' (debug) channel
+            # (don't touch repo.ui as its lifetime is longer than the server)
+            self.ui = self.ui.copy()
+            setuplogging(self.ui, repo=None, fp=self.cdebug)
+
         # TODO: add this to help/config.txt when stabilized
         # ``channel``
         #   Use separate channel for structured output. (Command-server only)
@@ -356,17 +365,18 @@
 
         return 0
 
-def setuplogging(ui):
+def setuplogging(ui, repo=None, fp=None):
     """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.
+    If cmdserver.log is '-', log messages will be sent to the given fp.
+    It should be the 'd' channel while a client is connected, and otherwise
+    is the stderr of the server process.
     """
     # developer config: cmdserver.log
     logpath = ui.config(b'cmdserver', b'log')
     if not logpath:
         return
+    tracked = {b'cmdserver'}
 
     global logfile
     if logpath == b'-':
@@ -374,6 +384,22 @@
     else:
         logfile = open(logpath, 'ab')
 
+    if logpath == b'-' and fp:
+        logger = loggingutil.fileobjectlogger(fp, tracked)
+    elif logpath == b'-':
+        logger = loggingutil.fileobjectlogger(ui.ferr, tracked)
+    else:
+        logpath = os.path.abspath(logpath)
+        vfs = vfsmod.vfs(os.path.dirname(logpath))
+        logger = loggingutil.filelogger(vfs, os.path.basename(logpath), tracked)
+
+    targetuis = {ui}
+    if repo:
+        targetuis.add(repo.baseui)
+        targetuis.add(repo.ui)
+    for u in targetuis:
+        u.setlogger(b'cmdserver', logger)
+
 class pipeservice(object):
     def __init__(self, ui, repo, opts):
         self.ui = ui