comparison hgext/blackbox.py @ 40643:56694b4d41b0

blackbox: rename variables to prepare extracting core logic from ui wrapper I'm going to add ui.setlogger() function so that I can enable logging feature in command server without extending ui.__class__. This prepares for it. "self" will be a logger instance, so this patch renames some of them to "ui".
author Yuya Nishihara <yuya@tcha.org>
date Sun, 11 Nov 2018 16:58:22 +0900
parents c9876c00d292
children a9393d7600f3
comparison
equal deleted inserted replaced
40642:9c3c697267db 40643:56694b4d41b0
89 ) 89 )
90 configitem('blackbox', 'date-format', 90 configitem('blackbox', 'date-format',
91 default='%Y/%m/%d %H:%M:%S', 91 default='%Y/%m/%d %H:%M:%S',
92 ) 92 )
93 93
94 lastui = None 94 _lastlogger = None
95 95
96 def _openlogfile(ui, vfs): 96 def _openlogfile(ui, vfs):
97 def rotate(oldpath, newpath): 97 def rotate(oldpath, newpath):
98 try: 98 try:
99 vfs.unlink(newpath) 99 vfs.unlink(newpath)
147 super(blackboxui, self).debug(*msg, **opts) 147 super(blackboxui, self).debug(*msg, **opts)
148 if self.debugflag: 148 if self.debugflag:
149 self.log('debug', '%s', ''.join(msg)) 149 self.log('debug', '%s', ''.join(msg))
150 150
151 def log(self, event, *msg, **opts): 151 def log(self, event, *msg, **opts):
152 global lastui 152 global _lastlogger
153 super(blackboxui, self).log(event, *msg, **opts) 153 super(blackboxui, self).log(event, *msg, **opts)
154 154
155 if not '*' in self.track and not event in self.track: 155 if not '*' in self.track and not event in self.track:
156 return 156 return
157 157
158 if self._bbvfs: 158 if self._bbvfs:
159 lastui = self 159 _lastlogger = self
160 elif lastui and lastui._bbvfs: 160 elif _lastlogger and _lastlogger._bbvfs:
161 # certain ui instances exist outside the context of 161 # certain logger instances exist outside the context of
162 # a repo, so just default to the last blackbox that 162 # a repo, so just default to the last blackbox logger that
163 # was seen. 163 # was seen.
164 pass 164 pass
165 else: 165 else:
166 return 166 return
167 lastui._log(event, msg, opts) 167 _lastlogger._log(self, event, msg, opts)
168 168
169 def _log(self, event, msg, opts): 169 def _log(self, ui, event, msg, opts):
170 if getattr(self, '_bbinlog', False): 170 if getattr(self, '_bbinlog', False):
171 # recursion and failure guard 171 # recursion and failure guard
172 return 172 return
173 self._bbinlog = True 173 self._bbinlog = True
174 default = self.configdate('devel', 'default-date') 174 default = ui.configdate('devel', 'default-date')
175 date = dateutil.datestr(default, 175 date = dateutil.datestr(default,
176 self.config('blackbox', 'date-format')) 176 ui.config('blackbox', 'date-format'))
177 user = procutil.getuser() 177 user = procutil.getuser()
178 pid = '%d' % procutil.getpid() 178 pid = '%d' % procutil.getpid()
179 formattedmsg = msg[0] % msg[1:] 179 formattedmsg = msg[0] % msg[1:]
180 rev = '(unknown)' 180 rev = '(unknown)'
181 changed = '' 181 changed = ''
182 ctx = self._bbrepo[None] 182 ctx = self._bbrepo[None]
183 parents = ctx.parents() 183 parents = ctx.parents()
184 rev = ('+'.join([hex(p.node()) for p in parents])) 184 rev = ('+'.join([hex(p.node()) for p in parents]))
185 if (self.configbool('blackbox', 'dirty') and 185 if (ui.configbool('blackbox', 'dirty') and
186 ctx.dirty(missing=True, merge=False, branch=False)): 186 ctx.dirty(missing=True, merge=False, branch=False)):
187 changed = '+' 187 changed = '+'
188 if self.configbool('blackbox', 'logsource'): 188 if ui.configbool('blackbox', 'logsource'):
189 src = ' [%s]' % event 189 src = ' [%s]' % event
190 else: 190 else:
191 src = '' 191 src = ''
192 try: 192 try:
193 fmt = '%s %s @%s%s (%s)%s> %s' 193 fmt = '%s %s @%s%s (%s)%s> %s'
194 args = (date, user, rev, changed, pid, src, formattedmsg) 194 args = (date, user, rev, changed, pid, src, formattedmsg)
195 with _openlogfile(self, self._bbvfs) as fp: 195 with _openlogfile(ui, self._bbvfs) as fp:
196 fp.write(fmt % args) 196 fp.write(fmt % args)
197 except (IOError, OSError) as err: 197 except (IOError, OSError) as err:
198 self.debug('warning: cannot write to blackbox.log: %s\n' % 198 ui.debug('warning: cannot write to blackbox.log: %s\n' %
199 encoding.strtolocal(err.strerror)) 199 encoding.strtolocal(err.strerror))
200 # do not restore _bbinlog intentionally to avoid failed 200 # do not restore _bbinlog intentionally to avoid failed
201 # logging again 201 # logging again
202 else: 202 else:
203 self._bbinlog = False 203 self._bbinlog = False
204 204
217 # the blackbox setup for it. 217 # the blackbox setup for it.
218 if not repo.local(): 218 if not repo.local():
219 return 219 return
220 220
221 if util.safehasattr(ui, 'setrepo'): 221 if util.safehasattr(ui, 'setrepo'):
222 ui.setrepo(repo) 222 logger = ui
223 223 logger.setrepo(repo)
224 # Set lastui even if ui.log is not called. This gives blackbox a 224
225 # Set _lastlogger even if ui.log is not called. This gives blackbox a
225 # fallback place to log. 226 # fallback place to log.
226 global lastui 227 global _lastlogger
227 if lastui is None: 228 if _lastlogger is None:
228 lastui = ui 229 _lastlogger = logger
229 230
230 repo._wlockfreeprefix.add('blackbox.log') 231 repo._wlockfreeprefix.add('blackbox.log')
231 232
232 @command('blackbox', 233 @command('blackbox',
233 [('l', 'limit', 10, _('the number of events to show')), 234 [('l', 'limit', 10, _('the number of events to show')),