comparison hgext/blackbox.py @ 40644:a9393d7600f3

blackbox: extract logger class from ui wrapper This moves most functions to new blackboxlogger class. The ui wrapper will be removed later.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 11 Nov 2018 17:17:49 +0900
parents 56694b4d41b0
children fff3e213ace9
comparison
equal deleted inserted replaced
40643:56694b4d41b0 40644:a9393d7600f3
125 newpath='%s.%d' % (path, i)) 125 newpath='%s.%d' % (path, i))
126 rotate(oldpath=path, 126 rotate(oldpath=path,
127 newpath=maxfiles > 0 and path + '.1') 127 newpath=maxfiles > 0 and path + '.1')
128 return vfs(name, 'a') 128 return vfs(name, 'a')
129 129
130 def wrapui(ui): 130 if True:
131 class blackboxui(ui.__class__): 131 class blackboxlogger(object):
132 def __init__(self, ui):
133 self.track = ui.configlist('blackbox', 'track')
134
132 @property 135 @property
133 def _bbvfs(self): 136 def _bbvfs(self):
134 vfs = None 137 vfs = None
135 repo = getattr(self, '_bbrepo', None) 138 repo = getattr(self, '_bbrepo', None)
136 if repo: 139 if repo:
137 vfs = repo.vfs 140 vfs = repo.vfs
138 if not vfs.isdir('.'): 141 if not vfs.isdir('.'):
139 vfs = None 142 vfs = None
140 return vfs 143 return vfs
141 144
142 @util.propertycache 145 def log(self, ui, event, msg, opts):
143 def track(self):
144 return self.configlist('blackbox', 'track')
145
146 def debug(self, *msg, **opts):
147 super(blackboxui, self).debug(*msg, **opts)
148 if self.debugflag:
149 self.log('debug', '%s', ''.join(msg))
150
151 def log(self, event, *msg, **opts):
152 global _lastlogger 146 global _lastlogger
153 super(blackboxui, self).log(event, *msg, **opts)
154
155 if not '*' in self.track and not event in self.track: 147 if not '*' in self.track and not event in self.track:
156 return 148 return
157 149
158 if self._bbvfs: 150 if self._bbvfs:
159 _lastlogger = self 151 _lastlogger = self
162 # a repo, so just default to the last blackbox logger that 154 # a repo, so just default to the last blackbox logger that
163 # was seen. 155 # was seen.
164 pass 156 pass
165 else: 157 else:
166 return 158 return
167 _lastlogger._log(self, event, msg, opts) 159 _lastlogger._log(ui, event, msg, opts)
168 160
169 def _log(self, ui, event, msg, opts): 161 def _log(self, ui, event, msg, opts):
170 if getattr(self, '_bbinlog', False): 162 if getattr(self, '_bbinlog', False):
171 # recursion and failure guard 163 # recursion and failure guard
172 return 164 return
203 self._bbinlog = False 195 self._bbinlog = False
204 196
205 def setrepo(self, repo): 197 def setrepo(self, repo):
206 self._bbrepo = repo 198 self._bbrepo = repo
207 199
200 def wrapui(ui):
201 class blackboxui(ui.__class__):
202 def __init__(self, src=None):
203 super(blackboxui, self).__init__(src)
204 if src and r'_bblogger' in src.__dict__:
205 self._bblogger = src._bblogger
206
207 # trick to initialize logger after configuration is loaded, which
208 # can be replaced later with blackboxlogger(ui) in uisetup(), where
209 # both user and repo configurations should be available.
210 @util.propertycache
211 def _bblogger(self):
212 return blackboxlogger(self)
213
214 def debug(self, *msg, **opts):
215 super(blackboxui, self).debug(*msg, **opts)
216 if self.debugflag:
217 self.log('debug', '%s', ''.join(msg))
218
219 def log(self, event, *msg, **opts):
220 super(blackboxui, self).log(event, *msg, **opts)
221 self._bblogger.log(self, event, msg, opts)
222
208 ui.__class__ = blackboxui 223 ui.__class__ = blackboxui
209 uimod.ui = blackboxui 224 uimod.ui = blackboxui
210 225
211 def uisetup(ui): 226 def uisetup(ui):
212 wrapui(ui) 227 wrapui(ui)
216 # It doesn't have a .hg directory to put a blackbox in, so we don't do 231 # It doesn't have a .hg directory to put a blackbox in, so we don't do
217 # the blackbox setup for it. 232 # the blackbox setup for it.
218 if not repo.local(): 233 if not repo.local():
219 return 234 return
220 235
221 if util.safehasattr(ui, 'setrepo'): 236 logger = getattr(ui, '_bblogger', None)
222 logger = ui 237 if logger:
223 logger.setrepo(repo) 238 logger.setrepo(repo)
224 239
225 # Set _lastlogger even if ui.log is not called. This gives blackbox a 240 # Set _lastlogger even if ui.log is not called. This gives blackbox a
226 # fallback place to log. 241 # fallback place to log.
227 global _lastlogger 242 global _lastlogger