changeset 40799:03127e580980

loggingutil: extract openlogfile() and proxylogger to new module This module isn't placed under the "utils" package since it needs "ui" to process things. It's called "loggingutil", not "logutil" because the word "log" is too obscure in our codebase.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 18 Nov 2018 18:25:37 +0900
parents 644adf9c20fb
children 698477777883
files hgext/blackbox.py mercurial/loggingutil.py
diffstat 2 files changed, 64 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/blackbox.py	Sun Nov 18 18:21:39 2018 +0900
+++ b/hgext/blackbox.py	Sun Nov 18 18:25:37 2018 +0900
@@ -42,7 +42,6 @@
 
 from __future__ import absolute_import
 
-import errno
 import re
 
 from mercurial.i18n import _
@@ -50,7 +49,7 @@
 
 from mercurial import (
     encoding,
-    pycompat,
+    loggingutil,
     registrar,
 )
 from mercurial.utils import (
@@ -89,51 +88,7 @@
     default='%Y/%m/%d %H:%M:%S',
 )
 
-def _openlogfile(ui, vfs, name, maxfiles=0, maxsize=0):
-    def rotate(oldpath, newpath):
-        try:
-            vfs.unlink(newpath)
-        except OSError as err:
-            if err.errno != errno.ENOENT:
-                ui.debug("warning: cannot remove '%s': %s\n" %
-                         (newpath, err.strerror))
-        try:
-            if newpath:
-                vfs.rename(oldpath, newpath)
-        except OSError as err:
-            if err.errno != errno.ENOENT:
-                ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
-                         (newpath, oldpath, err.strerror))
-
-    if maxsize > 0:
-        try:
-            st = vfs.stat(name)
-        except OSError:
-            pass
-        else:
-            if st.st_size >= maxsize:
-                path = vfs.join(name)
-                for i in pycompat.xrange(maxfiles - 1, 1, -1):
-                    rotate(oldpath='%s.%d' % (path, i - 1),
-                           newpath='%s.%d' % (path, i))
-                rotate(oldpath=path,
-                       newpath=maxfiles > 0 and path + '.1')
-    return vfs(name, 'a', makeparentdirs=False)
-
-class proxylogger(object):
-    """Forward log events to another logger to be set later"""
-
-    def __init__(self):
-        self.logger = None
-
-    def tracked(self, event):
-        return self.logger is not None and self.logger.tracked(event)
-
-    def log(self, ui, event, msg, opts):
-        assert self.logger is not None
-        self.logger.log(ui, event, msg, opts)
-
-_lastlogger = proxylogger()
+_lastlogger = loggingutil.proxylogger()
 
 class blackboxlogger(object):
     def __init__(self, ui, repo):
@@ -165,9 +120,9 @@
         try:
             fmt = '%s %s @%s%s (%s)%s> %s'
             args = (date, user, rev, changed, pid, src, msg)
-            with _openlogfile(ui, self._repo.vfs, name='blackbox.log',
-                              maxfiles=self._maxfiles,
-                              maxsize=self._maxsize) as fp:
+            with loggingutil.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/loggingutil.py	Sun Nov 18 18:25:37 2018 +0900
@@ -0,0 +1,59 @@
+# loggingutil.py - utility for logging events
+#
+# Copyright 2010 Nicolas Dumazet
+# Copyright 2013 Facebook, Inc.
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from __future__ import absolute_import
+
+import errno
+
+from . import (
+    pycompat,
+)
+
+def openlogfile(ui, vfs, name, maxfiles=0, maxsize=0):
+    def rotate(oldpath, newpath):
+        try:
+            vfs.unlink(newpath)
+        except OSError as err:
+            if err.errno != errno.ENOENT:
+                ui.debug("warning: cannot remove '%s': %s\n" %
+                         (newpath, err.strerror))
+        try:
+            if newpath:
+                vfs.rename(oldpath, newpath)
+        except OSError as err:
+            if err.errno != errno.ENOENT:
+                ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
+                         (newpath, oldpath, err.strerror))
+
+    if maxsize > 0:
+        try:
+            st = vfs.stat(name)
+        except OSError:
+            pass
+        else:
+            if st.st_size >= maxsize:
+                path = vfs.join(name)
+                for i in pycompat.xrange(maxfiles - 1, 1, -1):
+                    rotate(oldpath='%s.%d' % (path, i - 1),
+                           newpath='%s.%d' % (path, i))
+                rotate(oldpath=path,
+                       newpath=maxfiles > 0 and path + '.1')
+    return vfs(name, 'a', makeparentdirs=False)
+
+class proxylogger(object):
+    """Forward log events to another logger to be set later"""
+
+    def __init__(self):
+        self.logger = None
+
+    def tracked(self, event):
+        return self.logger is not None and self.logger.tracked(event)
+
+    def log(self, ui, event, msg, opts):
+        assert self.logger is not None
+        self.logger.log(ui, event, msg, opts)