hgext/blackbox.py
changeset 28244 c17d7b1c40be
parent 28243 45313f5a3a8c
child 28245 caa2a0c6fbb7
equal deleted inserted replaced
28243:45313f5a3a8c 28244:c17d7b1c40be
    45 # Note for extension authors: ONLY specify testedwith = 'internal' for
    45 # Note for extension authors: ONLY specify testedwith = 'internal' for
    46 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
    46 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
    47 # be specifying the version(s) of Mercurial they are tested with, or
    47 # be specifying the version(s) of Mercurial they are tested with, or
    48 # leave the attribute unspecified.
    48 # leave the attribute unspecified.
    49 testedwith = 'internal'
    49 testedwith = 'internal'
    50 lastblackbox = None
    50 lastfp = None
    51 
    51 
    52 filehandles = {}
    52 filehandles = {}
    53 
    53 
    54 def _openlog(vfs):
    54 def _openlog(vfs):
    55     path = vfs.join('blackbox.log')
    55     path = vfs.join('blackbox.log')
   101                            newpath=maxfiles > 0 and path + '.1')
   101                            newpath=maxfiles > 0 and path + '.1')
   102                     fp = _openlog(self._bbvfs)
   102                     fp = _openlog(self._bbvfs)
   103             return fp
   103             return fp
   104 
   104 
   105         def log(self, event, *msg, **opts):
   105         def log(self, event, *msg, **opts):
   106             global lastblackbox
   106             global lastfp
   107             super(blackboxui, self).log(event, *msg, **opts)
   107             super(blackboxui, self).log(event, *msg, **opts)
   108 
   108 
   109             if not '*' in self.track and not event in self.track:
   109             if not '*' in self.track and not event in self.track:
   110                 return
   110                 return
   111 
   111 
   112             if util.safehasattr(self, '_blackbox'):
   112             if util.safehasattr(self, '_blackbox'):
   113                 blackbox = self._blackbox
   113                 fp = self._blackbox
   114             elif util.safehasattr(self, '_bbvfs'):
   114             elif util.safehasattr(self, '_bbvfs'):
   115                 try:
   115                 try:
   116                     self._blackbox = self._openlogfile()
   116                     self._bbfp = self._openlogfile()
   117                 except (IOError, OSError) as err:
   117                 except (IOError, OSError) as err:
   118                     self.debug('warning: cannot write to blackbox.log: %s\n' %
   118                     self.debug('warning: cannot write to blackbox.log: %s\n' %
   119                                err.strerror)
   119                                err.strerror)
   120                     del self._bbvfs
   120                     del self._bbvfs
   121                     self._blackbox = None
   121                     self._bbfp = None
   122                 blackbox = self._blackbox
   122                 fp = self._bbfp
   123             else:
   123             else:
   124                 # certain ui instances exist outside the context of
   124                 # certain ui instances exist outside the context of
   125                 # a repo, so just default to the last blackbox that
   125                 # a repo, so just default to the last blackbox that
   126                 # was seen.
   126                 # was seen.
   127                 blackbox = lastblackbox
   127                 fp = lastfp
   128 
   128 
   129             if blackbox:
   129             if fp:
   130                 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
   130                 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
   131                 user = util.getuser()
   131                 user = util.getuser()
   132                 pid = str(util.getpid())
   132                 pid = str(util.getpid())
   133                 formattedmsg = msg[0] % msg[1:]
   133                 formattedmsg = msg[0] % msg[1:]
   134                 try:
   134                 try:
   135                     blackbox.write('%s %s (%s)> %s' %
   135                     fp.write('%s %s (%s)> %s' %
   136                                    (date, user, pid, formattedmsg))
   136                                    (date, user, pid, formattedmsg))
   137                     blackbox.flush()
   137                     fp.flush()
   138                 except IOError as err:
   138                 except IOError as err:
   139                     self.debug('warning: cannot write to blackbox.log: %s\n' %
   139                     self.debug('warning: cannot write to blackbox.log: %s\n' %
   140                                err.strerror)
   140                                err.strerror)
   141                 lastblackbox = blackbox
   141                 lastfp = fp
   142 
   142 
   143         def setrepo(self, repo):
   143         def setrepo(self, repo):
   144             self._bbvfs = repo.vfs
   144             self._bbvfs = repo.vfs
   145 
   145 
   146     ui.__class__ = blackboxui
   146     ui.__class__ = blackboxui
   168 
   168 
   169     if not repo.vfs.exists('blackbox.log'):
   169     if not repo.vfs.exists('blackbox.log'):
   170         return
   170         return
   171 
   171 
   172     limit = opts.get('limit')
   172     limit = opts.get('limit')
   173     blackbox = repo.vfs('blackbox.log', 'r')
   173     fp = repo.vfs('blackbox.log', 'r')
   174     lines = blackbox.read().split('\n')
   174     lines = fp.read().split('\n')
   175 
   175 
   176     count = 0
   176     count = 0
   177     output = []
   177     output = []
   178     for line in reversed(lines):
   178     for line in reversed(lines):
   179         if count >= limit:
   179         if count >= limit: