Show More
@@ -1,263 +1,266 | |||
|
1 | 1 | # blackbox.py - log repository events to a file for post-mortem debugging |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2010 Nicolas Dumazet |
|
4 | 4 | # Copyright 2013 Facebook, Inc. |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | 9 | """log repository events to a blackbox for debugging |
|
10 | 10 | |
|
11 | 11 | Logs event information to .hg/blackbox.log to help debug and diagnose problems. |
|
12 | 12 | The events that get logged can be configured via the blackbox.track config key. |
|
13 | 13 | |
|
14 | 14 | Examples:: |
|
15 | 15 | |
|
16 | 16 | [blackbox] |
|
17 | 17 | track = * |
|
18 | 18 | # dirty is *EXPENSIVE* (slow); |
|
19 | 19 | # each log entry indicates `+` if the repository is dirty, like :hg:`id`. |
|
20 | 20 | dirty = True |
|
21 | 21 | # record the source of log messages |
|
22 | 22 | logsource = True |
|
23 | 23 | |
|
24 | 24 | [blackbox] |
|
25 | 25 | track = command, commandfinish, commandexception, exthook, pythonhook |
|
26 | 26 | |
|
27 | 27 | [blackbox] |
|
28 | 28 | track = incoming |
|
29 | 29 | |
|
30 | 30 | [blackbox] |
|
31 | 31 | # limit the size of a log file |
|
32 | 32 | maxsize = 1.5 MB |
|
33 | 33 | # rotate up to N log files when the current one gets too big |
|
34 | 34 | maxfiles = 3 |
|
35 | 35 | |
|
36 | 36 | """ |
|
37 | 37 | |
|
38 | 38 | from __future__ import absolute_import |
|
39 | 39 | |
|
40 | 40 | import errno |
|
41 | 41 | import re |
|
42 | 42 | |
|
43 | 43 | from mercurial.i18n import _ |
|
44 | 44 | from mercurial.node import hex |
|
45 | 45 | |
|
46 | 46 | from mercurial import ( |
|
47 | 47 | registrar, |
|
48 | 48 | ui as uimod, |
|
49 | 49 | util, |
|
50 | 50 | ) |
|
51 | 51 | |
|
52 | 52 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
|
53 | 53 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
|
54 | 54 | # be specifying the version(s) of Mercurial they are tested with, or |
|
55 | 55 | # leave the attribute unspecified. |
|
56 | 56 | testedwith = 'ships-with-hg-core' |
|
57 | 57 | |
|
58 | 58 | cmdtable = {} |
|
59 | 59 | command = registrar.command(cmdtable) |
|
60 | 60 | |
|
61 | 61 | configtable = {} |
|
62 | 62 | configitem = registrar.configitem(configtable) |
|
63 | 63 | |
|
64 | 64 | configitem('blackbox', 'dirty', |
|
65 | 65 | default=False, |
|
66 | 66 | ) |
|
67 | 67 | configitem('blackbox', 'maxsize', |
|
68 | 68 | default='1 MB', |
|
69 | 69 | ) |
|
70 | configitem('blackbox', 'logsource', | |
|
71 | default=False, | |
|
72 | ) | |
|
70 | 73 | |
|
71 | 74 | lastui = None |
|
72 | 75 | |
|
73 | 76 | filehandles = {} |
|
74 | 77 | |
|
75 | 78 | def _openlog(vfs): |
|
76 | 79 | path = vfs.join('blackbox.log') |
|
77 | 80 | if path in filehandles: |
|
78 | 81 | return filehandles[path] |
|
79 | 82 | filehandles[path] = fp = vfs('blackbox.log', 'a') |
|
80 | 83 | return fp |
|
81 | 84 | |
|
82 | 85 | def _closelog(vfs): |
|
83 | 86 | path = vfs.join('blackbox.log') |
|
84 | 87 | fp = filehandles[path] |
|
85 | 88 | del filehandles[path] |
|
86 | 89 | fp.close() |
|
87 | 90 | |
|
88 | 91 | def wrapui(ui): |
|
89 | 92 | class blackboxui(ui.__class__): |
|
90 | 93 | def __init__(self, src=None): |
|
91 | 94 | super(blackboxui, self).__init__(src) |
|
92 | 95 | if src is None: |
|
93 | 96 | self._partialinit() |
|
94 | 97 | else: |
|
95 | 98 | self._bbfp = getattr(src, '_bbfp', None) |
|
96 | 99 | self._bbinlog = False |
|
97 | 100 | self._bbrepo = getattr(src, '_bbrepo', None) |
|
98 | 101 | self._bbvfs = getattr(src, '_bbvfs', None) |
|
99 | 102 | |
|
100 | 103 | def _partialinit(self): |
|
101 | 104 | if util.safehasattr(self, '_bbvfs'): |
|
102 | 105 | return |
|
103 | 106 | self._bbfp = None |
|
104 | 107 | self._bbinlog = False |
|
105 | 108 | self._bbrepo = None |
|
106 | 109 | self._bbvfs = None |
|
107 | 110 | |
|
108 | 111 | def copy(self): |
|
109 | 112 | self._partialinit() |
|
110 | 113 | return self.__class__(self) |
|
111 | 114 | |
|
112 | 115 | @util.propertycache |
|
113 | 116 | def track(self): |
|
114 | 117 | return self.configlist('blackbox', 'track', ['*']) |
|
115 | 118 | |
|
116 | 119 | def _openlogfile(self): |
|
117 | 120 | def rotate(oldpath, newpath): |
|
118 | 121 | try: |
|
119 | 122 | self._bbvfs.unlink(newpath) |
|
120 | 123 | except OSError as err: |
|
121 | 124 | if err.errno != errno.ENOENT: |
|
122 | 125 | self.debug("warning: cannot remove '%s': %s\n" % |
|
123 | 126 | (newpath, err.strerror)) |
|
124 | 127 | try: |
|
125 | 128 | if newpath: |
|
126 | 129 | self._bbvfs.rename(oldpath, newpath) |
|
127 | 130 | except OSError as err: |
|
128 | 131 | if err.errno != errno.ENOENT: |
|
129 | 132 | self.debug("warning: cannot rename '%s' to '%s': %s\n" % |
|
130 | 133 | (newpath, oldpath, err.strerror)) |
|
131 | 134 | |
|
132 | 135 | fp = _openlog(self._bbvfs) |
|
133 | 136 | maxsize = self.configbytes('blackbox', 'maxsize') |
|
134 | 137 | if maxsize > 0: |
|
135 | 138 | st = self._bbvfs.fstat(fp) |
|
136 | 139 | if st.st_size >= maxsize: |
|
137 | 140 | path = fp.name |
|
138 | 141 | _closelog(self._bbvfs) |
|
139 | 142 | maxfiles = self.configint('blackbox', 'maxfiles', 7) |
|
140 | 143 | for i in xrange(maxfiles - 1, 1, -1): |
|
141 | 144 | rotate(oldpath='%s.%d' % (path, i - 1), |
|
142 | 145 | newpath='%s.%d' % (path, i)) |
|
143 | 146 | rotate(oldpath=path, |
|
144 | 147 | newpath=maxfiles > 0 and path + '.1') |
|
145 | 148 | fp = _openlog(self._bbvfs) |
|
146 | 149 | return fp |
|
147 | 150 | |
|
148 | 151 | def _bbwrite(self, fmt, *args): |
|
149 | 152 | self._bbfp.write(fmt % args) |
|
150 | 153 | self._bbfp.flush() |
|
151 | 154 | |
|
152 | 155 | def log(self, event, *msg, **opts): |
|
153 | 156 | global lastui |
|
154 | 157 | super(blackboxui, self).log(event, *msg, **opts) |
|
155 | 158 | self._partialinit() |
|
156 | 159 | |
|
157 | 160 | if not '*' in self.track and not event in self.track: |
|
158 | 161 | return |
|
159 | 162 | |
|
160 | 163 | if self._bbfp: |
|
161 | 164 | ui = self |
|
162 | 165 | elif self._bbvfs: |
|
163 | 166 | try: |
|
164 | 167 | self._bbfp = self._openlogfile() |
|
165 | 168 | except (IOError, OSError) as err: |
|
166 | 169 | self.debug('warning: cannot write to blackbox.log: %s\n' % |
|
167 | 170 | err.strerror) |
|
168 | 171 | del self._bbvfs |
|
169 | 172 | self._bbfp = None |
|
170 | 173 | ui = self |
|
171 | 174 | else: |
|
172 | 175 | # certain ui instances exist outside the context of |
|
173 | 176 | # a repo, so just default to the last blackbox that |
|
174 | 177 | # was seen. |
|
175 | 178 | ui = lastui |
|
176 | 179 | |
|
177 | 180 | if not ui or not ui._bbfp: |
|
178 | 181 | return |
|
179 | 182 | if not lastui or ui._bbrepo: |
|
180 | 183 | lastui = ui |
|
181 | 184 | if ui._bbinlog: |
|
182 | 185 | # recursion guard |
|
183 | 186 | return |
|
184 | 187 | try: |
|
185 | 188 | ui._bbinlog = True |
|
186 | 189 | default = self.configdate('devel', 'default-date') |
|
187 | 190 | date = util.datestr(default, '%Y/%m/%d %H:%M:%S') |
|
188 | 191 | user = util.getuser() |
|
189 | 192 | pid = '%d' % util.getpid() |
|
190 | 193 | formattedmsg = msg[0] % msg[1:] |
|
191 | 194 | rev = '(unknown)' |
|
192 | 195 | changed = '' |
|
193 | 196 | if ui._bbrepo: |
|
194 | 197 | ctx = ui._bbrepo[None] |
|
195 | 198 | parents = ctx.parents() |
|
196 | 199 | rev = ('+'.join([hex(p.node()) for p in parents])) |
|
197 | 200 | if (ui.configbool('blackbox', 'dirty') and ( |
|
198 | 201 | any(ui._bbrepo.status()) or |
|
199 | 202 | any(ctx.sub(s).dirty() for s in ctx.substate) |
|
200 | 203 | )): |
|
201 | 204 | changed = '+' |
|
202 |
if ui.configbool('blackbox', 'logsource' |
|
|
205 | if ui.configbool('blackbox', 'logsource'): | |
|
203 | 206 | src = ' [%s]' % event |
|
204 | 207 | else: |
|
205 | 208 | src = '' |
|
206 | 209 | try: |
|
207 | 210 | ui._bbwrite('%s %s @%s%s (%s)%s> %s', |
|
208 | 211 | date, user, rev, changed, pid, src, formattedmsg) |
|
209 | 212 | except IOError as err: |
|
210 | 213 | self.debug('warning: cannot write to blackbox.log: %s\n' % |
|
211 | 214 | err.strerror) |
|
212 | 215 | finally: |
|
213 | 216 | ui._bbinlog = False |
|
214 | 217 | |
|
215 | 218 | def setrepo(self, repo): |
|
216 | 219 | self._bbfp = None |
|
217 | 220 | self._bbinlog = False |
|
218 | 221 | self._bbrepo = repo |
|
219 | 222 | self._bbvfs = repo.vfs |
|
220 | 223 | |
|
221 | 224 | ui.__class__ = blackboxui |
|
222 | 225 | uimod.ui = blackboxui |
|
223 | 226 | |
|
224 | 227 | def uisetup(ui): |
|
225 | 228 | wrapui(ui) |
|
226 | 229 | |
|
227 | 230 | def reposetup(ui, repo): |
|
228 | 231 | # During 'hg pull' a httppeer repo is created to represent the remote repo. |
|
229 | 232 | # It doesn't have a .hg directory to put a blackbox in, so we don't do |
|
230 | 233 | # the blackbox setup for it. |
|
231 | 234 | if not repo.local(): |
|
232 | 235 | return |
|
233 | 236 | |
|
234 | 237 | if util.safehasattr(ui, 'setrepo'): |
|
235 | 238 | ui.setrepo(repo) |
|
236 | 239 | |
|
237 | 240 | @command('^blackbox', |
|
238 | 241 | [('l', 'limit', 10, _('the number of events to show')), |
|
239 | 242 | ], |
|
240 | 243 | _('hg blackbox [OPTION]...')) |
|
241 | 244 | def blackbox(ui, repo, *revs, **opts): |
|
242 | 245 | '''view the recent repository events |
|
243 | 246 | ''' |
|
244 | 247 | |
|
245 | 248 | if not repo.vfs.exists('blackbox.log'): |
|
246 | 249 | return |
|
247 | 250 | |
|
248 | 251 | limit = opts.get('limit') |
|
249 | 252 | fp = repo.vfs('blackbox.log', 'r') |
|
250 | 253 | lines = fp.read().split('\n') |
|
251 | 254 | |
|
252 | 255 | count = 0 |
|
253 | 256 | output = [] |
|
254 | 257 | for line in reversed(lines): |
|
255 | 258 | if count >= limit: |
|
256 | 259 | break |
|
257 | 260 | |
|
258 | 261 | # count the commands by matching lines like: 2013/01/23 19:13:36 root> |
|
259 | 262 | if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line): |
|
260 | 263 | count += 1 |
|
261 | 264 | output.append(line) |
|
262 | 265 | |
|
263 | 266 | ui.status('\n'.join(reversed(output))) |
General Comments 0
You need to be logged in to leave comments.
Login now