hgext/blackbox.py
changeset 40798 644adf9c20fb
parent 40797 ea2688c84e4b
child 40799 03127e580980
equal deleted inserted replaced
40797:ea2688c84e4b 40798:644adf9c20fb
    87 )
    87 )
    88 configitem('blackbox', 'date-format',
    88 configitem('blackbox', 'date-format',
    89     default='%Y/%m/%d %H:%M:%S',
    89     default='%Y/%m/%d %H:%M:%S',
    90 )
    90 )
    91 
    91 
    92 def _openlogfile(ui, vfs):
    92 def _openlogfile(ui, vfs, name, maxfiles=0, maxsize=0):
    93     def rotate(oldpath, newpath):
    93     def rotate(oldpath, newpath):
    94         try:
    94         try:
    95             vfs.unlink(newpath)
    95             vfs.unlink(newpath)
    96         except OSError as err:
    96         except OSError as err:
    97             if err.errno != errno.ENOENT:
    97             if err.errno != errno.ENOENT:
   103         except OSError as err:
   103         except OSError as err:
   104             if err.errno != errno.ENOENT:
   104             if err.errno != errno.ENOENT:
   105                 ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
   105                 ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
   106                          (newpath, oldpath, err.strerror))
   106                          (newpath, oldpath, err.strerror))
   107 
   107 
   108     maxsize = ui.configbytes('blackbox', 'maxsize')
       
   109     name = 'blackbox.log'
       
   110     if maxsize > 0:
   108     if maxsize > 0:
   111         try:
   109         try:
   112             st = vfs.stat(name)
   110             st = vfs.stat(name)
   113         except OSError:
   111         except OSError:
   114             pass
   112             pass
   115         else:
   113         else:
   116             if st.st_size >= maxsize:
   114             if st.st_size >= maxsize:
   117                 path = vfs.join(name)
   115                 path = vfs.join(name)
   118                 maxfiles = ui.configint('blackbox', 'maxfiles')
       
   119                 for i in pycompat.xrange(maxfiles - 1, 1, -1):
   116                 for i in pycompat.xrange(maxfiles - 1, 1, -1):
   120                     rotate(oldpath='%s.%d' % (path, i - 1),
   117                     rotate(oldpath='%s.%d' % (path, i - 1),
   121                            newpath='%s.%d' % (path, i))
   118                            newpath='%s.%d' % (path, i))
   122                 rotate(oldpath=path,
   119                 rotate(oldpath=path,
   123                        newpath=maxfiles > 0 and path + '.1')
   120                        newpath=maxfiles > 0 and path + '.1')
   140 
   137 
   141 class blackboxlogger(object):
   138 class blackboxlogger(object):
   142     def __init__(self, ui, repo):
   139     def __init__(self, ui, repo):
   143         self._repo = repo
   140         self._repo = repo
   144         self._trackedevents = set(ui.configlist('blackbox', 'track'))
   141         self._trackedevents = set(ui.configlist('blackbox', 'track'))
       
   142         self._maxfiles = ui.configint('blackbox', 'maxfiles')
       
   143         self._maxsize = ui.configbytes('blackbox', 'maxsize')
   145 
   144 
   146     def tracked(self, event):
   145     def tracked(self, event):
   147         return b'*' in self._trackedevents or event in self._trackedevents
   146         return b'*' in self._trackedevents or event in self._trackedevents
   148 
   147 
   149     def log(self, ui, event, msg, opts):
   148     def log(self, ui, event, msg, opts):
   164         else:
   163         else:
   165             src = ''
   164             src = ''
   166         try:
   165         try:
   167             fmt = '%s %s @%s%s (%s)%s> %s'
   166             fmt = '%s %s @%s%s (%s)%s> %s'
   168             args = (date, user, rev, changed, pid, src, msg)
   167             args = (date, user, rev, changed, pid, src, msg)
   169             with _openlogfile(ui, self._repo.vfs) as fp:
   168             with _openlogfile(ui, self._repo.vfs, name='blackbox.log',
       
   169                               maxfiles=self._maxfiles,
       
   170                               maxsize=self._maxsize) as fp:
   170                 fp.write(fmt % args)
   171                 fp.write(fmt % args)
   171         except (IOError, OSError) as err:
   172         except (IOError, OSError) as err:
   172             # deactivate this to avoid failed logging again
   173             # deactivate this to avoid failed logging again
   173             self._trackedevents.clear()
   174             self._trackedevents.clear()
   174             ui.debug('warning: cannot write to blackbox.log: %s\n' %
   175             ui.debug('warning: cannot write to blackbox.log: %s\n' %