changeset 40677:2b859742ea15

logtoprocess: extract logger class from ui wrapper It provides the same interface as the blackboxlogger. The ui wrapper will be removed shortly.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 11 Nov 2018 17:45:18 +0900
parents 1a6bb5a85e30
children c72a81bc2e82
files hgext/logtoprocess.py
diffstat 1 files changed, 41 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/logtoprocess.py	Wed Nov 21 21:40:16 2018 +0900
+++ b/hgext/logtoprocess.py	Sun Nov 11 17:45:18 2018 +0900
@@ -38,6 +38,7 @@
 
 from mercurial import (
     pycompat,
+    util,
 )
 from mercurial.utils import (
     procutil,
@@ -49,27 +50,50 @@
 # leave the attribute unspecified.
 testedwith = 'ships-with-hg-core'
 
+class processlogger(object):
+    """Map log events to external commands
+
+    Arguments are passed on as environment variables.
+    """
+
+    def __init__(self, ui):
+        self._scripts = dict(ui.configitems(b'logtoprocess'))
+
+    def tracked(self, event):
+        return bool(self._scripts.get(event))
+
+    def log(self, ui, event, msg, opts):
+        script = self._scripts.get(event)
+        if not script:
+            return
+        env = {
+            b'EVENT': event,
+            b'HGPID': os.getpid(),
+            b'MSG1': msg[0] % msg[1:],
+        }
+        # keyword arguments get prefixed with OPT_ and uppercased
+        env.update((b'OPT_%s' % key.upper(), value)
+                   for key, value in pycompat.byteskwargs(opts).items())
+        fullenv = procutil.shellenviron(env)
+        procutil.runbgcommand(script, fullenv, shell=True)
+
 def uisetup(ui):
 
     class logtoprocessui(ui.__class__):
-        def log(self, event, *msg, **opts):
-            """Map log events to external commands
-
-            Arguments are passed on as environment variables.
+        def __init__(self, src=None):
+            super(logtoprocessui, self).__init__(src)
+            if src and r'_ltplogger' in src.__dict__:
+                self._ltplogger = src._ltplogger
 
-            """
-            script = self.config('logtoprocess', event)
-            if script:
-                env = {
-                    b'EVENT': event,
-                    b'HGPID': os.getpid(),
-                    b'MSG1': msg[0] % msg[1:],
-                }
-                # keyword arguments get prefixed with OPT_ and uppercased
-                env.update((b'OPT_%s' % key.upper(), value)
-                           for key, value in pycompat.byteskwargs(opts).items())
-                fullenv = procutil.shellenviron(env)
-                procutil.runbgcommand(script, fullenv, shell=True)
+        # trick to initialize logger after configuration is loaded, which
+        # can be replaced later with processlogger(ui) in uisetup(), where
+        # both user and repo configurations should be available.
+        @util.propertycache
+        def _ltplogger(self):
+            return processlogger(self)
+
+        def log(self, event, *msg, **opts):
+            self._ltplogger.log(self, event, msg, opts)
             return super(logtoprocessui, self).log(event, *msg, **opts)
 
     # Replace the class for this instance and all clones created from it: