Patchwork [1,of,5,V2] logtoprocess: extract logger class from ui wrapper

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 21, 2018, 2:03 p.m.
Message ID <96d5d57ab30e831b11e4.1542808986@mimosa>
Download mbox | patch
Permalink /patch/36689/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 21, 2018, 2:03 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1541925918 -32400
#      Sun Nov 11 17:45:18 2018 +0900
# Node ID 96d5d57ab30e831b11e48f92936c3ebcfb3e7510
# Parent  1a6bb5a85e3055d745d83e72fe17f3a6a8a13f63
logtoprocess: extract logger class from ui wrapper

It provides the same interface as the blackboxlogger. The ui wrapper will
be removed shortly.
Pulkit Goyal - Nov. 22, 2018, 9:23 a.m.
On Wed, Nov 21, 2018 at 5:08 PM Yuya Nishihara <yuya@tcha.org> wrote:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1541925918 -32400
> #      Sun Nov 11 17:45:18 2018 +0900
> # Node ID 96d5d57ab30e831b11e48f92936c3ebcfb3e7510
> # Parent  1a6bb5a85e3055d745d83e72fe17f3a6a8a13f63
> logtoprocess: extract logger class from ui wrapper
>
> It provides the same interface as the blackboxlogger. The ui wrapper will
> be removed shortly.
>

I have queued the first two patches. Many thanks!

Patch

diff --git a/hgext/logtoprocess.py b/hgext/logtoprocess.py
--- a/hgext/logtoprocess.py
+++ b/hgext/logtoprocess.py
@@ -38,6 +38,7 @@  import os
 
 from mercurial import (
     pycompat,
+    util,
 )
 from mercurial.utils import (
     procutil,
@@ -49,27 +50,50 @@  from mercurial.utils import (
 # 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: