comparison hgext/blackbox.py @ 28248:851c41a21869

blackbox: properly replace ui class Without this, anyone creating a ui object using: uimod.ui() skips the blackbox. Also, anyone doing ui.copy() skipped the blackbox. Unfortunately, the ui object lifestyle is a bit messy, the first one that's created is never actually initialized with subclasses, instead pieces of the subclass are adopted into the primal ui object. In order to handle this, a _partialinit method will be called to ensure that the blackboxui is properly initialized.
author timeless <timeless@mozdev.org>
date Wed, 03 Feb 2016 04:54:40 +0000
parents d2c0527af364
children ce24de063aa5
comparison
equal deleted inserted replaced
28247:d2c0527af364 28248:851c41a21869
42 from mercurial.i18n import _ 42 from mercurial.i18n import _
43 from mercurial.node import hex 43 from mercurial.node import hex
44 44
45 from mercurial import ( 45 from mercurial import (
46 cmdutil, 46 cmdutil,
47 ui as uimod,
47 util, 48 util,
48 ) 49 )
49 50
50 cmdtable = {} 51 cmdtable = {}
51 command = cmdutil.command(cmdtable) 52 command = cmdutil.command(cmdtable)
77 else: 78 else:
78 return hex(node) 79 return hex(node)
79 80
80 def wrapui(ui): 81 def wrapui(ui):
81 class blackboxui(ui.__class__): 82 class blackboxui(ui.__class__):
83 def __init__(self, src=None):
84 super(blackboxui, self).__init__(src)
85 if src is None:
86 self._partialinit()
87 else:
88 self._bbfp = src._bbfp
89 self._bbrepo = src._bbrepo
90 self._bbvfs = src._bbvfs
91
92 def _partialinit(self):
93 if util.safehasattr(self, '_bbvfs'):
94 return
95 self._bbfp = None
96 self._bbrepo = None
97 self._bbvfs = None
98
99 def copy(self):
100 self._partialinit()
101 return self.__class__(self)
102
82 @util.propertycache 103 @util.propertycache
83 def track(self): 104 def track(self):
84 return self.configlist('blackbox', 'track', ['*']) 105 return self.configlist('blackbox', 'track', ['*'])
85 106
86 def _openlogfile(self): 107 def _openlogfile(self):
120 self._bbfp.flush() 141 self._bbfp.flush()
121 142
122 def log(self, event, *msg, **opts): 143 def log(self, event, *msg, **opts):
123 global lastui 144 global lastui
124 super(blackboxui, self).log(event, *msg, **opts) 145 super(blackboxui, self).log(event, *msg, **opts)
146 self._partialinit()
125 147
126 if not '*' in self.track and not event in self.track: 148 if not '*' in self.track and not event in self.track:
127 return 149 return
128 150
129 if util.safehasattr(self, '_bbfp'): 151 if self._bbfp:
130 ui = self 152 ui = self
131 elif util.safehasattr(self, '_bbvfs'): 153 elif self._bbvfs:
132 try: 154 try:
133 self._bbfp = self._openlogfile() 155 self._bbfp = self._openlogfile()
134 except (IOError, OSError) as err: 156 except (IOError, OSError) as err:
135 self.debug('warning: cannot write to blackbox.log: %s\n' % 157 self.debug('warning: cannot write to blackbox.log: %s\n' %
136 err.strerror) 158 err.strerror)
141 # certain ui instances exist outside the context of 163 # certain ui instances exist outside the context of
142 # a repo, so just default to the last blackbox that 164 # a repo, so just default to the last blackbox that
143 # was seen. 165 # was seen.
144 ui = lastui 166 ui = lastui
145 167
146 if (util.safehasattr(ui, '_bbfp') and 168 if ui and ui._bbfp:
147 ui._bbfp is not None):
148 date = util.datestr(None, '%Y/%m/%d %H:%M:%S') 169 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
149 user = util.getuser() 170 user = util.getuser()
150 pid = str(util.getpid()) 171 pid = str(util.getpid())
151 formattedmsg = msg[0] % msg[1:] 172 formattedmsg = msg[0] % msg[1:]
152 rev = '(unknown)' 173 rev = '(unknown)'
153 changed = '' 174 changed = ''
154 if util.safehasattr(ui, '_bbrepo'): 175 if ui._bbrepo:
155 ctx = ui._bbrepo[None] 176 ctx = ui._bbrepo[None]
156 if ctx.rev() is not None: 177 if ctx.rev() is not None:
157 rev = hexfn(ctx.node()) 178 rev = hexfn(ctx.node())
158 else: 179 else:
159 parents = ctx.parents() 180 parents = ctx.parents()
167 ui._bbwrite('%s %s @%s%s (%s)> %s', 188 ui._bbwrite('%s %s @%s%s (%s)> %s',
168 date, user, rev, changed, pid, formattedmsg) 189 date, user, rev, changed, pid, formattedmsg)
169 except IOError as err: 190 except IOError as err:
170 self.debug('warning: cannot write to blackbox.log: %s\n' % 191 self.debug('warning: cannot write to blackbox.log: %s\n' %
171 err.strerror) 192 err.strerror)
172 if not lastui or util.safehasattr(ui, '_bbrepo'): 193 if not lastui or ui._bbrepo:
173 lastui = ui 194 lastui = ui
174 195
175 def setrepo(self, repo): 196 def setrepo(self, repo):
197 self._bbfp = None
198 self._bbrepo = repo
176 self._bbvfs = repo.vfs 199 self._bbvfs = repo.vfs
177 self._bbrepo = repo
178 200
179 ui.__class__ = blackboxui 201 ui.__class__ = blackboxui
202 uimod.ui = blackboxui
180 203
181 def uisetup(ui): 204 def uisetup(ui):
182 wrapui(ui) 205 wrapui(ui)
183 206
184 def reposetup(ui, repo): 207 def reposetup(ui, repo):