hgext/blackbox.py
changeset 28247 d2c0527af364
parent 28246 b862e793ec10
child 28248 851c41a21869
equal deleted inserted replaced
28246:b862e793ec10 28247:d2c0527af364
    52 # Note for extension authors: ONLY specify testedwith = 'internal' for
    52 # Note for extension authors: ONLY specify testedwith = 'internal' for
    53 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
    53 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
    54 # be specifying the version(s) of Mercurial they are tested with, or
    54 # be specifying the version(s) of Mercurial they are tested with, or
    55 # leave the attribute unspecified.
    55 # leave the attribute unspecified.
    56 testedwith = 'internal'
    56 testedwith = 'internal'
    57 lastfp = None
    57 lastui = None
    58 
    58 
    59 filehandles = {}
    59 filehandles = {}
    60 
    60 
    61 def _openlog(vfs):
    61 def _openlog(vfs):
    62     path = vfs.join('blackbox.log')
    62     path = vfs.join('blackbox.log')
   113                     rotate(oldpath=path,
   113                     rotate(oldpath=path,
   114                            newpath=maxfiles > 0 and path + '.1')
   114                            newpath=maxfiles > 0 and path + '.1')
   115                     fp = _openlog(self._bbvfs)
   115                     fp = _openlog(self._bbvfs)
   116             return fp
   116             return fp
   117 
   117 
       
   118         def _bbwrite(self, fmt, *args):
       
   119             self._bbfp.write(fmt % args)
       
   120             self._bbfp.flush()
       
   121 
   118         def log(self, event, *msg, **opts):
   122         def log(self, event, *msg, **opts):
   119             global lastfp
   123             global lastui
   120             super(blackboxui, self).log(event, *msg, **opts)
   124             super(blackboxui, self).log(event, *msg, **opts)
   121 
   125 
   122             if not '*' in self.track and not event in self.track:
   126             if not '*' in self.track and not event in self.track:
   123                 return
   127                 return
   124 
   128 
   125             if util.safehasattr(self, '_blackbox'):
   129             if util.safehasattr(self, '_bbfp'):
   126                 fp = self._blackbox
   130                 ui = self
   127             elif util.safehasattr(self, '_bbvfs'):
   131             elif util.safehasattr(self, '_bbvfs'):
   128                 try:
   132                 try:
   129                     self._bbfp = self._openlogfile()
   133                     self._bbfp = self._openlogfile()
   130                 except (IOError, OSError) as err:
   134                 except (IOError, OSError) as err:
   131                     self.debug('warning: cannot write to blackbox.log: %s\n' %
   135                     self.debug('warning: cannot write to blackbox.log: %s\n' %
   132                                err.strerror)
   136                                err.strerror)
   133                     del self._bbvfs
   137                     del self._bbvfs
   134                     self._bbfp = None
   138                     self._bbfp = None
   135                 fp = self._bbfp
   139                 ui = self
   136             else:
   140             else:
   137                 # certain ui instances exist outside the context of
   141                 # certain ui instances exist outside the context of
   138                 # a repo, so just default to the last blackbox that
   142                 # a repo, so just default to the last blackbox that
   139                 # was seen.
   143                 # was seen.
   140                 fp = lastfp
   144                 ui = lastui
   141 
   145 
   142             if fp:
   146             if (util.safehasattr(ui, '_bbfp') and
       
   147                 ui._bbfp is not None):
   143                 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
   148                 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
   144                 user = util.getuser()
   149                 user = util.getuser()
   145                 pid = str(util.getpid())
   150                 pid = str(util.getpid())
   146                 formattedmsg = msg[0] % msg[1:]
   151                 formattedmsg = msg[0] % msg[1:]
   147                 rev = '(unknown)'
   152                 rev = '(unknown)'
   148                 changed = ''
   153                 changed = ''
   149                 if util.safehasattr(self, '_bbrepo'):
   154                 if util.safehasattr(ui, '_bbrepo'):
   150                     ctx = self._bbrepo[None]
   155                     ctx = ui._bbrepo[None]
   151                     if ctx.rev() is not None:
   156                     if ctx.rev() is not None:
   152                         rev = hexfn(ctx.node())
   157                         rev = hexfn(ctx.node())
   153                     else:
   158                     else:
   154                         parents = ctx.parents()
   159                         parents = ctx.parents()
   155                         rev = ('+'.join([hexfn(p.node()) for p in parents]))
   160                         rev = ('+'.join([hexfn(p.node()) for p in parents]))
   156                         if (self.configbool('blackbox', 'dirty', False) and (
   161                         if (ui.configbool('blackbox', 'dirty', False) and (
   157                             any(self._bbrepo.status()) or
   162                             any(ui._bbrepo.status()) or
   158                             any(ctx.sub(s).dirty() for s in ctx.substate)
   163                             any(ctx.sub(s).dirty() for s in ctx.substate)
   159                         )):
   164                         )):
   160                             changed = '+'
   165                             changed = '+'
   161                 try:
   166                 try:
   162                     fp.write('%s %s @%s%s (%s)> %s' %
   167                     ui._bbwrite('%s %s @%s%s (%s)> %s',
   163                         (date, user, rev, changed, pid, formattedmsg))
   168                         date, user, rev, changed, pid, formattedmsg)
   164                     fp.flush()
       
   165                 except IOError as err:
   169                 except IOError as err:
   166                     self.debug('warning: cannot write to blackbox.log: %s\n' %
   170                     self.debug('warning: cannot write to blackbox.log: %s\n' %
   167                                err.strerror)
   171                                err.strerror)
   168                 if not lastfp or util.safehasattr(self, '_bbrepo'):
   172                 if not lastui or util.safehasattr(ui, '_bbrepo'):
   169                     lastfp = fp
   173                     lastui = ui
   170 
   174 
   171         def setrepo(self, repo):
   175         def setrepo(self, repo):
   172             self._bbvfs = repo.vfs
   176             self._bbvfs = repo.vfs
   173             self._bbrepo = repo
   177             self._bbrepo = repo
   174 
   178