changeset 40798:644adf9c20fb

blackbox: pass in options to _openlogfile() as arguments This prepares for extracting utility function from the blackbox module.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 18 Nov 2018 18:21:39 +0900
parents ea2688c84e4b
children 03127e580980
files hgext/blackbox.py
diffstat 1 files changed, 6 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/blackbox.py	Sat Nov 17 22:10:27 2018 +0900
+++ b/hgext/blackbox.py	Sun Nov 18 18:21:39 2018 +0900
@@ -89,7 +89,7 @@
     default='%Y/%m/%d %H:%M:%S',
 )
 
-def _openlogfile(ui, vfs):
+def _openlogfile(ui, vfs, name, maxfiles=0, maxsize=0):
     def rotate(oldpath, newpath):
         try:
             vfs.unlink(newpath)
@@ -105,8 +105,6 @@
                 ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
                          (newpath, oldpath, err.strerror))
 
-    maxsize = ui.configbytes('blackbox', 'maxsize')
-    name = 'blackbox.log'
     if maxsize > 0:
         try:
             st = vfs.stat(name)
@@ -115,7 +113,6 @@
         else:
             if st.st_size >= maxsize:
                 path = vfs.join(name)
-                maxfiles = ui.configint('blackbox', 'maxfiles')
                 for i in pycompat.xrange(maxfiles - 1, 1, -1):
                     rotate(oldpath='%s.%d' % (path, i - 1),
                            newpath='%s.%d' % (path, i))
@@ -142,6 +139,8 @@
     def __init__(self, ui, repo):
         self._repo = repo
         self._trackedevents = set(ui.configlist('blackbox', 'track'))
+        self._maxfiles = ui.configint('blackbox', 'maxfiles')
+        self._maxsize = ui.configbytes('blackbox', 'maxsize')
 
     def tracked(self, event):
         return b'*' in self._trackedevents or event in self._trackedevents
@@ -166,7 +165,9 @@
         try:
             fmt = '%s %s @%s%s (%s)%s> %s'
             args = (date, user, rev, changed, pid, src, msg)
-            with _openlogfile(ui, self._repo.vfs) as fp:
+            with _openlogfile(ui, self._repo.vfs, name='blackbox.log',
+                              maxfiles=self._maxfiles,
+                              maxsize=self._maxsize) as fp:
                 fp.write(fmt % args)
         except (IOError, OSError) as err:
             # deactivate this to avoid failed logging again