[PATCH 4 of 5] dispatch: log everything

timeless timeless at mozdev.org
Fri Mar 11 12:43:56 EST 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1454482910 0
#      Wed Feb 03 07:01:50 2016 +0000
# Node ID 1a7bc043788ab7933dc46b6c9780a9dde0eed455
# Parent  a8312d440678d9a54d011efdeef3650de00b6cc1
dispatch: log everything

This makes it easier for the blackbox extension to log
what is happening (especially when things go wrong at dispatch,
which is set up in ways that don't make it easy for blackbox
to hook it).

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -66,20 +66,33 @@
         ss = ", ".join(sorted(similar))
         write(_("(did you mean one of %s?)\n") % ss)
 
-def _formatparse(write, inst):
+def _formatparse(ui, write, inst):
     similar = []
     if isinstance(inst, error.UnknownIdentifier):
         # make sure to check fileset first, as revset can invoke fileset
         similar = _getsimilar(inst.symbols, inst.function)
     if len(inst.args) > 1:
-        write(_("hg: parse error at %s: %s\n") %
+        msg = _("hg: parse error at %s: %s\n")
+        write(msg %
                          (inst.args[1], inst.args[0]))
+        if ui is not None:
+            ui.log("dispatch", msg, inst.args[1], inst.args[0])
         if (inst.args[0][0] == ' '):
-            write(_("unexpected leading whitespace\n"))
+            msg = _("unexpected leading whitespace\n")
+            write(msg)
+            if ui is not None:
+                ui.log("dispatch", msg)
     else:
-        write(_("hg: parse error: %s\n") % inst.args[0])
+        msg = _("hg: parse error: %s\n")
+        write(msg % inst.args[0])
+        if ui is not None:
+            ui.log("dispatch", msg, inst.args[0])
         _reportsimilar(write, similar)
 
+def log(ui, write, msg, *args, **opts):
+    ui.log("dispatch", msg, *args, **opts)
+    write(msg % args)
+
 def dispatch(req):
     "run the command specified in req.args"
     if req.ferr:
@@ -103,14 +116,20 @@
         if req.ferr:
             req.ui.ferr = req.ferr
     except error.Abort as inst:
-        ferr.write(_("abort: %s\n") % inst)
+        log(req.ui, ferr.write,
+            _("abort: %s\n"),
+            inst)
         if inst.hint:
-            ferr.write(_("(%s)\n") % inst.hint)
+            log(req.ui, ferr.write,
+                _("(%s)\n"),
+                inst.hint)
         return -1
     except error.ParseError as inst:
-        _formatparse(ferr.write, inst)
+        _formatparse(req.ui, ferr.write, inst)
         if inst.hint:
-            ferr.write(_("(%s)\n") % inst.hint)
+            log(req.ui, ferr.write,
+                _("(%s)\n"),
+                inst.hint)
         return -1
 
     msg = ' '.join(' ' in a and repr(a) or a for a in req.args)
@@ -121,7 +140,8 @@
         return ret
     finally:
         duration = time.time() - starttime
-        req.ui.log("commandfinish", "%s exited %s after %0.2f seconds\n",
+        req.ui.log("commandfinish",
+                   "%s exited %s after %0.2f seconds\n",
                    msg, ret or 0, duration)
 
 def _runcatch(req):
@@ -178,13 +198,16 @@
 
             # enter the debugger before command execution
             if '--debugger' in req.args:
-                ui.warn(_("entering debugger - "
-                        "type c to continue starting hg or h for help\n"))
+                log(ui, ui.warn,
+                    _("entering debugger - "
+                      "type c to continue starting hg or h for help\n"))
 
                 if (debugger != 'pdb' and
                     debugtrace[debugger] == debugtrace['pdb']):
-                    ui.warn(_("%s debugger specified "
-                              "but its module was not found\n") % debugger)
+                    log(ui, ui.warn,
+                        _("%s debugger specified "
+                          "but its module was not found\n"),
+                        debugger)
                 with demandimport.deactivated():
                     debugtrace[debugger]()
             try:
@@ -202,59 +225,81 @@
     # Global exception handling, alphabetically
     # Mercurial-specific first, followed by built-in and library exceptions
     except error.AmbiguousCommand as inst:
-        ui.warn(_("hg: command '%s' is ambiguous:\n    %s\n") %
-                (inst.args[0], " ".join(inst.args[1])))
+        log(ui, ui.warn,
+            _("hg: command '%s' is ambiguous:\n    %s\n"),
+            inst.args[0], " ".join(inst.args[1]))
     except error.ParseError as inst:
-        _formatparse(ui.warn, inst)
+        _formatparse(ui, ui.warn, inst)
         if inst.hint:
-            ui.warn(_("(%s)\n") % inst.hint)
+            log(ui, ui.warn,
+                _("(%s)\n"),
+                inst.hint)
         return -1
     except error.LockHeld as inst:
         if inst.errno == errno.ETIMEDOUT:
             reason = _('timed out waiting for lock held by %s') % inst.locker
         else:
             reason = _('lock held by %s') % inst.locker
-        ui.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason))
+        log(ui, ui.warn,
+            _("abort: %s: %s\n"),
+            inst.desc or inst.filename, reason)
     except error.LockUnavailable as inst:
-        ui.warn(_("abort: could not lock %s: %s\n") %
-               (inst.desc or inst.filename, inst.strerror))
+        log(ui, ui.warn,
+            _("abort: could not lock %s: %s\n"),
+            inst.desc or inst.filename, inst.strerror)
     except error.CommandError as inst:
         if inst.args[0]:
-            ui.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1]))
+            log(ui, ui.warn,
+                _("hg %s: %s\n"),
+                inst.args[0], inst.args[1])
             commands.help_(ui, inst.args[0], full=False, command=True)
         else:
-            ui.warn(_("hg: %s\n") % inst.args[1])
+            log(ui, ui.warn,
+                _("hg: %s\n"),
+                inst.args[1])
             commands.help_(ui, 'shortlist')
     except error.OutOfBandError as inst:
         if inst.args:
             msg = _("abort: remote error:\n")
         else:
             msg = _("abort: remote error\n")
-        ui.warn(msg)
+        log(ui, ui.warn, msg)
         if inst.args:
-            ui.warn(''.join(inst.args))
+            log(ui, ui.warn, '%s', ''.join(inst.args))
         if inst.hint:
-            ui.warn('(%s)\n' % inst.hint)
+            log(ui, ui.warn, '(%s)\n', inst.hint)
     except error.RepoError as inst:
-        ui.warn(_("abort: %s!\n") % inst)
+        log(ui, ui.warn,
+            _("abort: %s!\n"),
+            inst)
         if inst.hint:
-            ui.warn(_("(%s)\n") % inst.hint)
+            log(ui, ui.warn,
+                _("(%s)\n"),
+                inst.hint)
     except error.ResponseError as inst:
-        ui.warn(_("abort: %s") % inst.args[0])
+        msg = [_("abort: %s") % inst.args[0]]
         if not isinstance(inst.args[1], basestring):
-            ui.warn(" %r\n" % (inst.args[1],))
+            msg.append(" %r\n" % inst.args[1])
         elif not inst.args[1]:
-            ui.warn(_(" empty string\n"))
+            msg.append(_(" empty string\n"))
         else:
-            ui.warn("\n%r\n" % util.ellipsis(inst.args[1]))
+            msg.append("\n%r\n" % util.ellipsis(inst.args[1]))
+        log(ui, ui.warn, ''.join(msg))
     except error.CensoredNodeError as inst:
-        ui.warn(_("abort: file censored %s!\n") % inst)
+        log(ui, ui.warn,
+            _("abort: file censored %s!\n"),
+            inst)
     except error.RevlogError as inst:
-        ui.warn(_("abort: %s!\n") % inst)
+        log(ui, ui.warn,
+            _("abort: %s!\n"),
+            inst)
     except error.SignalInterrupt:
-        ui.warn(_("killed!\n"))
+        log(ui, ui.warn,
+            _("killed!\n"))
     except error.UnknownCommand as inst:
-        ui.warn(_("hg: unknown command '%s'\n") % inst.args[0])
+        log(ui, ui.warn,
+            _("hg: unknown command '%s'\n"),
+            inst.args[0])
         try:
             # check if the command is in a disabled extension
             # (but don't check for extensions themselves)
@@ -269,24 +314,36 @@
             if not suggested:
                 commands.help_(ui, 'shortlist')
     except error.InterventionRequired as inst:
-        ui.warn("%s\n" % inst)
+        log(ui, ui.warn, "%s\n", inst)
         if inst.hint:
-            ui.warn(_("(%s)\n") % inst.hint)
+            log(ui, ui.warn,
+                _("(%s)\n"),
+                inst.hint)
         return 1
     except error.Abort as inst:
-        ui.warn(_("abort: %s\n") % inst)
+        log(ui, ui.warn,
+            _("abort: %s\n"),
+            inst)
         if inst.hint:
-            ui.warn(_("(%s)\n") % inst.hint)
+            log(ui, ui.warn,
+                _("(%s)\n"),
+                inst.hint)
     except ImportError as inst:
-        ui.warn(_("abort: %s!\n") % inst)
+        log(ui, ui.warn,
+            _("abort: %s!\n"),
+            inst)
         m = str(inst).split()[-1]
         if m in "mpatch bdiff".split():
-            ui.warn(_("(did you forget to compile extensions?)\n"))
+            log(ui, ui.warn,
+                _("(did you forget to compile extensions?)\n"))
         elif m in "zlib".split():
-            ui.warn(_("(is your Python install correct?)\n"))
+            log(ui, ui.warn,
+                _("(is your Python install correct?)\n"))
     except IOError as inst:
         if util.safehasattr(inst, "code"):
