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