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