-            ui.warn(_("abort: %s\n") % inst)
+            log(ui, ui.warn,
+                _("abort: %s\n"),
+                inst)
         elif util.safehasattr(inst, "reason"):
             try: # usually it is in the form (errno, strerror)
                 reason = inst.reason.args[1]
@@ -296,36 +353,42 @@
             if isinstance(reason, unicode):
                 # SSLError of Python 2.7.9 contains a unicode
                 reason = reason.encode(encoding.encoding, 'replace')
-            ui.warn(_("abort: error: %s\n") % reason)
+            log(ui, ui.warn,
+                _("abort: error: %s\n"),
+                reason)
         elif (util.safehasattr(inst, "args")
               and inst.args and inst.args[0] == errno.EPIPE):
             pass
         elif getattr(inst, "strerror", None):
             if getattr(inst, "filename", None):
-                ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
+                log(ui, ui.warn,
+                    _("abort: %s: %s\n"),
+                    inst.strerror, inst.filename)
             else:
-                ui.warn(_("abort: %s\n") % inst.strerror)
+                log(ui, ui.warn, _("abort: %s\n"), inst.strerror)
         else:
             raise
     except OSError as inst:
         if getattr(inst, "filename", None) is not None:
-            ui.warn(_("abort: %s: '%s'\n") % (inst.strerror, inst.filename))
+            log(ui, ui.warn,
+                _("abort: %s: '%s'\n"),
+                inst.strerror, inst.filename)
         else:
-            ui.warn(_("abort: %s\n") % inst.strerror)
+            log(ui, ui.warn, _("abort: %s\n"), inst.strerror)
     except KeyboardInterrupt:
         try:
-            ui.warn(_("interrupted!\n"))
+            log(ui, ui.warn, _("interrupted!\n"))
         except IOError as inst:
             if inst.errno != errno.EPIPE:
                 raise
     except MemoryError:
-        ui.warn(_("abort: out of memory\n"))
+        log(ui, ui.warn, _("abort: out of memory\n"))
     except SystemExit as inst:
         # Commands shouldn't sys.exit directly, but give a return code.
         # Just in case catch this and and pass exit code to caller.
         return inst.code
     except socket.error as inst:
-        ui.warn(_("abort: %s\n") % inst.args[-1])
+        log(ui, ui.warn, _("abort: %s\n"), inst.args[-1])
     except: # re-raises
         # For compatibility checking, we discard the portion of the hg
         # version after the + on the assumption that if a "normal
@@ -775,8 +838,9 @@
         cmdtable = getattr(module, 'cmdtable', {})
         overrides = [cmd for cmd in cmdtable if cmd in commands.table]
         if overrides:
-            ui.warn(_("extension '%s' overrides commands: %s\n")
-                    % (name, " ".join(overrides)))
+            log(ui, ui.warn,
+                _("extension '%s' overrides commands: %s\n"),
+                name, " ".join(overrides))
         commands.table.update(cmdtable)
         _loaded.add(name)
 
@@ -822,8 +886,9 @@
         s = get_times()
         def print_time():
             t = get_times()
-            ui.warn(_("time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n") %
-                (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3]))
+            log(ui, ui.warn,
+                _("time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n"),
+                t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3])
         atexit.register(print_time)
 
     uis = set([ui, lui])
@@ -898,7 +963,8 @@
                 repo = repo.unfiltered()
         args.insert(0, repo)
     elif rpath:
-        ui.warn(_("warning: --repository ignored\n"))
+        log(ui, ui.warn,
+            _("warning: --repository ignored\n"))
 
     msg = ' '.join(' ' in a and repr(a) or a for a in fullargs)
     ui.log("command", '%s\n', msg)
@@ -917,8 +983,9 @@
     climit = ui.configint('profiling', 'nested', default=0)
 
     if format not in ['text', 'kcachegrind']:
-        ui.warn(_("unrecognized profiling format '%s'"
-                    " - Ignored\n") % format)
+        log(ui, ui.warn,
+            _("unrecognized profiling format '%s' - Ignored\n"),
+            format)
         format = 'text'
 
     try:
@@ -980,7 +1047,9 @@
     if freq > 0:
         statprof.reset(freq)
     else:
-        ui.warn(_("invalid sampling frequency '%s' - ignoring\n") % freq)
+        log(ui, ui.warn,
+            _("invalid sampling frequency '%s' - ignoring\n"),
+            freq)
 
     statprof.start()
     try:
@@ -1005,7 +1074,9 @@
         if profiler is None:
             profiler = ui.config('profiling', 'type', default='ls')
         if profiler not in ('ls', 'stat', 'flame'):
-            ui.warn(_("unrecognized profiler '%s' - ignored\n") % profiler)
+            log(ui, ui.warn,
+                _("unrecognized profiler '%s' - ignored\n"),
+                profiler)
             profiler = 'ls'
 
         output = ui.config('profiling', 'output')


More information about the Mercurial-devel mailing list