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