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