Mercurial > hg
annotate hgext/blackbox.py @ 28262:53dc4aada2d9
chgserver: add utilities to calculate confighash
confighash is the hash of sensitive config items like [extensions], and
sensitive environment variables like HG*, LD_*, etc. The config items
can come from global, user, repo config, and command line flags.
For chgserver, it is designed that once confighash changes, the server is
not qualified to serve its client and should redirect the client to a new
server. The server does not need to exit in this case, since it can still
be valid (have a matched confighash) to serve other chg clients.
author | Jun Wu <quark@fb.com> |
---|---|
date | Fri, 26 Feb 2016 14:50:04 +0000 |
parents | 851c41a21869 |
children | ce24de063aa5 |
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. |
28246 | 13 |
14 If you want to record whether the repository is dirty (a `+` sign, as you'd | |
15 get when using :hg:`id`), you can set the blackbox.dirty config key. | |
16 | |
19162
27013ace80eb
blackbox: fix literal block syntax
Takumi IINO <trot.thunder@gmail.com>
parents:
19066
diff
changeset
|
17 Examples:: |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
18 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
19 [blackbox] |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
20 track = * |
28246 | 21 dirty = True |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
22 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
23 [blackbox] |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
24 track = command, commandfinish, commandexception, exthook, pythonhook |
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 [blackbox] |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
27 track = incoming |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
28 |
19066
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
29 [blackbox] |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
30 # limit the size of a log file |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
31 maxsize = 1.5 MB |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
32 # rotate up to N log files when the current one gets too big |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
33 maxfiles = 3 |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
34 |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
35 """ |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
36 |
28090
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
37 from __future__ import absolute_import |
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
38 |
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
39 import errno |
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
40 import re |
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
41 |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
42 from mercurial.i18n import _ |
28245
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
43 from mercurial.node import hex |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
44 |
28090
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
45 from mercurial import ( |
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
46 cmdutil, |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
47 ui as uimod, |
28090
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
48 util, |
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
49 ) |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
50 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
51 cmdtable = {} |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
52 command = cmdutil.command(cmdtable) |
25186
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
23877
diff
changeset
|
53 # Note for extension authors: ONLY specify testedwith = 'internal' for |
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
23877
diff
changeset
|
54 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
23877
diff
changeset
|
55 # be specifying the version(s) of Mercurial they are tested with, or |
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
23877
diff
changeset
|
56 # leave the attribute unspecified. |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
57 testedwith = 'internal' |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
58 lastui = None |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
59 |
28243
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
60 filehandles = {} |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
61 |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
62 def _openlog(vfs): |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
63 path = vfs.join('blackbox.log') |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
64 if path in filehandles: |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
65 return filehandles[path] |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
66 filehandles[path] = fp = vfs('blackbox.log', 'a') |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
67 return fp |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
68 |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
69 def _closelog(vfs): |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
70 path = vfs.join('blackbox.log') |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
71 fp = filehandles[path] |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
72 del filehandles[path] |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
73 fp.close() |
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
74 |
28245
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
75 def hexfn(node): |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
76 if node is None: |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
77 return None |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
78 else: |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
79 return hex(node) |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
80 |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
81 def wrapui(ui): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
82 class blackboxui(ui.__class__): |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
83 def __init__(self, src=None): |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
84 super(blackboxui, self).__init__(src) |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
85 if src is None: |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
86 self._partialinit() |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
87 else: |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
88 self._bbfp = src._bbfp |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
89 self._bbrepo = src._bbrepo |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
90 self._bbvfs = src._bbvfs |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
91 |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
92 def _partialinit(self): |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
93 if util.safehasattr(self, '_bbvfs'): |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
94 return |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
95 self._bbfp = None |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
96 self._bbrepo = None |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
97 self._bbvfs = None |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
98 |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
99 def copy(self): |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
100 self._partialinit() |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
101 return self.__class__(self) |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
102 |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
103 @util.propertycache |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
104 def track(self): |
19052
63a783d1ac85
blackbox: fix a case of name capture
Bryan O'Sullivan <bryano@fb.com>
parents:
18831
diff
changeset
|
105 return self.configlist('blackbox', 'track', ['*']) |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
106 |
19066
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
107 def _openlogfile(self): |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
108 def rotate(oldpath, newpath): |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
109 try: |
28026
a6db1163befa
blackbox: refactor use of vfs as _bbvfs
timeless <timeless@mozdev.org>
parents:
28025
diff
changeset
|
110 self._bbvfs.unlink(newpath) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25186
diff
changeset
|
111 except OSError as err: |
19066
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
112 if err.errno != errno.ENOENT: |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
113 self.debug("warning: cannot remove '%s': %s\n" % |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
114 (newpath, err.strerror)) |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
115 try: |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
116 if newpath: |
28026
a6db1163befa
blackbox: refactor use of vfs as _bbvfs
timeless <timeless@mozdev.org>
parents:
28025
diff
changeset
|
117 self._bbvfs.rename(oldpath, newpath) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25186
diff
changeset
|
118 except OSError as err: |
19066
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
119 if err.errno != errno.ENOENT: |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
120 self.debug("warning: cannot rename '%s' to '%s': %s\n" % |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
121 (newpath, oldpath, err.strerror)) |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
122 |
28243
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
123 fp = _openlog(self._bbvfs) |
19066
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
124 maxsize = self.configbytes('blackbox', 'maxsize', 1048576) |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
125 if maxsize > 0: |
28026
a6db1163befa
blackbox: refactor use of vfs as _bbvfs
timeless <timeless@mozdev.org>
parents:
28025
diff
changeset
|
126 st = self._bbvfs.fstat(fp) |
19066
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
127 if st.st_size >= maxsize: |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
128 path = fp.name |
28243
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
129 _closelog(self._bbvfs) |
19066
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
130 maxfiles = self.configint('blackbox', 'maxfiles', 7) |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
131 for i in xrange(maxfiles - 1, 1, -1): |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
132 rotate(oldpath='%s.%d' % (path, i - 1), |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
133 newpath='%s.%d' % (path, i)) |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
134 rotate(oldpath=path, |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
135 newpath=maxfiles > 0 and path + '.1') |
28243
45313f5a3a8c
blackbox: avoid creating multiple file handles for a single log
timeless <timeless@mozdev.org>
parents:
28090
diff
changeset
|
136 fp = _openlog(self._bbvfs) |
19066
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
137 return fp |
2cad301a7f06
blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents:
19052
diff
changeset
|
138 |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
139 def _bbwrite(self, fmt, *args): |
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
140 self._bbfp.write(fmt % args) |
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
141 self._bbfp.flush() |
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
142 |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
143 def log(self, event, *msg, **opts): |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
144 global lastui |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
145 super(blackboxui, self).log(event, *msg, **opts) |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
146 self._partialinit() |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
147 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
148 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
|
149 return |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
150 |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
151 if self._bbfp: |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
152 ui = self |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
153 elif self._bbvfs: |
18831
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
154 try: |
28244
c17d7b1c40be
blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents:
28243
diff
changeset
|
155 self._bbfp = self._openlogfile() |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25186
diff
changeset
|
156 except (IOError, OSError) as err: |
18831
17f6644a2fbc
blackbox: defer opening a log file until needed (issue3869)
Bryan O'Sullivan <bryano@fb.com>
parents:
18810
diff
changeset
|
157 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
|
158 err.strerror) |
28026
a6db1163befa
blackbox: refactor use of vfs as _bbvfs
timeless <timeless@mozdev.org>
parents:
28025
diff
changeset
|
159 del self._bbvfs |
28244
c17d7b1c40be
blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents:
28243
diff
changeset
|
160 self._bbfp = None |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
161 ui = self |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
162 else: |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
163 # certain ui instances exist outside the context of |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
164 # a repo, so just default to the last blackbox that |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
165 # was seen. |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
166 ui = lastui |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
167 |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
168 if ui and ui._bbfp: |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
169 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
|
170 user = util.getuser() |
28027
14033c5dd261
util: enable getpid to be replaced
timeless <timeless@mozdev.org>
parents:
28026
diff
changeset
|
171 pid = str(util.getpid()) |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
172 formattedmsg = msg[0] % msg[1:] |
28245
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
173 rev = '(unknown)' |
28246 | 174 changed = '' |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
175 if ui._bbrepo: |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
176 ctx = ui._bbrepo[None] |
28245
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
177 if ctx.rev() is not None: |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
178 rev = hexfn(ctx.node()) |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
179 else: |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
180 parents = ctx.parents() |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
181 rev = ('+'.join([hexfn(p.node()) for p in parents])) |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
182 if (ui.configbool('blackbox', 'dirty', False) and ( |
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
183 any(ui._bbrepo.status()) or |
28246 | 184 any(ctx.sub(s).dirty() for s in ctx.substate) |
185 )): | |
186 changed = '+' | |
18786
ed39a8f94e95
blackbox: prevent failed I/O from causing hg to abort
Bryan O'Sullivan <bryano@fb.com>
parents:
18676
diff
changeset
|
187 try: |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
188 ui._bbwrite('%s %s @%s%s (%s)> %s', |
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
189 date, user, rev, changed, pid, formattedmsg) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25186
diff
changeset
|
190 except IOError as err: |
18786
ed39a8f94e95
blackbox: prevent failed I/O from causing hg to abort
Bryan O'Sullivan <bryano@fb.com>
parents:
18676
diff
changeset
|
191 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
|
192 err.strerror) |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
193 if not lastui or ui._bbrepo: |
28247
d2c0527af364
blackbox: store the blackbox ui object instead of the log file
timeless <timeless@mozdev.org>
parents:
28246
diff
changeset
|
194 lastui = ui |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
195 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
196 def setrepo(self, repo): |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
197 self._bbfp = None |
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
198 self._bbrepo = repo |
28026
a6db1163befa
blackbox: refactor use of vfs as _bbvfs
timeless <timeless@mozdev.org>
parents:
28025
diff
changeset
|
199 self._bbvfs = repo.vfs |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
200 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
201 ui.__class__ = blackboxui |
28248
851c41a21869
blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents:
28247
diff
changeset
|
202 uimod.ui = blackboxui |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
203 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
204 def uisetup(ui): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
205 wrapui(ui) |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
206 |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
207 def reposetup(ui, repo): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
208 # 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
|
209 # 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
|
210 # the blackbox setup for it. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
211 if not repo.local(): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
212 return |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
213 |
19230
6d6f148cada9
blackbox: fix blackbox causing exceptions in tests
Durham Goode <durham@fb.com>
parents:
19162
diff
changeset
|
214 if util.safehasattr(ui, 'setrepo'): |
6d6f148cada9
blackbox: fix blackbox causing exceptions in tests
Durham Goode <durham@fb.com>
parents:
19162
diff
changeset
|
215 ui.setrepo(repo) |
18673
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
216 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
217 @command('^blackbox', |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
218 [('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
|
219 ], |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
220 _('hg blackbox [OPTION]...')) |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
221 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
|
222 '''view the recent repository events |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
223 ''' |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
224 |
28026
a6db1163befa
blackbox: refactor use of vfs as _bbvfs
timeless <timeless@mozdev.org>
parents:
28025
diff
changeset
|
225 if not repo.vfs.exists('blackbox.log'): |
18673
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
226 return |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
227 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
228 limit = opts.get('limit') |
28244
c17d7b1c40be
blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents:
28243
diff
changeset
|
229 fp = repo.vfs('blackbox.log', 'r') |
c17d7b1c40be
blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents:
28243
diff
changeset
|
230 lines = fp.read().split('\n') |
18673
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
231 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
232 count = 0 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
233 output = [] |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
234 for line in reversed(lines): |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
235 if count >= limit: |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
236 break |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
237 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
238 # 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
|
239 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
|
240 count += 1 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
241 output.append(line) |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
242 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
243 ui.status('\n'.join(reversed(output))) |