Mercurial > hg
annotate hgext/blackbox.py @ 18849:b4ddc43ddf9d
bookmarks.updatefromremote: rename local rb to remotemarks
This local will become a parameter in an upcoming patch, so give it a more
descriptive name. The name stays similar to the existing localmarks.
author | Siddharth Agarwal <sid0@fb.com> |
---|---|
date | Fri, 29 Mar 2013 18:28:59 -0700 |
parents | 17f6644a2fbc |
children | 63a783d1ac85 |
rev | line source |
---|---|
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 _ |
18787
f56278a0a0c5
blackbox: use util.getuser for portability
Bryan O'Sullivan <bryano@fb.com>
parents:
18786
diff
changeset
|
28 import os, 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 |
18831
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
50 elif util.safehasattr(self, '_bbopener'): |
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
51 try: |
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
52 self._blackbox = self._bbopener('blackbox.log', 'a') |
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
53 except (IOError, OSError), err: |
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
54 self.debug('warning: cannot write to blackbox.log: %s\n' % |
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
55 err.strerror) |
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
56 del self._bbopener |
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
57 self._blackbox = None |
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
58 blackbox = self._blackbox |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
59 else: |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
60 # certain ui instances exist outside the context of |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
61 # a repo, so just default to the last blackbox that |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
62 # was seen. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
63 blackbox = lastblackbox |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
64 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
65 if blackbox: |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
66 date = util.datestr(None, '%Y/%m/%d %H:%M:%S') |
18787
f56278a0a0c5
blackbox: use util.getuser for portability
Bryan O'Sullivan <bryano@fb.com>
parents:
18786
diff
changeset
|
67 user = util.getuser() |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
68 formattedmsg = msg[0] % msg[1:] |
18786
ed39a8f94e95
blackbox: prevent failed I/O from causing hg to abort
Bryan O'Sullivan <bryano@fb.com>
parents:
18676
diff
changeset
|
69 try: |
ed39a8f94e95
blackbox: prevent failed I/O from causing hg to abort
Bryan O'Sullivan <bryano@fb.com>
parents:
18676
diff
changeset
|
70 blackbox.write('%s %s> %s' % (date, user, formattedmsg)) |
ed39a8f94e95
blackbox: prevent failed I/O from causing hg to abort
Bryan O'Sullivan <bryano@fb.com>
parents:
18676
diff
changeset
|
71 except IOError, err: |
ed39a8f94e95
blackbox: prevent failed I/O from causing hg to abort
Bryan O'Sullivan <bryano@fb.com>
parents:
18676
diff
changeset
|
72 self.debug('warning: cannot write to blackbox.log: %s\n' % |
ed39a8f94e95
blackbox: prevent failed I/O from causing hg to abort
Bryan O'Sullivan <bryano@fb.com>
parents:
18676
diff
changeset
|
73 err.strerror) |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
74 lastblackbox = blackbox |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
75 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
76 def setrepo(self, repo): |
18831
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
77 self._bbopener = repo.opener |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
78 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
79 ui.__class__ = blackboxui |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
80 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
81 def uisetup(ui): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
82 wrapui(ui) |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
83 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
84 def reposetup(ui, repo): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
85 # 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
|
86 # 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
|
87 # the blackbox setup for it. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
88 if not repo.local(): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
89 return |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
90 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
91 ui.setrepo(repo) |
18673
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
92 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
93 @command('^blackbox', |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
94 [('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
|
95 ], |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
96 _('hg blackbox [OPTION]...')) |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
97 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
|
98 '''view the recent repository events |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
99 ''' |
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 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
|
102 return |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
103 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
104 limit = opts.get('limit') |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
105 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
|
106 lines = blackbox.read().split('\n') |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
107 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
108 count = 0 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
109 output = [] |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
110 for line in reversed(lines): |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
111 if count >= limit: |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
112 break |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
113 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
114 # 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
|
115 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
|
116 count += 1 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
117 output.append(line) |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
118 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
119 ui.status('\n'.join(reversed(output))) |