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