hgext/blackbox.py
changeset 34300 e6723c939344
parent 34299 b1d4ac068961
child 34517 49b72b6f6d66
equal deleted inserted replaced
34299:b1d4ac068961 34300:e6723c939344
    70 configitem('blackbox', 'logsource',
    70 configitem('blackbox', 'logsource',
    71     default=False,
    71     default=False,
    72 )
    72 )
    73 
    73 
    74 lastui = None
    74 lastui = None
       
    75 
       
    76 def _openlogfile(ui, vfs):
       
    77     def rotate(oldpath, newpath):
       
    78         try:
       
    79             vfs.unlink(newpath)
       
    80         except OSError as err:
       
    81             if err.errno != errno.ENOENT:
       
    82                 ui.debug("warning: cannot remove '%s': %s\n" %
       
    83                          (newpath, err.strerror))
       
    84         try:
       
    85             if newpath:
       
    86                 vfs.rename(oldpath, newpath)
       
    87         except OSError as err:
       
    88             if err.errno != errno.ENOENT:
       
    89                 ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
       
    90                          (newpath, oldpath, err.strerror))
       
    91 
       
    92     maxsize = ui.configbytes('blackbox', 'maxsize')
       
    93     name = 'blackbox.log'
       
    94     if maxsize > 0:
       
    95         try:
       
    96             st = vfs.stat(name)
       
    97         except OSError:
       
    98             pass
       
    99         else:
       
   100             if st.st_size >= maxsize:
       
   101                 path = vfs.join(name)
       
   102                 maxfiles = ui.configint('blackbox', 'maxfiles', 7)
       
   103                 for i in xrange(maxfiles - 1, 1, -1):
       
   104                     rotate(oldpath='%s.%d' % (path, i - 1),
       
   105                            newpath='%s.%d' % (path, i))
       
   106                 rotate(oldpath=path,
       
   107                        newpath=maxfiles > 0 and path + '.1')
       
   108     return vfs(name, 'a')
    75 
   109 
    76 def wrapui(ui):
   110 def wrapui(ui):
    77     class blackboxui(ui.__class__):
   111     class blackboxui(ui.__class__):
    78         @property
   112         @property
    79         def _bbvfs(self):
   113         def _bbvfs(self):
    86             return vfs
   120             return vfs
    87 
   121 
    88         @util.propertycache
   122         @util.propertycache
    89         def track(self):
   123         def track(self):
    90             return self.configlist('blackbox', 'track', ['*'])
   124             return self.configlist('blackbox', 'track', ['*'])
    91 
       
    92         def _openlogfile(self):
       
    93             def rotate(oldpath, newpath):
       
    94                 try:
       
    95                     self._bbvfs.unlink(newpath)
       
    96                 except OSError as err:
       
    97                     if err.errno != errno.ENOENT:
       
    98                         self.debug("warning: cannot remove '%s': %s\n" %
       
    99                                    (newpath, err.strerror))
       
   100                 try:
       
   101                     if newpath:
       
   102                         self._bbvfs.rename(oldpath, newpath)
       
   103                 except OSError as err:
       
   104                     if err.errno != errno.ENOENT:
       
   105                         self.debug("warning: cannot rename '%s' to '%s': %s\n" %
       
   106                                    (newpath, oldpath, err.strerror))
       
   107 
       
   108             maxsize = self.configbytes('blackbox', 'maxsize')
       
   109             name = 'blackbox.log'
       
   110             if maxsize > 0:
       
   111                 try:
       
   112                     st = self._bbvfs.stat(name)
       
   113                 except OSError:
       
   114                     pass
       
   115                 else:
       
   116                     if st.st_size >= maxsize:
       
   117                         path = self._bbvfs.join(name)
       
   118                         maxfiles = self.configint('blackbox', 'maxfiles', 7)
       
   119                         for i in xrange(maxfiles - 1, 1, -1):
       
   120                             rotate(oldpath='%s.%d' % (path, i - 1),
       
   121                                    newpath='%s.%d' % (path, i))
       
   122                         rotate(oldpath=path,
       
   123                                newpath=maxfiles > 0 and path + '.1')
       
   124             return self._bbvfs(name, 'a')
       
   125 
   125 
   126         def log(self, event, *msg, **opts):
   126         def log(self, event, *msg, **opts):
   127             global lastui
   127             global lastui
   128             super(blackboxui, self).log(event, *msg, **opts)
   128             super(blackboxui, self).log(event, *msg, **opts)
   129 
   129 
   170             else:
   170             else:
   171                 src = ''
   171                 src = ''
   172             try:
   172             try:
   173                 fmt = '%s %s @%s%s (%s)%s> %s'
   173                 fmt = '%s %s @%s%s (%s)%s> %s'
   174                 args = (date, user, rev, changed, pid, src, formattedmsg)
   174                 args = (date, user, rev, changed, pid, src, formattedmsg)
   175                 with ui._openlogfile() as fp:
   175                 with _openlogfile(ui, vfs) as fp:
   176                     fp.write(fmt % args)
   176                     fp.write(fmt % args)
   177             except (IOError, OSError) as err:
   177             except (IOError, OSError) as err:
   178                 self.debug('warning: cannot write to blackbox.log: %s\n' %
   178                 self.debug('warning: cannot write to blackbox.log: %s\n' %
   179                            err.strerror)
   179                            err.strerror)
   180                 # do not restore _bbinlog intentionally to avoid failed
   180                 # do not restore _bbinlog intentionally to avoid failed