comparison hgext/logtoprocess.py @ 40730:55b053af7196

ui: manage logger instances and event filtering by core ui The setup code in blackbox needs more tweaks since it has lots of black magics. I'll fix them by follow-up patches. To be clear, the goal of this series is to provide a proper way for command server to install its own logger. I need it to debug in-memory repository cache.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 11 Nov 2018 18:08:33 +0900
parents 2b859742ea15
children ffd574c144d2
comparison
equal deleted inserted replaced
40729:c93d046d4300 40730:55b053af7196
36 36
37 import os 37 import os
38 38
39 from mercurial import ( 39 from mercurial import (
40 pycompat, 40 pycompat,
41 util,
42 ) 41 )
43 from mercurial.utils import ( 42 from mercurial.utils import (
44 procutil, 43 procutil,
45 ) 44 )
46 45
61 60
62 def tracked(self, event): 61 def tracked(self, event):
63 return bool(self._scripts.get(event)) 62 return bool(self._scripts.get(event))
64 63
65 def log(self, ui, event, msg, opts): 64 def log(self, ui, event, msg, opts):
66 script = self._scripts.get(event) 65 script = self._scripts[event]
67 if not script:
68 return
69 env = { 66 env = {
70 b'EVENT': event, 67 b'EVENT': event,
71 b'HGPID': os.getpid(), 68 b'HGPID': os.getpid(),
72 b'MSG1': msg[0] % msg[1:], 69 b'MSG1': msg[0] % msg[1:],
73 } 70 }
75 env.update((b'OPT_%s' % key.upper(), value) 72 env.update((b'OPT_%s' % key.upper(), value)
76 for key, value in pycompat.byteskwargs(opts).items()) 73 for key, value in pycompat.byteskwargs(opts).items())
77 fullenv = procutil.shellenviron(env) 74 fullenv = procutil.shellenviron(env)
78 procutil.runbgcommand(script, fullenv, shell=True) 75 procutil.runbgcommand(script, fullenv, shell=True)
79 76
80 def uisetup(ui): 77 def uipopulate(ui):
81 78 ui.setlogger(b'logtoprocess', processlogger(ui))
82 class logtoprocessui(ui.__class__):
83 def __init__(self, src=None):
84 super(logtoprocessui, self).__init__(src)
85 if src and r'_ltplogger' in src.__dict__:
86 self._ltplogger = src._ltplogger
87
88 # trick to initialize logger after configuration is loaded, which
89 # can be replaced later with processlogger(ui) in uisetup(), where
90 # both user and repo configurations should be available.
91 @util.propertycache
92 def _ltplogger(self):
93 return processlogger(self)
94
95 def log(self, event, *msg, **opts):
96 self._ltplogger.log(self, event, msg, opts)
97 return super(logtoprocessui, self).log(event, *msg, **opts)
98
99 # Replace the class for this instance and all clones created from it:
100 ui.__class__ = logtoprocessui