##// END OF EJS Templates
devel: update blackbox to use default-date...
Boris Feld -
r32412:043948c8 default
parent child Browse files
Show More
@@ -1,250 +1,251 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 registrar,
48 48 ui as uimod,
49 49 util,
50 50 )
51 51
52 52 cmdtable = {}
53 53 command = registrar.command(cmdtable)
54 54 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' 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 = 'ships-with-hg-core'
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 83 self._bbfp = getattr(src, '_bbfp', None)
84 84 self._bbinlog = False
85 85 self._bbrepo = getattr(src, '_bbrepo', None)
86 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 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
174 default = self.configdate('devel', 'default-date')
175 date = util.datestr(default, '%Y/%m/%d %H:%M:%S')
175 176 user = util.getuser()
176 177 pid = '%d' % util.getpid()
177 178 formattedmsg = msg[0] % msg[1:]
178 179 rev = '(unknown)'
179 180 changed = ''
180 181 if ui._bbrepo:
181 182 ctx = ui._bbrepo[None]
182 183 parents = ctx.parents()
183 184 rev = ('+'.join([hex(p.node()) for p in parents]))
184 185 if (ui.configbool('blackbox', 'dirty', False) and (
185 186 any(ui._bbrepo.status()) or
186 187 any(ctx.sub(s).dirty() for s in ctx.substate)
187 188 )):
188 189 changed = '+'
189 190 if ui.configbool('blackbox', 'logsource', False):
190 191 src = ' [%s]' % event
191 192 else:
192 193 src = ''
193 194 try:
194 195 ui._bbwrite('%s %s @%s%s (%s)%s> %s',
195 196 date, user, rev, changed, pid, src, formattedmsg)
196 197 except IOError as err:
197 198 self.debug('warning: cannot write to blackbox.log: %s\n' %
198 199 err.strerror)
199 200 finally:
200 201 ui._bbinlog = False
201 202
202 203 def setrepo(self, repo):
203 204 self._bbfp = None
204 205 self._bbinlog = False
205 206 self._bbrepo = repo
206 207 self._bbvfs = repo.vfs
207 208
208 209 ui.__class__ = blackboxui
209 210 uimod.ui = blackboxui
210 211
211 212 def uisetup(ui):
212 213 wrapui(ui)
213 214
214 215 def reposetup(ui, repo):
215 216 # During 'hg pull' a httppeer repo is created to represent the remote repo.
216 217 # It doesn't have a .hg directory to put a blackbox in, so we don't do
217 218 # the blackbox setup for it.
218 219 if not repo.local():
219 220 return
220 221
221 222 if util.safehasattr(ui, 'setrepo'):
222 223 ui.setrepo(repo)
223 224
224 225 @command('^blackbox',
225 226 [('l', 'limit', 10, _('the number of events to show')),
226 227 ],
227 228 _('hg blackbox [OPTION]...'))
228 229 def blackbox(ui, repo, *revs, **opts):
229 230 '''view the recent repository events
230 231 '''
231 232
232 233 if not repo.vfs.exists('blackbox.log'):
233 234 return
234 235
235 236 limit = opts.get('limit')
236 237 fp = repo.vfs('blackbox.log', 'r')
237 238 lines = fp.read().split('\n')
238 239
239 240 count = 0
240 241 output = []
241 242 for line in reversed(lines):
242 243 if count >= limit:
243 244 break
244 245
245 246 # count the commands by matching lines like: 2013/01/23 19:13:36 root>
246 247 if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line):
247 248 count += 1
248 249 output.append(line)
249 250
250 251 ui.status('\n'.join(reversed(output)))
@@ -1,17 +1,15 b''
1 1 from __future__ import absolute_import
2 2 from mercurial import (
3 3 util,
4 4 )
5 5
6 def makedate():
7 return 0, 0
6 # XXX: we should probably offer a devel option to do this in blackbox directly
8 7 def getuser():
9 8 return 'bob'
10 9 def getpid():
11 10 return 5000
12 11
13 12 # mock the date and user apis so the output is always the same
14 13 def uisetup(ui):
15 util.makedate = makedate
16 14 util.getuser = getuser
17 15 util.getpid = getpid
General Comments 0
You need to be logged in to leave comments. Login now