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