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