##// END OF EJS Templates
blackbox: extract function to test if log event is tracked...
Yuya Nishihara -
r40684:85372dc0 default
parent child Browse files
Show More
@@ -1,275 +1,278
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 [blackbox]
37 37 # Include nanoseconds in log entries with %f (see Python function
38 38 # datetime.datetime.strftime)
39 39 date-format = '%Y-%m-%d @ %H:%M:%S.%f'
40 40
41 41 """
42 42
43 43 from __future__ import absolute_import
44 44
45 45 import errno
46 46 import re
47 47
48 48 from mercurial.i18n import _
49 49 from mercurial.node import hex
50 50
51 51 from mercurial import (
52 52 encoding,
53 53 pycompat,
54 54 registrar,
55 55 ui as uimod,
56 56 util,
57 57 )
58 58 from mercurial.utils import (
59 59 dateutil,
60 60 procutil,
61 61 )
62 62
63 63 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
64 64 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
65 65 # be specifying the version(s) of Mercurial they are tested with, or
66 66 # leave the attribute unspecified.
67 67 testedwith = 'ships-with-hg-core'
68 68
69 69 cmdtable = {}
70 70 command = registrar.command(cmdtable)
71 71
72 72 configtable = {}
73 73 configitem = registrar.configitem(configtable)
74 74
75 75 configitem('blackbox', 'dirty',
76 76 default=False,
77 77 )
78 78 configitem('blackbox', 'maxsize',
79 79 default='1 MB',
80 80 )
81 81 configitem('blackbox', 'logsource',
82 82 default=False,
83 83 )
84 84 configitem('blackbox', 'maxfiles',
85 85 default=7,
86 86 )
87 87 configitem('blackbox', 'track',
88 88 default=lambda: ['*'],
89 89 )
90 90 configitem('blackbox', 'date-format',
91 91 default='%Y/%m/%d %H:%M:%S',
92 92 )
93 93
94 94 _lastlogger = None
95 95
96 96 def _openlogfile(ui, vfs):
97 97 def rotate(oldpath, newpath):
98 98 try:
99 99 vfs.unlink(newpath)
100 100 except OSError as err:
101 101 if err.errno != errno.ENOENT:
102 102 ui.debug("warning: cannot remove '%s': %s\n" %
103 103 (newpath, err.strerror))
104 104 try:
105 105 if newpath:
106 106 vfs.rename(oldpath, newpath)
107 107 except OSError as err:
108 108 if err.errno != errno.ENOENT:
109 109 ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
110 110 (newpath, oldpath, err.strerror))
111 111
112 112 maxsize = ui.configbytes('blackbox', 'maxsize')
113 113 name = 'blackbox.log'
114 114 if maxsize > 0:
115 115 try:
116 116 st = vfs.stat(name)
117 117 except OSError:
118 118 pass
119 119 else:
120 120 if st.st_size >= maxsize:
121 121 path = vfs.join(name)
122 122 maxfiles = ui.configint('blackbox', 'maxfiles')
123 123 for i in pycompat.xrange(maxfiles - 1, 1, -1):
124 124 rotate(oldpath='%s.%d' % (path, i - 1),
125 125 newpath='%s.%d' % (path, i))
126 126 rotate(oldpath=path,
127 127 newpath=maxfiles > 0 and path + '.1')
128 128 return vfs(name, 'a')
129 129
130 130 class blackboxlogger(object):
131 131 def __init__(self, ui):
132 132 self._repo = None
133 133 self._inlog = False
134 self.track = ui.configlist('blackbox', 'track')
134 self._trackedevents = set(ui.configlist('blackbox', 'track'))
135 135
136 136 @property
137 137 def _bbvfs(self):
138 138 vfs = None
139 139 if self._repo:
140 140 vfs = self._repo.vfs
141 141 if not vfs.isdir('.'):
142 142 vfs = None
143 143 return vfs
144 144
145 def tracked(self, event):
146 return b'*' in self._trackedevents or event in self._trackedevents
147
145 148 def log(self, ui, event, msg, opts):
146 149 global _lastlogger
147 if not '*' in self.track and not event in self.track:
150 if not self.tracked(event):
148 151 return
149 152
150 153 if self._bbvfs:
151 154 _lastlogger = self
152 155 elif _lastlogger and _lastlogger._bbvfs:
153 156 # certain logger instances exist outside the context of
154 157 # a repo, so just default to the last blackbox logger that
155 158 # was seen.
156 159 pass
157 160 else:
158 161 return
159 162 _lastlogger._log(ui, event, msg, opts)
160 163
161 164 def _log(self, ui, event, msg, opts):
162 165 if self._inlog:
163 166 # recursion and failure guard
164 167 return
165 168 self._inlog = True
166 169 default = ui.configdate('devel', 'default-date')
167 170 date = dateutil.datestr(default, ui.config('blackbox', 'date-format'))
168 171 user = procutil.getuser()
169 172 pid = '%d' % procutil.getpid()
170 173 formattedmsg = msg[0] % msg[1:]
171 174 rev = '(unknown)'
172 175 changed = ''
173 176 ctx = self._repo[None]
174 177 parents = ctx.parents()
175 178 rev = ('+'.join([hex(p.node()) for p in parents]))
176 179 if (ui.configbool('blackbox', 'dirty') and
177 180 ctx.dirty(missing=True, merge=False, branch=False)):
178 181 changed = '+'
179 182 if ui.configbool('blackbox', 'logsource'):
180 183 src = ' [%s]' % event
181 184 else:
182 185 src = ''
183 186 try:
184 187 fmt = '%s %s @%s%s (%s)%s> %s'
185 188 args = (date, user, rev, changed, pid, src, formattedmsg)
186 189 with _openlogfile(ui, self._bbvfs) as fp:
187 190 fp.write(fmt % args)
188 191 except (IOError, OSError) as err:
189 192 ui.debug('warning: cannot write to blackbox.log: %s\n' %
190 193 encoding.strtolocal(err.strerror))
191 194 # do not restore _inlog intentionally to avoid failed
192 195 # logging again
193 196 else:
194 197 self._inlog = False
195 198
196 199 def setrepo(self, repo):
197 200 self._repo = repo
198 201
199 202 def wrapui(ui):
200 203 class blackboxui(ui.__class__):
201 204 def __init__(self, src=None):
202 205 super(blackboxui, self).__init__(src)
203 206 if src and r'_bblogger' in src.__dict__:
204 207 self._bblogger = src._bblogger
205 208
206 209 # trick to initialize logger after configuration is loaded, which
207 210 # can be replaced later with blackboxlogger(ui) in uisetup(), where
208 211 # both user and repo configurations should be available.
209 212 @util.propertycache
210 213 def _bblogger(self):
211 214 return blackboxlogger(self)
212 215
213 216 def debug(self, *msg, **opts):
214 217 super(blackboxui, self).debug(*msg, **opts)
215 218 if self.debugflag:
216 219 self.log('debug', '%s', ''.join(msg))
217 220
218 221 def log(self, event, *msg, **opts):
219 222 super(blackboxui, self).log(event, *msg, **opts)
220 223 self._bblogger.log(self, event, msg, opts)
221 224
222 225 ui.__class__ = blackboxui
223 226 uimod.ui = blackboxui
224 227
225 228 def uisetup(ui):
226 229 wrapui(ui)
227 230
228 231 def reposetup(ui, repo):
229 232 # During 'hg pull' a httppeer repo is created to represent the remote repo.
230 233 # It doesn't have a .hg directory to put a blackbox in, so we don't do
231 234 # the blackbox setup for it.
232 235 if not repo.local():
233 236 return
234 237
235 238 logger = getattr(ui, '_bblogger', None)
236 239 if logger:
237 240 logger.setrepo(repo)
238 241
239 242 # Set _lastlogger even if ui.log is not called. This gives blackbox a
240 243 # fallback place to log.
241 244 global _lastlogger
242 245 if _lastlogger is None:
243 246 _lastlogger = logger
244 247
245 248 repo._wlockfreeprefix.add('blackbox.log')
246 249
247 250 @command('blackbox',
248 251 [('l', 'limit', 10, _('the number of events to show')),
249 252 ],
250 253 _('hg blackbox [OPTION]...'),
251 254 helpcategory=command.CATEGORY_MAINTENANCE,
252 255 helpbasic=True)
253 256 def blackbox(ui, repo, *revs, **opts):
254 257 '''view the recent repository events
255 258 '''
256 259
257 260 if not repo.vfs.exists('blackbox.log'):
258 261 return
259 262
260 263 limit = opts.get(r'limit')
261 264 fp = repo.vfs('blackbox.log', 'r')
262 265 lines = fp.read().split('\n')
263 266
264 267 count = 0
265 268 output = []
266 269 for line in reversed(lines):
267 270 if count >= limit:
268 271 break
269 272
270 273 # count the commands by matching lines like: 2013/01/23 19:13:36 root>
271 274 if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line):
272 275 count += 1
273 276 output.append(line)
274 277
275 278 ui.status('\n'.join(reversed(output)))
General Comments 0
You need to be logged in to leave comments. Login now