Mercurial > hg
comparison hgext/blackbox.py @ 40730:55b053af7196
ui: manage logger instances and event filtering by core ui
The setup code in blackbox needs more tweaks since it has lots of black
magics. I'll fix them by follow-up patches.
To be clear, the goal of this series is to provide a proper way for command
server to install its own logger. I need it to debug in-memory repository
cache.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 11 Nov 2018 18:08:33 +0900 |
parents | 85372dc0cca3 |
children | eb5948f29c60 |
comparison
equal
deleted
inserted
replaced
40729:c93d046d4300 | 40730:55b053af7196 |
---|---|
51 from mercurial import ( | 51 from mercurial import ( |
52 encoding, | 52 encoding, |
53 pycompat, | 53 pycompat, |
54 registrar, | 54 registrar, |
55 ui as uimod, | 55 ui as uimod, |
56 util, | |
57 ) | 56 ) |
58 from mercurial.utils import ( | 57 from mercurial.utils import ( |
59 dateutil, | 58 dateutil, |
60 procutil, | 59 procutil, |
61 ) | 60 ) |
145 def tracked(self, event): | 144 def tracked(self, event): |
146 return b'*' in self._trackedevents or event in self._trackedevents | 145 return b'*' in self._trackedevents or event in self._trackedevents |
147 | 146 |
148 def log(self, ui, event, msg, opts): | 147 def log(self, ui, event, msg, opts): |
149 global _lastlogger | 148 global _lastlogger |
150 if not self.tracked(event): | |
151 return | |
152 | |
153 if self._bbvfs: | 149 if self._bbvfs: |
154 _lastlogger = self | 150 _lastlogger = self |
155 elif _lastlogger and _lastlogger._bbvfs: | 151 elif _lastlogger and _lastlogger._bbvfs: |
156 # certain logger instances exist outside the context of | 152 # certain logger instances exist outside the context of |
157 # a repo, so just default to the last blackbox logger that | 153 # a repo, so just default to the last blackbox logger that |
199 def setrepo(self, repo): | 195 def setrepo(self, repo): |
200 self._repo = repo | 196 self._repo = repo |
201 | 197 |
202 def wrapui(ui): | 198 def wrapui(ui): |
203 class blackboxui(ui.__class__): | 199 class blackboxui(ui.__class__): |
204 def __init__(self, src=None): | |
205 super(blackboxui, self).__init__(src) | |
206 if src and r'_bblogger' in src.__dict__: | |
207 self._bblogger = src._bblogger | |
208 | |
209 # trick to initialize logger after configuration is loaded, which | |
210 # can be replaced later with blackboxlogger(ui) in uisetup(), where | |
211 # both user and repo configurations should be available. | |
212 @util.propertycache | |
213 def _bblogger(self): | |
214 return blackboxlogger(self) | |
215 | |
216 def debug(self, *msg, **opts): | 200 def debug(self, *msg, **opts): |
217 super(blackboxui, self).debug(*msg, **opts) | 201 super(blackboxui, self).debug(*msg, **opts) |
218 if self.debugflag: | 202 if self.debugflag: |
219 self.log('debug', '%s', ''.join(msg)) | 203 self.log('debug', '%s', ''.join(msg)) |
220 | 204 |
221 def log(self, event, *msg, **opts): | |
222 super(blackboxui, self).log(event, *msg, **opts) | |
223 self._bblogger.log(self, event, msg, opts) | |
224 | |
225 ui.__class__ = blackboxui | 205 ui.__class__ = blackboxui |
226 uimod.ui = blackboxui | 206 uimod.ui = blackboxui |
227 | 207 |
228 def uisetup(ui): | 208 def uisetup(ui): |
229 wrapui(ui) | 209 wrapui(ui) |
210 | |
211 def uipopulate(ui): | |
212 ui.setlogger(b'blackbox', blackboxlogger(ui)) | |
230 | 213 |
231 def reposetup(ui, repo): | 214 def reposetup(ui, repo): |
232 # During 'hg pull' a httppeer repo is created to represent the remote repo. | 215 # During 'hg pull' a httppeer repo is created to represent the remote repo. |
233 # It doesn't have a .hg directory to put a blackbox in, so we don't do | 216 # It doesn't have a .hg directory to put a blackbox in, so we don't do |
234 # the blackbox setup for it. | 217 # the blackbox setup for it. |
235 if not repo.local(): | 218 if not repo.local(): |
236 return | 219 return |
237 | 220 |
238 logger = getattr(ui, '_bblogger', None) | 221 # Since blackbox.log is stored in the repo directory, the logger should be |
222 # instantiated per repository. | |
223 logger = blackboxlogger(ui) | |
224 ui.setlogger(b'blackbox', logger) | |
239 if logger: | 225 if logger: |
240 logger.setrepo(repo) | 226 logger.setrepo(repo) |
241 | 227 |
242 # Set _lastlogger even if ui.log is not called. This gives blackbox a | 228 # Set _lastlogger even if ui.log is not called. This gives blackbox a |
243 # fallback place to log. | 229 # fallback place to log. |