Patchwork [2,of,8] blackbox: extract logger class from ui wrapper

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 15, 2018, 1:02 p.m.
Message ID <98b744aedd32e458d01b.1542286963@mimosa>
Download mbox | patch
Permalink /patch/36605/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 15, 2018, 1:02 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1541924269 -32400
#      Sun Nov 11 17:17:49 2018 +0900
# Node ID 98b744aedd32e458d01b2ab9fb9e9e25ae0e7c34
# Parent  3b542fd216f334f3225d5df73cdc55d43ed1b257
blackbox: extract logger class from ui wrapper

This moves most functions to new blackboxlogger class. The ui wrapper will
be removed later.

Patch

diff --git a/hgext/blackbox.py b/hgext/blackbox.py
--- a/hgext/blackbox.py
+++ b/hgext/blackbox.py
@@ -127,8 +127,11 @@  def _openlogfile(ui, vfs):
                        newpath=maxfiles > 0 and path + '.1')
     return vfs(name, 'a')
 
-def wrapui(ui):
-    class blackboxui(ui.__class__):
+if True:
+    class blackboxlogger(object):
+        def __init__(self, ui):
+            self.track = ui.configlist('blackbox', 'track')
+
         @property
         def _bbvfs(self):
             vfs = None
@@ -139,19 +142,8 @@  def wrapui(ui):
                     vfs = None
             return vfs
 
-        @util.propertycache
-        def track(self):
-            return self.configlist('blackbox', 'track')
-
-        def debug(self, *msg, **opts):
-            super(blackboxui, self).debug(*msg, **opts)
-            if self.debugflag:
-                self.log('debug', '%s', ''.join(msg))
-
-        def log(self, event, *msg, **opts):
+        def log(self, ui, event, msg, opts):
             global _lastlogger
-            super(blackboxui, self).log(event, *msg, **opts)
-
             if not '*' in self.track and not event in self.track:
                 return
 
@@ -164,7 +156,7 @@  def wrapui(ui):
                 pass
             else:
                 return
-            _lastlogger._log(self, event, msg, opts)
+            _lastlogger._log(ui, event, msg, opts)
 
         def _log(self, ui, event, msg, opts):
             if getattr(self, '_bbinlog', False):
@@ -205,6 +197,29 @@  def wrapui(ui):
         def setrepo(self, repo):
             self._bbrepo = repo
 
+def wrapui(ui):
+    class blackboxui(ui.__class__):
+        def __init__(self, src=None):
+            super(blackboxui, self).__init__(src)
+            if src and r'_bblogger' in src.__dict__:
+                self._bblogger = src._bblogger
+
+        # trick to initialize logger after configuration is loaded, which
+        # can be replaced later with blackboxlogger(ui) in uisetup(), where
+        # both user and repo configurations should be available.
+        @util.propertycache
+        def _bblogger(self):
+            return blackboxlogger(self)
+
+        def debug(self, *msg, **opts):
+            super(blackboxui, self).debug(*msg, **opts)
+            if self.debugflag:
+                self.log('debug', '%s', ''.join(msg))
+
+        def log(self, event, *msg, **opts):
+            super(blackboxui, self).log(event, *msg, **opts)
+            self._bblogger.log(self, event, msg, opts)
+
     ui.__class__ = blackboxui
     uimod.ui = blackboxui
 
@@ -218,8 +233,8 @@  def reposetup(ui, repo):
     if not repo.local():
         return
 
-    if util.safehasattr(ui, 'setrepo'):
-        logger = ui
+    logger = getattr(ui, '_bblogger', None)
+    if logger:
         logger.setrepo(repo)
 
         # Set _lastlogger even if ui.log is not called. This gives blackbox a