2013-02-13 22:54:52 +04:00
|
|
|
# blackbox.py - log repository events to a file for post-mortem debugging
|
2013-02-13 02:08:33 +04:00
|
|
|
#
|
2013-02-13 22:54:52 +04:00
|
|
|
# Copyright 2010 Nicolas Dumazet
|
2013-02-13 02:08:33 +04:00
|
|
|
# Copyright 2013 Facebook, Inc.
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2 or any later version.
|
|
|
|
|
|
|
|
"""log repository events to a blackbox for debugging
|
|
|
|
|
|
|
|
Logs event information to .hg/blackbox.log to help debug and diagnose problems.
|
|
|
|
The events that get logged can be configured via the blackbox.track config key.
|
2016-02-09 18:44:13 +03:00
|
|
|
|
2013-05-09 06:37:20 +04:00
|
|
|
Examples::
|
2013-02-13 02:08:33 +04:00
|
|
|
|
|
|
|
[blackbox]
|
|
|
|
track = *
|
2016-03-01 13:43:52 +03:00
|
|
|
# dirty is *EXPENSIVE* (slow);
|
|
|
|
# each log entry indicates `+` if the repository is dirty, like :hg:`id`.
|
2016-02-09 18:44:13 +03:00
|
|
|
dirty = True
|
2016-02-11 22:38:26 +03:00
|
|
|
# record the source of log messages
|
|
|
|
logsource = True
|
2013-02-13 02:08:33 +04:00
|
|
|
|
|
|
|
[blackbox]
|
|
|
|
track = command, commandfinish, commandexception, exthook, pythonhook
|
|
|
|
|
|
|
|
[blackbox]
|
|
|
|
track = incoming
|
|
|
|
|
2013-04-19 03:17:59 +04:00
|
|
|
[blackbox]
|
|
|
|
# limit the size of a log file
|
|
|
|
maxsize = 1.5 MB
|
|
|
|
# rotate up to N log files when the current one gets too big
|
|
|
|
maxfiles = 3
|
|
|
|
|
2013-02-13 02:08:33 +04:00
|
|
|
"""
|
|
|
|
|
2016-02-10 04:30:38 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import errno
|
|
|
|
import re
|
|
|
|
|
2013-02-13 02:08:33 +04:00
|
|
|
from mercurial.i18n import _
|
2016-02-09 22:16:06 +03:00
|
|
|
from mercurial.node import hex
|
|
|
|
|
2016-02-10 04:30:38 +03:00
|
|
|
from mercurial import (
|
2016-01-09 17:07:20 +03:00
|
|
|
registrar,
|
2016-02-03 07:54:40 +03:00
|
|
|
ui as uimod,
|
2016-02-10 04:30:38 +03:00
|
|
|
util,
|
|
|
|
)
|
2013-02-13 02:08:33 +04:00
|
|
|
|
2016-08-23 18:26:08 +03:00
|
|
|
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
|
2015-04-28 23:44:37 +03:00
|
|
|
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
|
|
|
|
# be specifying the version(s) of Mercurial they are tested with, or
|
|
|
|
# leave the attribute unspecified.
|
2016-08-23 18:26:08 +03:00
|
|
|
testedwith = 'ships-with-hg-core'
|
2017-06-17 14:41:28 +03:00
|
|
|
|
|
|
|
cmdtable = {}
|
|
|
|
command = registrar.command(cmdtable)
|
|
|
|
|
2017-06-17 14:21:06 +03:00
|
|
|
configtable = {}
|
|
|
|
configitem = registrar.configitem(configtable)
|
|
|
|
|
2017-06-30 04:28:11 +03:00
|
|
|
configitem('blackbox', 'dirty',
|
|
|
|
default=False,
|
|
|
|
)
|
2017-06-17 14:21:06 +03:00
|
|
|
configitem('blackbox', 'maxsize',
|
2017-06-18 21:49:08 +03:00
|
|
|
default='1 MB',
|
2017-06-17 14:21:06 +03:00
|
|
|
)
|
2017-06-30 04:28:18 +03:00
|
|
|
configitem('blackbox', 'logsource',
|
|
|
|
default=False,
|
|
|
|
)
|
2017-06-17 14:21:06 +03:00
|
|
|
|
2016-02-03 20:05:04 +03:00
|
|
|
lastui = None
|
2013-02-13 02:08:33 +04:00
|
|
|
|
|
|
|
def wrapui(ui):
|
|
|
|
class blackboxui(ui.__class__):
|
2016-02-03 07:54:40 +03:00
|
|
|
def __init__(self, src=None):
|
|
|
|
super(blackboxui, self).__init__(src)
|
|
|
|
if src is None:
|
|
|
|
self._partialinit()
|
|
|
|
else:
|
2016-03-15 13:36:02 +03:00
|
|
|
self._bbfp = getattr(src, '_bbfp', None)
|
2016-03-08 23:34:59 +03:00
|
|
|
self._bbinlog = False
|
2016-03-15 13:36:02 +03:00
|
|
|
self._bbrepo = getattr(src, '_bbrepo', None)
|
|
|
|
self._bbvfs = getattr(src, '_bbvfs', None)
|
2016-02-03 07:54:40 +03:00
|
|
|
|
|
|
|
def _partialinit(self):
|
|
|
|
if util.safehasattr(self, '_bbvfs'):
|
|
|
|
return
|
|
|
|
self._bbfp = None
|
2016-03-08 23:34:59 +03:00
|
|
|
self._bbinlog = False
|
2016-02-03 07:54:40 +03:00
|
|
|
self._bbrepo = None
|
|
|
|
self._bbvfs = None
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
self._partialinit()
|
|
|
|
return self.__class__(self)
|
|
|
|
|
2013-02-13 02:08:33 +04:00
|
|
|
@util.propertycache
|
|
|
|
def track(self):
|
2013-04-18 23:55:58 +04:00
|
|
|
return self.configlist('blackbox', 'track', ['*'])
|
2013-02-13 02:08:33 +04:00
|
|
|
|
2013-04-19 03:17:59 +04:00
|
|
|
def _openlogfile(self):
|
|
|
|
def rotate(oldpath, newpath):
|
|
|
|
try:
|
2016-02-08 03:47:36 +03:00
|
|
|
self._bbvfs.unlink(newpath)
|
2015-06-24 08:20:08 +03:00
|
|
|
except OSError as err:
|
2013-04-19 03:17:59 +04:00
|
|
|
if err.errno != errno.ENOENT:
|
|
|
|
self.debug("warning: cannot remove '%s': %s\n" %
|
|
|
|
(newpath, err.strerror))
|
|
|
|
try:
|
|
|
|
if newpath:
|
2016-02-08 03:47:36 +03:00
|
|
|
self._bbvfs.rename(oldpath, newpath)
|
2015-06-24 08:20:08 +03:00
|
|
|
except OSError as err:
|
2013-04-19 03:17:59 +04:00
|
|
|
if err.errno != errno.ENOENT:
|
|
|
|
self.debug("warning: cannot rename '%s' to '%s': %s\n" %
|
|
|
|
(newpath, oldpath, err.strerror))
|
|
|
|
|
2017-06-17 14:21:06 +03:00
|
|
|
maxsize = self.configbytes('blackbox', 'maxsize')
|
2017-09-07 05:27:30 +03:00
|
|
|
name = 'blackbox.log'
|
2013-04-19 03:17:59 +04:00
|
|
|
if maxsize > 0:
|
2017-09-07 05:27:30 +03:00
|
|
|
try:
|
|
|
|
st = self._bbvfs.stat(name)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if st.st_size >= maxsize:
|
|
|
|
path = self._bbvfs.join(name)
|
|
|
|
maxfiles = self.configint('blackbox', 'maxfiles', 7)
|
|
|
|
for i in xrange(maxfiles - 1, 1, -1):
|
|
|
|
rotate(oldpath='%s.%d' % (path, i - 1),
|
|
|
|
newpath='%s.%d' % (path, i))
|
|
|
|
rotate(oldpath=path,
|
|
|
|
newpath=maxfiles > 0 and path + '.1')
|
|
|
|
return self._bbvfs(name, 'a')
|
2013-04-19 03:17:59 +04:00
|
|
|
|
2016-02-03 20:05:04 +03:00
|
|
|
def _bbwrite(self, fmt, *args):
|
|
|
|
self._bbfp.write(fmt % args)
|
|
|
|
self._bbfp.flush()
|
|
|
|
|
2013-02-13 02:08:33 +04:00
|
|
|
def log(self, event, *msg, **opts):
|
2016-02-03 20:05:04 +03:00
|
|
|
global lastui
|
2013-02-13 02:08:33 +04:00
|
|
|
super(blackboxui, self).log(event, *msg, **opts)
|
2016-02-03 07:54:40 +03:00
|
|
|
self._partialinit()
|
2013-02-13 02:08:33 +04:00
|
|
|
|
|
|
|
if not '*' in self.track and not event in self.track:
|
|
|
|
return
|
|
|
|
|
2016-02-03 07:54:40 +03:00
|
|
|
if self._bbfp:
|
2016-02-03 20:05:04 +03:00
|
|
|
ui = self
|
2016-02-03 07:54:40 +03:00
|
|
|
elif self._bbvfs:
|
2013-03-27 03:27:51 +04:00
|
|
|
try:
|
2016-02-08 06:37:26 +03:00
|
|
|
self._bbfp = self._openlogfile()
|
2015-06-24 08:20:08 +03:00
|
|
|
except (IOError, OSError) as err:
|
2013-03-27 03:27:51 +04:00
|
|
|
self.debug('warning: cannot write to blackbox.log: %s\n' %
|
|
|
|
err.strerror)
|
2016-02-08 03:47:36 +03:00
|
|
|
del self._bbvfs
|
2016-02-08 06:37:26 +03:00
|
|
|
self._bbfp = None
|
2016-02-03 20:05:04 +03:00
|
|
|
ui = self
|
2013-02-13 02:08:33 +04:00
|
|
|
else:
|
|
|
|
# certain ui instances exist outside the context of
|
|
|
|
# a repo, so just default to the last blackbox that
|
|
|
|
# was seen.
|
2016-02-03 20:05:04 +03:00
|
|
|
ui = lastui
|
2013-02-13 02:08:33 +04:00
|
|
|
|
2016-03-08 23:34:59 +03:00
|
|
|
if not ui or not ui._bbfp:
|
|
|
|
return
|
|
|
|
if not lastui or ui._bbrepo:
|
|
|
|
lastui = ui
|
|
|
|
if ui._bbinlog:
|
|
|
|
# recursion guard
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
ui._bbinlog = True
|
2017-05-16 19:36:08 +03:00
|
|
|
default = self.configdate('devel', 'default-date')
|
|
|
|
date = util.datestr(default, '%Y/%m/%d %H:%M:%S')
|
2013-03-21 00:40:06 +04:00
|
|
|
user = util.getuser()
|
2017-05-04 23:11:54 +03:00
|
|
|
pid = '%d' % util.getpid()
|
2013-02-13 02:08:33 +04:00
|
|
|
formattedmsg = msg[0] % msg[1:]
|
2016-02-09 22:16:06 +03:00
|
|
|
rev = '(unknown)'
|
2016-02-09 18:44:13 +03:00
|
|
|
changed = ''
|
2016-02-03 07:54:40 +03:00
|
|
|
if ui._bbrepo:
|
2016-02-03 20:05:04 +03:00
|
|
|
ctx = ui._bbrepo[None]
|
2016-03-01 13:45:47 +03:00
|
|
|
parents = ctx.parents()
|
|
|
|
rev = ('+'.join([hex(p.node()) for p in parents]))
|
2017-07-09 07:23:03 +03:00
|
|
|
if (ui.configbool('blackbox', 'dirty') and
|
|
|
|
ctx.dirty(missing=True, merge=False, branch=False)):
|
2016-03-01 13:45:47 +03:00
|
|
|
changed = '+'
|
2017-06-30 04:28:18 +03:00
|
|
|
if ui.configbool('blackbox', 'logsource'):
|
2016-02-11 22:38:26 +03:00
|
|
|
src = ' [%s]' % event
|
|
|
|
else:
|
|
|
|
src = ''
|
2013-03-21 00:40:05 +04:00
|
|
|
try:
|
2016-02-11 22:38:26 +03:00
|
|
|
ui._bbwrite('%s %s @%s%s (%s)%s> %s',
|
|
|
|
date, user, rev, changed, pid, src, formattedmsg)
|
2015-06-24 08:20:08 +03:00
|
|
|
except IOError as err:
|
2013-03-21 00:40:05 +04:00
|
|
|
self.debug('warning: cannot write to blackbox.log: %s\n' %
|
|
|
|
err.strerror)
|
2016-03-08 23:34:59 +03:00
|
|
|
finally:
|
|
|
|
ui._bbinlog = False
|
2013-02-13 02:08:33 +04:00
|
|
|
|
|
|
|
def setrepo(self, repo):
|
2016-02-03 07:54:40 +03:00
|
|
|
self._bbfp = None
|
2016-03-08 23:34:59 +03:00
|
|
|
self._bbinlog = False
|
2016-02-09 22:16:06 +03:00
|
|
|
self._bbrepo = repo
|
2016-02-03 07:54:40 +03:00
|
|
|
self._bbvfs = repo.vfs
|
2013-02-13 02:08:33 +04:00
|
|
|
|
|
|
|
ui.__class__ = blackboxui
|
2016-02-03 07:54:40 +03:00
|
|
|
uimod.ui = blackboxui
|
2013-02-13 02:08:33 +04:00
|
|
|
|
|
|
|
def uisetup(ui):
|
|
|
|
wrapui(ui)
|
|
|
|
|
|
|
|
def reposetup(ui, repo):
|
|
|
|
# During 'hg pull' a httppeer repo is created to represent the remote repo.
|
|
|
|
# It doesn't have a .hg directory to put a blackbox in, so we don't do
|
|
|
|
# the blackbox setup for it.
|
|
|
|
if not repo.local():
|
|
|
|
return
|
|
|
|
|
2013-05-23 04:38:24 +04:00
|
|
|
if util.safehasattr(ui, 'setrepo'):
|
|
|
|
ui.setrepo(repo)
|
2017-07-11 13:38:17 +03:00
|
|
|
repo._wlockfreeprefix.add('blackbox.log')
|
2013-02-09 21:09:46 +04:00
|
|
|
|
|
|
|
@command('^blackbox',
|
|
|
|
[('l', 'limit', 10, _('the number of events to show')),
|
|
|
|
],
|
|
|
|
_('hg blackbox [OPTION]...'))
|
|
|
|
def blackbox(ui, repo, *revs, **opts):
|
|
|
|
'''view the recent repository events
|
|
|
|
'''
|
|
|
|
|
2016-02-08 03:47:36 +03:00
|
|
|
if not repo.vfs.exists('blackbox.log'):
|
2013-02-09 21:09:46 +04:00
|
|
|
return
|
|
|
|
|
|
|
|
limit = opts.get('limit')
|
2016-02-08 06:37:26 +03:00
|
|
|
fp = repo.vfs('blackbox.log', 'r')
|
|
|
|
lines = fp.read().split('\n')
|
2013-02-09 21:09:46 +04:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
output = []
|
|
|
|
for line in reversed(lines):
|
|
|
|
if count >= limit:
|
|
|
|
break
|
|
|
|
|
|
|
|
# count the commands by matching lines like: 2013/01/23 19:13:36 root>
|
|
|
|
if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line):
|
|
|
|
count += 1
|
|
|
|
output.append(line)
|
|
|
|
|
|
|
|
ui.status('\n'.join(reversed(output)))
|