sapling/tests/extralog.py
Mark Thomas cfe64a31f7 blackbox: add util.log for blackbox logging when no ui object is available
Summary:
If a ui object is not available, then callers can't make blackbox logs.  However,
the blackbox extension has `lastui()` which lets it find a ui object.

Add `util.log`, a hook point for blackbox that lets callers log to blackbox without
having a ui object to hand.

Reviewed By: quark-zju

Differential Revision: D15495215

fbshipit-source-id: b468647ccacc6992f71b9a8be2552db860d16ebe
2019-05-28 02:40:07 -07:00

43 lines
1.2 KiB
Python

"""enable ui.log output in tests
Wraps the ``ui.log`` method, printing out events which are enabled.
To enable events add them to the ``extralog.events`` config list.
"""
from __future__ import absolute_import
from edenscm.mercurial import extensions, util
def logevent(ui, event, *msg, **opts):
items = ui.configlist("extralog", "events")
if event in items:
keywords = ""
if opts and ui.configbool("extralog", "keywords"):
keywords = " (%s)\n" % " ".join(
"%s=%s" % (n, v) for n, v in sorted(opts.items())
)
if msg:
ui.write("%s: " % event)
ui.write(msg[0] % msg[1:])
ui.write("%s" % keywords)
else:
ui.write("%s%s" % (event, keywords))
def uisetup(ui):
class extralogui(ui.__class__):
def log(self, event, *msg, **opts):
logevent(self, event, *msg, **opts)
return super(extralogui, self).log(event, *msg, **opts)
ui.__class__ = extralogui
# Wrap util.log as an inner function so that we can use the ui object.
def utillog(orig, event, *msg, **opts):
logevent(ui, event, *msg, **opts)
return orig(event, *msg, **opts)
extensions.wrapfunction(util, "log", utillog)