blackbox: avoid creating multiple file handles for a single log
authortimeless <timeless@mozdev.org>
Wed, 03 Feb 2016 15:41:31 +0000
changeset 28243 45313f5a3a8c
parent 28242 e8ad56d95fbb
child 28244 c17d7b1c40be
blackbox: avoid creating multiple file handles for a single log There are multiple ui objects in Mercurial that can relate to a repository, before this change, each one would have its own file pointer, which results in unfortunate logging behavior. Also, any log rotation results would be bad because only the active blackboxui object's file pointer would be refreshed. Note that this does not prevent two long running hg commands for the same repository from causing problems.
hgext/blackbox.py
--- a/hgext/blackbox.py	Wed Feb 24 20:04:32 2016 +0000
+++ b/hgext/blackbox.py	Wed Feb 03 15:41:31 2016 +0000
@@ -49,6 +49,21 @@
 testedwith = 'internal'
 lastblackbox = None
 
+filehandles = {}
+
+def _openlog(vfs):
+    path = vfs.join('blackbox.log')
+    if path in filehandles:
+        return filehandles[path]
+    filehandles[path] = fp = vfs('blackbox.log', 'a')
+    return fp
+
+def _closelog(vfs):
+    path = vfs.join('blackbox.log')
+    fp = filehandles[path]
+    del filehandles[path]
+    fp.close()
+
 def wrapui(ui):
     class blackboxui(ui.__class__):
         @util.propertycache
@@ -71,20 +86,20 @@
                         self.debug("warning: cannot rename '%s' to '%s': %s\n" %
                                    (newpath, oldpath, err.strerror))
 
-            fp = self._bbvfs('blackbox.log', 'a')
+            fp = _openlog(self._bbvfs)
             maxsize = self.configbytes('blackbox', 'maxsize', 1048576)
             if maxsize > 0:
                 st = self._bbvfs.fstat(fp)
                 if st.st_size >= maxsize:
                     path = fp.name
-                    fp.close()
+                    _closelog(self._bbvfs)
                     maxfiles = self.configint('blackbox', 'maxfiles', 7)
                     for i in 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')
-                    fp = self._bbvfs('blackbox.log', 'a')
+                    fp = _openlog(self._bbvfs)
             return fp
 
         def log(self, event, *msg, **opts):