author | Thomas Arendsen Hein <thomas@intevation.de> |
Thu, 28 Feb 2013 21:34:44 +0100 | |
changeset 18734 | b72697653306 |
parent 18676 | 1506eb487ddd |
child 18786 | ed39a8f94e95 |
permissions | -rw-r--r-- |
18676 | 1 |
# blackbox.py - log repository events to a file for post-mortem debugging |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
2 |
# |
18676 | 3 |
# Copyright 2010 Nicolas Dumazet |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
4 |
# Copyright 2013 Facebook, Inc. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
5 |
# |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
6 |
# This software may be used and distributed according to the terms of the |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
7 |
# GNU General Public License version 2 or any later version. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
8 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
9 |
"""log repository events to a blackbox for debugging |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
10 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
11 |
Logs event information to .hg/blackbox.log to help debug and diagnose problems. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
12 |
The events that get logged can be configured via the blackbox.track config key. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
13 |
Examples: |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
14 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
15 |
[blackbox] |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
16 |
track = * |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
17 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
18 |
[blackbox] |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
19 |
track = command, commandfinish, commandexception, exthook, pythonhook |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
20 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
21 |
[blackbox] |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
22 |
track = incoming |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
23 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
24 |
""" |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
25 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
26 |
from mercurial import util, cmdutil |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
27 |
from mercurial.i18n import _ |
18675
f816aa377e0d
blackbox: fix a failing pyflakes test
Bryan O'Sullivan <bryano@fb.com>
parents:
18673
diff
changeset
|
28 |
import os, getpass, re |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
29 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
30 |
cmdtable = {} |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
31 |
command = cmdutil.command(cmdtable) |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
32 |
testedwith = 'internal' |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
33 |
lastblackbox = None |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
34 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
35 |
def wrapui(ui): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
36 |
class blackboxui(ui.__class__): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
37 |
@util.propertycache |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
38 |
def track(self): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
39 |
return ui.configlist('blackbox', 'track', ['*']) |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
40 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
41 |
def log(self, event, *msg, **opts): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
42 |
global lastblackbox |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
43 |
super(blackboxui, self).log(event, *msg, **opts) |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
44 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
45 |
if not '*' in self.track and not event in self.track: |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
46 |
return |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
47 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
48 |
if util.safehasattr(self, '_blackbox'): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
49 |
blackbox = self._blackbox |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
50 |
else: |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
51 |
# certain ui instances exist outside the context of |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
52 |
# a repo, so just default to the last blackbox that |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
53 |
# was seen. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
54 |
blackbox = lastblackbox |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
55 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
56 |
if blackbox: |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
57 |
date = util.datestr(None, '%Y/%m/%d %H:%M:%S') |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
58 |
user = getpass.getuser() |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
59 |
formattedmsg = msg[0] % msg[1:] |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
60 |
blackbox.write('%s %s> %s' % (date, user, formattedmsg)) |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
61 |
lastblackbox = blackbox |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
62 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
63 |
def setrepo(self, repo): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
64 |
self._blackbox = repo.opener('blackbox.log', 'a') |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
65 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
66 |
ui.__class__ = blackboxui |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
67 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
68 |
def uisetup(ui): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
69 |
wrapui(ui) |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
70 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
71 |
def reposetup(ui, repo): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
72 |
# During 'hg pull' a httppeer repo is created to represent the remote repo. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
73 |
# It doesn't have a .hg directory to put a blackbox in, so we don't do |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
74 |
# the blackbox setup for it. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
75 |
if not repo.local(): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
76 |
return |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
77 |
|
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
78 |
ui.setrepo(repo) |
18673
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
79 |
|
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
80 |
@command('^blackbox', |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
81 |
[('l', 'limit', 10, _('the number of events to show')), |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
82 |
], |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
83 |
_('hg blackbox [OPTION]...')) |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
84 |
def blackbox(ui, repo, *revs, **opts): |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
85 |
'''view the recent repository events |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
86 |
''' |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
87 |
|
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
88 |
if not os.path.exists(repo.join('blackbox.log')): |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
89 |
return |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
90 |
|
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
91 |
limit = opts.get('limit') |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
92 |
blackbox = repo.opener('blackbox.log', 'r') |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
93 |
lines = blackbox.read().split('\n') |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
94 |
|
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
95 |
count = 0 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
96 |
output = [] |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
97 |
for line in reversed(lines): |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
98 |
if count >= limit: |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
99 |
break |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
100 |
|
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
101 |
# count the commands by matching lines like: 2013/01/23 19:13:36 root> |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
102 |
if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line): |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
103 |
count += 1 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
104 |
output.append(line) |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
105 |
|
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
106 |
ui.status('\n'.join(reversed(output))) |