| 1 |
# blackbox.py - log repository events to a file for post-mortem debugging |
| 2 |
# |
| 3 |
# Copyright 2010 Nicolas Dumazet |
| 4 |
# Copyright 2013 Facebook, Inc. |
| 5 |
# |
| 6 |
# This software may be used and distributed according to the terms of the |
| 7 |
# GNU General Public License version 2 or any later version. |
| 8 |
|
| 9 |
"""log repository events to a blackbox for debugging |
| 10 |
|
| 11 |
Logs event information to .hg/blackbox.log to help debug and diagnose problems. |
| 12 |
The events that get logged can be configured via the blackbox.track and |
| 13 |
blackbox.ignore config keys. |
| 14 |
|
| 15 |
Examples:: |
| 16 |
|
| 17 |
[blackbox] |
| 18 |
track = * |
| 19 |
ignore = pythonhook |
| 20 |
# dirty is *EXPENSIVE* (slow); |
| 21 |
# each log entry indicates `+` if the repository is dirty, like :hg:`id`. |
| 22 |
dirty = True |
| 23 |
# record the source of log messages |
| 24 |
logsource = True |
| 25 |
|
| 26 |
[blackbox] |
| 27 |
track = command, commandfinish, commandexception, exthook, pythonhook |
| 28 |
|
| 29 |
[blackbox] |
| 30 |
track = incoming |
| 31 |
|
| 32 |
[blackbox] |
| 33 |
# limit the size of a log file |
| 34 |
maxsize = 1.5 MB |
| 35 |
# rotate up to N log files when the current one gets too big |
| 36 |
maxfiles = 3 |
| 37 |
|
| 38 |
[blackbox] |
| 39 |
# Include microseconds in log entries with %f (see Python function |
| 40 |
# datetime.datetime.strftime) |
| 41 |
date-format = %Y-%m-%d @ %H:%M:%S.%f |
| 42 |
|
| 43 |
""" |
| 44 |
|
| 45 |
from __future__ import annotations |
| 46 |
|
| 47 |
import re |
| 48 |
|
| 49 |
from mercurial.i18n import _ |
| 50 |
from mercurial.node import hex |
| 51 |
|
| 52 |
from mercurial import ( |
| 53 |
encoding, |
| 54 |
loggingutil, |
| 55 |
registrar, |
| 56 |
) |
| 57 |
from mercurial.utils import ( |
| 58 |
dateutil, |
| 59 |
procutil, |
| 60 |
) |
| 61 |
|
| 62 |
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
| 63 |
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
| 64 |
# be specifying the version(s) of Mercurial they are tested with, or |
| 65 |
# leave the attribute unspecified. |
| 66 |
testedwith = b'ships-with-hg-core' |
| 67 |
|
| 68 |
cmdtable = {} |
| 69 |
command = registrar.command(cmdtable) |
| 70 |
|
| 71 |
_lastlogger = loggingutil.proxylogger() |
| 72 |
|
| 73 |
|
| 74 |
class blackboxlogger: |
| 75 |
def __init__(self, ui, repo): |
| 76 |
self._repo = repo |
| 77 |
self._trackedevents = set(ui.configlist(b'blackbox', b'track')) |
| 78 |
self._ignoredevents = set(ui.configlist(b'blackbox', b'ignore')) |
| 79 |
self._maxfiles = ui.configint(b'blackbox', b'maxfiles') |
| 80 |
self._maxsize = ui.configbytes(b'blackbox', b'maxsize') |
| 81 |
self._inlog = False |
| 82 |
|
| 83 |
def tracked(self, event): |
| 84 |
return ( |
| 85 |
b'*' in self._trackedevents and event not in self._ignoredevents |
| 86 |
) or event in self._trackedevents |
| 87 |
|
| 88 |
def log(self, ui, event, msg, opts): |
| 89 |
# self._log() -> ctx.dirty() may create new subrepo instance, which |
| 90 |
# ui is derived from baseui. So the recursion guard in ui.log() |
| 91 |
# doesn't work as it's local to the ui instance. |
| 92 |
if self._inlog: |
| 93 |
return |
| 94 |
self._inlog = True |
| 95 |
try: |
| 96 |
self._log(ui, event, msg, opts) |
| 97 |
finally: |
| 98 |
self._inlog = False |
| 99 |
|
| 100 |
def _log(self, ui, event, msg, opts): |
| 101 |
default = ui.configdate(b'devel', b'default-date') |
| 102 |
dateformat = ui.config(b'blackbox', b'date-format') |
| 103 |
debug_to_stderr = ui.configbool(b'blackbox', b'debug.to-stderr') |
| 104 |
if dateformat: |
| 105 |
date = dateutil.datestr(default, dateformat) |
| 106 |
else: |
| 107 |
# We want to display milliseconds (more precision seems |
| 108 |
# unnecessary). Since %.3f is not supported, use %f and truncate |
| 109 |
# microseconds. |
| 110 |
date = dateutil.datestr(default, b'%Y-%m-%d %H:%M:%S.%f')[:-3] |
| 111 |
user = procutil.getuser() |
| 112 |
pid = b'%d' % procutil.getpid() |
| 113 |
changed = b'' |
| 114 |
ctx = self._repo[None] |
| 115 |
parents = ctx.parents() |
| 116 |
rev = b'+'.join([hex(p.node()) for p in parents]) |
| 117 |
if ui.configbool(b'blackbox', b'dirty') and ctx.dirty( |
| 118 |
missing=True, merge=False, branch=False |
| 119 |
): |
| 120 |
changed = b'+' |
| 121 |
if ui.configbool(b'blackbox', b'logsource'): |
| 122 |
src = b' [%s]' % event |
| 123 |
else: |
| 124 |
src = b'' |
| 125 |
try: |
| 126 |
fmt = b'%s %s @%s%s (%s)%s> %s' |
| 127 |
args = (date, user, rev, changed, pid, src, msg) |
| 128 |
with loggingutil.openlogfile( |
| 129 |
ui, |
| 130 |
self._repo.vfs, |
| 131 |
name=b'blackbox.log', |
| 132 |
maxfiles=self._maxfiles, |
| 133 |
maxsize=self._maxsize, |
| 134 |
) as fp: |
| 135 |
msg = fmt % args |
| 136 |
fp.write(msg) |
| 137 |
if debug_to_stderr: |
| 138 |
ui.write_err(msg) |
| 139 |
except OSError as err: |
| 140 |
# deactivate this to avoid failed logging again |
| 141 |
self._trackedevents.clear() |
| 142 |
ui.debug( |
| 143 |
b'warning: cannot write to blackbox.log: %s\n' |
| 144 |
% encoding.strtolocal(err.strerror) |
| 145 |
) |
| 146 |
return |
| 147 |
_lastlogger.logger = self |
| 148 |
|
| 149 |
|
| 150 |
def uipopulate(ui): |
| 151 |
ui.setlogger(b'blackbox', _lastlogger) |
| 152 |
|
| 153 |
|
| 154 |
def reposetup(ui, repo): |
| 155 |
# During 'hg pull' a httppeer repo is created to represent the remote repo. |
| 156 |
# It doesn't have a .hg directory to put a blackbox in, so we don't do |
| 157 |
# the blackbox setup for it. |
| 158 |
if not repo.local(): |
| 159 |
return |
| 160 |
|
| 161 |
# Since blackbox.log is stored in the repo directory, the logger should be |
| 162 |
# instantiated per repository. |
| 163 |
logger = blackboxlogger(ui, repo) |
| 164 |
ui.setlogger(b'blackbox', logger) |
| 165 |
|
| 166 |
# Set _lastlogger even if ui.log is not called. This gives blackbox a |
| 167 |
# fallback place to log |
| 168 |
if _lastlogger.logger is None: |
| 169 |
_lastlogger.logger = logger |
| 170 |
|
| 171 |
repo._wlockfreeprefix.add(b'blackbox.log') |
| 172 |
|
| 173 |
|
| 174 |
@command( |
| 175 |
b'blackbox', |
| 176 |
[ |
| 177 |
(b'l', b'limit', 10, _(b'the number of events to show')), |
| 178 |
], |
| 179 |
_(b'hg blackbox [OPTION]...'), |
| 180 |
helpcategory=command.CATEGORY_MAINTENANCE, |
| 181 |
helpbasic=True, |
| 182 |
) |
| 183 |
def blackbox(ui, repo, *revs, **opts): |
| 184 |
"""view the recent repository events""" |
| 185 |
|
| 186 |
if not repo.vfs.exists(b'blackbox.log'): |
| 187 |
return |
| 188 |
|
| 189 |
limit = opts.get('limit') |
| 190 |
assert limit is not None # help pytype |
| 191 |
|
| 192 |
fp = repo.vfs(b'blackbox.log', b'r') |
| 193 |
lines = fp.read().split(b'\n') |
| 194 |
|
| 195 |
count = 0 |
| 196 |
output = [] |
| 197 |
for line in reversed(lines): |
| 198 |
if count >= limit: |
| 199 |
break |
| 200 |
|
| 201 |
# count the commands by matching lines like: |
| 202 |
# 2013/01/23 19:13:36 root> |
| 203 |
# 2013/01/23 19:13:36 root (1234)> |
| 204 |
# 2013/01/23 19:13:36 root @0000000000000000000000000000000000000000 (1234)> |
| 205 |
# 2013-01-23 19:13:36.000 root @0000000000000000000000000000000000000000 (1234)> |
| 206 |
if re.match( |
| 207 |
br'^\d{4}[-/]\d{2}[-/]\d{2} \d{2}:\d{2}:\d{2}(.\d*)? .*> .*', line |
| 208 |
): |
| 209 |
count += 1 |
| 210 |
output.append(line) |
| 211 |
|
| 212 |
ui.status(b'\n'.join(reversed(output))) |