Patchwork [2,of,7] blackbox: send debug message to logger by core ui

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 27, 2018, 12:57 p.m.
Message ID <29e1cd661c3c67c2099a.1543323459@mimosa>
Download mbox | patch
Permalink /patch/36810/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 27, 2018, 12:57 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1541925286 -32400
#      Sun Nov 11 17:34:46 2018 +0900
# Node ID 29e1cd661c3c67c2099af4dd21631bee0df172ed
# Parent  ee25cad69149d026527056398ec6df3f9122e5a6
blackbox: send debug message to logger by core ui

Since the core ui.log() may recurse into ui.log() through ui.debug(), it
must guard against recursion.

The ui extension class can finally be removed.

Patch

diff --git a/hgext/blackbox.py b/hgext/blackbox.py
--- a/hgext/blackbox.py
+++ b/hgext/blackbox.py
@@ -52,7 +52,6 @@  from mercurial import (
     encoding,
     pycompat,
     registrar,
-    ui as uimod,
 )
 from mercurial.utils import (
     dateutil,
@@ -129,7 +128,6 @@  def _openlogfile(ui, vfs):
 class blackboxlogger(object):
     def __init__(self, ui):
         self._repo = None
-        self._inlog = False
         self._trackedevents = set(ui.configlist('blackbox', 'track'))
 
     @property
@@ -158,10 +156,6 @@  class blackboxlogger(object):
         _lastlogger._log(ui, event, msg, opts)
 
     def _log(self, ui, event, msg, opts):
-        if self._inlog:
-            # recursion guard
-            return
-        self._inlog = True
         default = ui.configdate('devel', 'default-date')
         date = dateutil.datestr(default, ui.config('blackbox', 'date-format'))
         user = procutil.getuser()
@@ -189,25 +183,10 @@  class blackboxlogger(object):
             self._repo = None
             ui.debug('warning: cannot write to blackbox.log: %s\n' %
                      encoding.strtolocal(err.strerror))
-        else:
-            self._inlog = False
 
     def setrepo(self, repo):
         self._repo = repo
 
-def wrapui(ui):
-    class blackboxui(ui.__class__):
-        def debug(self, *msg, **opts):
-            super(blackboxui, self).debug(*msg, **opts)
-            if self.debugflag:
-                self.log('debug', '%s', ''.join(msg))
-
-    ui.__class__ = blackboxui
-    uimod.ui = blackboxui
-
-def uisetup(ui):
-    wrapui(ui)
-
 def uipopulate(ui):
     ui.setlogger(b'blackbox', blackboxlogger(ui))
 
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1521,6 +1521,7 @@  class ui(object):
         '''
         if self.debugflag:
             self._writemsg(self._fmsgout, type='debug', *msg, **opts)
+            self.log(b'debug', b'%s', b''.join(msg))
 
     def edit(self, text, user, extra=None, editform=None, pending=None,
              repopath=None, action=None):
@@ -1740,8 +1741,14 @@  class ui(object):
                          if l.tracked(event)]
         if not activeloggers:
             return
-        for logger in activeloggers:
-            logger.log(self, event, msg, opts)
+        # guard against recursion from e.g. ui.debug()
+        registeredloggers = self._loggers
+        self._loggers = {}
+        try:
+            for logger in activeloggers:
+                logger.log(self, event, msg, opts)
+        finally:
+            self._loggers = registeredloggers
 
     def label(self, msg, label):
         '''style msg based on supplied label