hgext/blackbox.py
changeset 40645 fff3e213ace9
parent 40644 a9393d7600f3
child 40646 179c02baaa8c
equal deleted inserted replaced
40644:a9393d7600f3 40645:fff3e213ace9
   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 if True:
   130 class blackboxlogger(object):
   131     class blackboxlogger(object):
   131     def __init__(self, ui):
   132         def __init__(self, ui):
   132         self.track = ui.configlist('blackbox', 'track')
   133             self.track = ui.configlist('blackbox', 'track')
   133 
   134 
   134     @property
   135         @property
   135     def _bbvfs(self):
   136         def _bbvfs(self):
   136         vfs = None
   137             vfs = None
   137         repo = getattr(self, '_bbrepo', None)
   138             repo = getattr(self, '_bbrepo', None)
   138         if repo:
   139             if repo:
   139             vfs = repo.vfs
   140                 vfs = repo.vfs
   140             if not vfs.isdir('.'):
   141                 if not vfs.isdir('.'):
   141                 vfs = None
   142                     vfs = None
   142         return vfs
   143             return vfs
   143 
   144 
   144     def log(self, ui, event, msg, opts):
   145         def log(self, ui, event, msg, opts):
   145         global _lastlogger
   146             global _lastlogger
   146         if not '*' in self.track and not event in self.track:
   147             if not '*' in self.track and not event in self.track:
   147             return
   148                 return
   148 
   149 
   149         if self._bbvfs:
   150             if self._bbvfs:
   150             _lastlogger = self
   151                 _lastlogger = self
   151         elif _lastlogger and _lastlogger._bbvfs:
   152             elif _lastlogger and _lastlogger._bbvfs:
   152             # certain logger instances exist outside the context of
   153                 # certain logger instances exist outside the context of
   153             # a repo, so just default to the last blackbox logger that
   154                 # a repo, so just default to the last blackbox logger that
   154             # was seen.
   155                 # was seen.
   155             pass
   156                 pass
   156         else:
   157             else:
   157             return
   158                 return
   158         _lastlogger._log(ui, event, msg, opts)
   159             _lastlogger._log(ui, event, msg, opts)
   159 
   160 
   160     def _log(self, ui, event, msg, opts):
   161         def _log(self, ui, event, msg, opts):
   161         if getattr(self, '_bbinlog', False):
   162             if getattr(self, '_bbinlog', False):
   162             # recursion and failure guard
   163                 # recursion and failure guard
   163             return
   164                 return
   164         self._bbinlog = True
   165             self._bbinlog = True
   165         default = ui.configdate('devel', 'default-date')
   166             default = ui.configdate('devel', 'default-date')
   166         date = dateutil.datestr(default, ui.config('blackbox', 'date-format'))
   167             date = dateutil.datestr(default,
   167         user = procutil.getuser()
   168                                     ui.config('blackbox', 'date-format'))
   168         pid = '%d' % procutil.getpid()
   169             user = procutil.getuser()
   169         formattedmsg = msg[0] % msg[1:]
   170             pid = '%d' % procutil.getpid()
   170         rev = '(unknown)'
   171             formattedmsg = msg[0] % msg[1:]
   171         changed = ''
   172             rev = '(unknown)'
   172         ctx = self._bbrepo[None]
   173             changed = ''
   173         parents = ctx.parents()
   174             ctx = self._bbrepo[None]
   174         rev = ('+'.join([hex(p.node()) for p in parents]))
   175             parents = ctx.parents()
   175         if (ui.configbool('blackbox', 'dirty') and
   176             rev = ('+'.join([hex(p.node()) for p in parents]))
   176             ctx.dirty(missing=True, merge=False, branch=False)):
   177             if (ui.configbool('blackbox', 'dirty') and
   177             changed = '+'
   178                 ctx.dirty(missing=True, merge=False, branch=False)):
   178         if ui.configbool('blackbox', 'logsource'):
   179                 changed = '+'
   179             src = ' [%s]' % event
   180             if ui.configbool('blackbox', 'logsource'):
   180         else:
   181                 src = ' [%s]' % event
   181             src = ''
   182             else:
   182         try:
   183                 src = ''
   183             fmt = '%s %s @%s%s (%s)%s> %s'
   184             try:
   184             args = (date, user, rev, changed, pid, src, formattedmsg)
   185                 fmt = '%s %s @%s%s (%s)%s> %s'
   185             with _openlogfile(ui, self._bbvfs) as fp:
   186                 args = (date, user, rev, changed, pid, src, formattedmsg)
   186                 fp.write(fmt % args)
   187                 with _openlogfile(ui, self._bbvfs) as fp:
   187         except (IOError, OSError) as err:
   188                     fp.write(fmt % args)
   188             ui.debug('warning: cannot write to blackbox.log: %s\n' %
   189             except (IOError, OSError) as err:
   189                      encoding.strtolocal(err.strerror))
   190                 ui.debug('warning: cannot write to blackbox.log: %s\n' %
   190             # do not restore _bbinlog intentionally to avoid failed
   191                          encoding.strtolocal(err.strerror))
   191             # logging again
   192                 # do not restore _bbinlog intentionally to avoid failed
   192         else:
   193                 # logging again
   193             self._bbinlog = False
   194             else:
   194 
   195                 self._bbinlog = False
   195     def setrepo(self, repo):
   196 
   196         self._bbrepo = repo
   197         def setrepo(self, repo):
       
   198             self._bbrepo = repo
       
   199 
   197 
   200 def wrapui(ui):
   198 def wrapui(ui):
   201     class blackboxui(ui.__class__):
   199     class blackboxui(ui.__class__):
   202         def __init__(self, src=None):
   200         def __init__(self, src=None):
   203             super(blackboxui, self).__init__(src)
   201             super(blackboxui, self).__init__(src)