Patchwork [3,of,5] blackbox: pass in options to _openlogfile() as arguments

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 30, 2018, 1:41 p.m.
Message ID <dba8c880874d32c4160c.1543585286@mimosa>
Download mbox | patch
Permalink /patch/36880/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 30, 2018, 1:41 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1542532899 -32400
#      Sun Nov 18 18:21:39 2018 +0900
# Node ID dba8c880874d32c4160c73feb5f84d2c66c58013
# Parent  c299fe88974e640b83192dcedabb9c4e599d7ed3
blackbox: pass in options to _openlogfile() as arguments

This prepares for extracting utility function from the blackbox module.

Patch

diff --git a/hgext/blackbox.py b/hgext/blackbox.py
--- a/hgext/blackbox.py
+++ b/hgext/blackbox.py
@@ -89,7 +89,7 @@  configitem('blackbox', 'date-format',
     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 @@  def _openlogfile(ui, vfs):
                 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 @@  def _openlogfile(ui, vfs):
         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 @@  class blackboxlogger(object):
     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 @@  class blackboxlogger(object):
         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