Show More
@@ -1,250 +1,250 b'' | |||
|
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 | cmdutil, |
|
48 | 48 | ui as uimod, |
|
49 | 49 | util, |
|
50 | 50 | ) |
|
51 | 51 | |
|
52 | 52 | cmdtable = {} |
|
53 | 53 | command = cmdutil.command(cmdtable) |
|
54 | 54 | # Note for extension authors: ONLY specify testedwith = 'internal' for |
|
55 | 55 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
|
56 | 56 | # be specifying the version(s) of Mercurial they are tested with, or |
|
57 | 57 | # leave the attribute unspecified. |
|
58 | 58 | testedwith = 'internal' |
|
59 | 59 | lastui = None |
|
60 | 60 | |
|
61 | 61 | filehandles = {} |
|
62 | 62 | |
|
63 | 63 | def _openlog(vfs): |
|
64 | 64 | path = vfs.join('blackbox.log') |
|
65 | 65 | if path in filehandles: |
|
66 | 66 | return filehandles[path] |
|
67 | 67 | filehandles[path] = fp = vfs('blackbox.log', 'a') |
|
68 | 68 | return fp |
|
69 | 69 | |
|
70 | 70 | def _closelog(vfs): |
|
71 | 71 | path = vfs.join('blackbox.log') |
|
72 | 72 | fp = filehandles[path] |
|
73 | 73 | del filehandles[path] |
|
74 | 74 | fp.close() |
|
75 | 75 | |
|
76 | 76 | def wrapui(ui): |
|
77 | 77 | class blackboxui(ui.__class__): |
|
78 | 78 | def __init__(self, src=None): |
|
79 | 79 | super(blackboxui, self).__init__(src) |
|
80 | 80 | if src is None: |
|
81 | 81 | self._partialinit() |
|
82 | 82 | else: |
|
83 |
self._bbfp = src |
|
|
83 | self._bbfp = getattr(src, '_bbfp', None) | |
|
84 | 84 | self._bbinlog = False |
|
85 |
self._bbrepo = src |
|
|
86 |
self._bbvfs = src |
|
|
85 | self._bbrepo = getattr(src, '_bbrepo', None) | |
|
86 | self._bbvfs = getattr(src, '_bbvfs', None) | |
|
87 | 87 | |
|
88 | 88 | def _partialinit(self): |
|
89 | 89 | if util.safehasattr(self, '_bbvfs'): |
|
90 | 90 | return |
|
91 | 91 | self._bbfp = None |
|
92 | 92 | self._bbinlog = False |
|
93 | 93 | self._bbrepo = None |
|
94 | 94 | self._bbvfs = None |
|
95 | 95 | |
|
96 | 96 | def copy(self): |
|
97 | 97 | self._partialinit() |
|
98 | 98 | return self.__class__(self) |
|
99 | 99 | |
|
100 | 100 | @util.propertycache |
|
101 | 101 | def track(self): |
|
102 | 102 | return self.configlist('blackbox', 'track', ['*']) |
|
103 | 103 | |
|
104 | 104 | def _openlogfile(self): |
|
105 | 105 | def rotate(oldpath, newpath): |
|
106 | 106 | try: |
|
107 | 107 | self._bbvfs.unlink(newpath) |
|
108 | 108 | except OSError as err: |
|
109 | 109 | if err.errno != errno.ENOENT: |
|
110 | 110 | self.debug("warning: cannot remove '%s': %s\n" % |
|
111 | 111 | (newpath, err.strerror)) |
|
112 | 112 | try: |
|
113 | 113 | if newpath: |
|
114 | 114 | self._bbvfs.rename(oldpath, newpath) |
|
115 | 115 | except OSError as err: |
|
116 | 116 | if err.errno != errno.ENOENT: |
|
117 | 117 | self.debug("warning: cannot rename '%s' to '%s': %s\n" % |
|
118 | 118 | (newpath, oldpath, err.strerror)) |
|
119 | 119 | |
|
120 | 120 | fp = _openlog(self._bbvfs) |
|
121 | 121 | maxsize = self.configbytes('blackbox', 'maxsize', 1048576) |
|
122 | 122 | if maxsize > 0: |
|
123 | 123 | st = self._bbvfs.fstat(fp) |
|
124 | 124 | if st.st_size >= maxsize: |
|
125 | 125 | path = fp.name |
|
126 | 126 | _closelog(self._bbvfs) |
|
127 | 127 | maxfiles = self.configint('blackbox', 'maxfiles', 7) |
|
128 | 128 | for i in xrange(maxfiles - 1, 1, -1): |
|
129 | 129 | rotate(oldpath='%s.%d' % (path, i - 1), |
|
130 | 130 | newpath='%s.%d' % (path, i)) |
|
131 | 131 | rotate(oldpath=path, |
|
132 | 132 | newpath=maxfiles > 0 and path + '.1') |
|
133 | 133 | fp = _openlog(self._bbvfs) |
|
134 | 134 | return fp |
|
135 | 135 | |
|
136 | 136 | def _bbwrite(self, fmt, *args): |
|
137 | 137 | self._bbfp.write(fmt % args) |
|
138 | 138 | self._bbfp.flush() |
|
139 | 139 | |
|
140 | 140 | def log(self, event, *msg, **opts): |
|
141 | 141 | global lastui |
|
142 | 142 | super(blackboxui, self).log(event, *msg, **opts) |
|
143 | 143 | self._partialinit() |
|
144 | 144 | |
|
145 | 145 | if not '*' in self.track and not event in self.track: |
|
146 | 146 | return |
|
147 | 147 | |
|
148 | 148 | if self._bbfp: |
|
149 | 149 | ui = self |
|
150 | 150 | elif self._bbvfs: |
|
151 | 151 | try: |
|
152 | 152 | self._bbfp = self._openlogfile() |
|
153 | 153 | except (IOError, OSError) as err: |
|
154 | 154 | self.debug('warning: cannot write to blackbox.log: %s\n' % |
|
155 | 155 | err.strerror) |
|
156 | 156 | del self._bbvfs |
|
157 | 157 | self._bbfp = None |
|
158 | 158 | ui = self |
|
159 | 159 | else: |
|
160 | 160 | # certain ui instances exist outside the context of |
|
161 | 161 | # a repo, so just default to the last blackbox that |
|
162 | 162 | # was seen. |
|
163 | 163 | ui = lastui |
|
164 | 164 | |
|
165 | 165 | if not ui or not ui._bbfp: |
|
166 | 166 | return |
|
167 | 167 | if not lastui or ui._bbrepo: |
|
168 | 168 | lastui = ui |
|
169 | 169 | if ui._bbinlog: |
|
170 | 170 | # recursion guard |
|
171 | 171 | return |
|
172 | 172 | try: |
|
173 | 173 | ui._bbinlog = True |
|
174 | 174 | date = util.datestr(None, '%Y/%m/%d %H:%M:%S') |
|
175 | 175 | user = util.getuser() |
|
176 | 176 | pid = str(util.getpid()) |
|
177 | 177 | formattedmsg = msg[0] % msg[1:] |
|
178 | 178 | rev = '(unknown)' |
|
179 | 179 | changed = '' |
|
180 | 180 | if ui._bbrepo: |
|
181 | 181 | ctx = ui._bbrepo[None] |
|
182 | 182 | parents = ctx.parents() |
|
183 | 183 | rev = ('+'.join([hex(p.node()) for p in parents])) |
|
184 | 184 | if (ui.configbool('blackbox', 'dirty', False) and ( |
|
185 | 185 | any(ui._bbrepo.status()) or |
|
186 | 186 | any(ctx.sub(s).dirty() for s in ctx.substate) |
|
187 | 187 | )): |
|
188 | 188 | changed = '+' |
|
189 | 189 | if ui.configbool('blackbox', 'logsource', False): |
|
190 | 190 | src = ' [%s]' % event |
|
191 | 191 | else: |
|
192 | 192 | src = '' |
|
193 | 193 | try: |
|
194 | 194 | ui._bbwrite('%s %s @%s%s (%s)%s> %s', |
|
195 | 195 | date, user, rev, changed, pid, src, formattedmsg) |
|
196 | 196 | except IOError as err: |
|
197 | 197 | self.debug('warning: cannot write to blackbox.log: %s\n' % |
|
198 | 198 | err.strerror) |
|
199 | 199 | finally: |
|
200 | 200 | ui._bbinlog = False |
|
201 | 201 | |
|
202 | 202 | def setrepo(self, repo): |
|
203 | 203 | self._bbfp = None |
|
204 | 204 | self._bbinlog = False |
|
205 | 205 | self._bbrepo = repo |
|
206 | 206 | self._bbvfs = repo.vfs |
|
207 | 207 | |
|
208 | 208 | ui.__class__ = blackboxui |
|
209 | 209 | uimod.ui = blackboxui |
|
210 | 210 | |
|
211 | 211 | def uisetup(ui): |
|
212 | 212 | wrapui(ui) |
|
213 | 213 | |
|
214 | 214 | def reposetup(ui, repo): |
|
215 | 215 | # During 'hg pull' a httppeer repo is created to represent the remote repo. |
|
216 | 216 | # It doesn't have a .hg directory to put a blackbox in, so we don't do |
|
217 | 217 | # the blackbox setup for it. |
|
218 | 218 | if not repo.local(): |
|
219 | 219 | return |
|
220 | 220 | |
|
221 | 221 | if util.safehasattr(ui, 'setrepo'): |
|
222 | 222 | ui.setrepo(repo) |
|
223 | 223 | |
|
224 | 224 | @command('^blackbox', |
|
225 | 225 | [('l', 'limit', 10, _('the number of events to show')), |
|
226 | 226 | ], |
|
227 | 227 | _('hg blackbox [OPTION]...')) |
|
228 | 228 | def blackbox(ui, repo, *revs, **opts): |
|
229 | 229 | '''view the recent repository events |
|
230 | 230 | ''' |
|
231 | 231 | |
|
232 | 232 | if not repo.vfs.exists('blackbox.log'): |
|
233 | 233 | return |
|
234 | 234 | |
|
235 | 235 | limit = opts.get('limit') |
|
236 | 236 | fp = repo.vfs('blackbox.log', 'r') |
|
237 | 237 | lines = fp.read().split('\n') |
|
238 | 238 | |
|
239 | 239 | count = 0 |
|
240 | 240 | output = [] |
|
241 | 241 | for line in reversed(lines): |
|
242 | 242 | if count >= limit: |
|
243 | 243 | break |
|
244 | 244 | |
|
245 | 245 | # count the commands by matching lines like: 2013/01/23 19:13:36 root> |
|
246 | 246 | if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line): |
|
247 | 247 | count += 1 |
|
248 | 248 | output.append(line) |
|
249 | 249 | |
|
250 | 250 | ui.status('\n'.join(reversed(output))) |
General Comments 0
You need to be logged in to leave comments.
Login now