mercurial/commandserver.py
changeset 40824 82210d88d814
parent 40823 368ecbf734af
child 40825 eaabcb689747
equal deleted inserted replaced
40823:368ecbf734af 40824:82210d88d814
    24 
    24 
    25 from .i18n import _
    25 from .i18n import _
    26 from . import (
    26 from . import (
    27     encoding,
    27     encoding,
    28     error,
    28     error,
       
    29     loggingutil,
    29     pycompat,
    30     pycompat,
    30     util,
    31     util,
       
    32     vfs as vfsmod,
    31 )
    33 )
    32 from .utils import (
    34 from .utils import (
    33     cborutil,
    35     cborutil,
    34     procutil,
    36     procutil,
    35 )
    37 )
   221             self.repoui = repo.ui
   223             self.repoui = repo.ui
   222         else:
   224         else:
   223             self.ui = ui
   225             self.ui = ui
   224             self.repo = self.repoui = None
   226             self.repo = self.repoui = None
   225 
   227 
       
   228         self.cdebug = logfile
   226         self.cerr = channeledoutput(fout, 'e')
   229         self.cerr = channeledoutput(fout, 'e')
   227         self.cout = channeledoutput(fout, 'o')
   230         self.cout = channeledoutput(fout, 'o')
   228         self.cin = channeledinput(fin, fout, 'I')
   231         self.cin = channeledinput(fin, fout, 'I')
   229         self.cresult = channeledoutput(fout, 'r')
   232         self.cresult = channeledoutput(fout, 'r')
       
   233 
       
   234         if self.ui.config(b'cmdserver', b'log') == b'-':
       
   235             # switch log stream of server's ui to the 'd' (debug) channel
       
   236             # (don't touch repo.ui as its lifetime is longer than the server)
       
   237             self.ui = self.ui.copy()
       
   238             setuplogging(self.ui, repo=None, fp=self.cdebug)
   230 
   239 
   231         # TODO: add this to help/config.txt when stabilized
   240         # TODO: add this to help/config.txt when stabilized
   232         # ``channel``
   241         # ``channel``
   233         #   Use separate channel for structured output. (Command-server only)
   242         #   Use separate channel for structured output. (Command-server only)
   234         self.cmsg = None
   243         self.cmsg = None
   354             # its request
   363             # its request
   355             return 1
   364             return 1
   356 
   365 
   357         return 0
   366         return 0
   358 
   367 
   359 def setuplogging(ui):
   368 def setuplogging(ui, repo=None, fp=None):
   360     """Set up server logging facility
   369     """Set up server logging facility
   361 
   370 
   362     If cmdserver.log is '-', log messages will be sent to the 'd' channel
   371     If cmdserver.log is '-', log messages will be sent to the given fp.
   363     while a client is connected. Otherwise, messages will be written to
   372     It should be the 'd' channel while a client is connected, and otherwise
   364     the stderr of the server process.
   373     is the stderr of the server process.
   365     """
   374     """
   366     # developer config: cmdserver.log
   375     # developer config: cmdserver.log
   367     logpath = ui.config(b'cmdserver', b'log')
   376     logpath = ui.config(b'cmdserver', b'log')
   368     if not logpath:
   377     if not logpath:
   369         return
   378         return
       
   379     tracked = {b'cmdserver'}
   370 
   380 
   371     global logfile
   381     global logfile
   372     if logpath == b'-':
   382     if logpath == b'-':
   373         logfile = ui.ferr
   383         logfile = ui.ferr
   374     else:
   384     else:
   375         logfile = open(logpath, 'ab')
   385         logfile = open(logpath, 'ab')
       
   386 
       
   387     if logpath == b'-' and fp:
       
   388         logger = loggingutil.fileobjectlogger(fp, tracked)
       
   389     elif logpath == b'-':
       
   390         logger = loggingutil.fileobjectlogger(ui.ferr, tracked)
       
   391     else:
       
   392         logpath = os.path.abspath(logpath)
       
   393         vfs = vfsmod.vfs(os.path.dirname(logpath))
       
   394         logger = loggingutil.filelogger(vfs, os.path.basename(logpath), tracked)
       
   395 
       
   396     targetuis = {ui}
       
   397     if repo:
       
   398         targetuis.add(repo.baseui)
       
   399         targetuis.add(repo.ui)
       
   400     for u in targetuis:
       
   401         u.setlogger(b'cmdserver', logger)
   376 
   402 
   377 class pipeservice(object):
   403 class pipeservice(object):
   378     def __init__(self, ui, repo, opts):
   404     def __init__(self, ui, repo, opts):
   379         self.ui = ui
   405         self.ui = ui
   380         self.repo = repo
   406         self.repo = repo