comparison hgext/blackbox.py @ 28243:45313f5a3a8c

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.
author timeless <timeless@mozdev.org>
date Wed, 03 Feb 2016 15:41:31 +0000
parents 8113c88b8e6d
children c17d7b1c40be
comparison
equal deleted inserted replaced
28242:e8ad56d95fbb 28243:45313f5a3a8c
47 # be specifying the version(s) of Mercurial they are tested with, or 47 # be specifying the version(s) of Mercurial they are tested with, or
48 # leave the attribute unspecified. 48 # leave the attribute unspecified.
49 testedwith = 'internal' 49 testedwith = 'internal'
50 lastblackbox = None 50 lastblackbox = None
51 51
52 filehandles = {}
53
54 def _openlog(vfs):
55 path = vfs.join('blackbox.log')
56 if path in filehandles:
57 return filehandles[path]
58 filehandles[path] = fp = vfs('blackbox.log', 'a')
59 return fp
60
61 def _closelog(vfs):
62 path = vfs.join('blackbox.log')
63 fp = filehandles[path]
64 del filehandles[path]
65 fp.close()
66
52 def wrapui(ui): 67 def wrapui(ui):
53 class blackboxui(ui.__class__): 68 class blackboxui(ui.__class__):
54 @util.propertycache 69 @util.propertycache
55 def track(self): 70 def track(self):
56 return self.configlist('blackbox', 'track', ['*']) 71 return self.configlist('blackbox', 'track', ['*'])
69 except OSError as err: 84 except OSError as err:
70 if err.errno != errno.ENOENT: 85 if err.errno != errno.ENOENT:
71 self.debug("warning: cannot rename '%s' to '%s': %s\n" % 86 self.debug("warning: cannot rename '%s' to '%s': %s\n" %
72 (newpath, oldpath, err.strerror)) 87 (newpath, oldpath, err.strerror))
73 88
74 fp = self._bbvfs('blackbox.log', 'a') 89 fp = _openlog(self._bbvfs)
75 maxsize = self.configbytes('blackbox', 'maxsize', 1048576) 90 maxsize = self.configbytes('blackbox', 'maxsize', 1048576)
76 if maxsize > 0: 91 if maxsize > 0:
77 st = self._bbvfs.fstat(fp) 92 st = self._bbvfs.fstat(fp)
78 if st.st_size >= maxsize: 93 if st.st_size >= maxsize:
79 path = fp.name 94 path = fp.name
80 fp.close() 95 _closelog(self._bbvfs)
81 maxfiles = self.configint('blackbox', 'maxfiles', 7) 96 maxfiles = self.configint('blackbox', 'maxfiles', 7)
82 for i in xrange(maxfiles - 1, 1, -1): 97 for i in xrange(maxfiles - 1, 1, -1):
83 rotate(oldpath='%s.%d' % (path, i - 1), 98 rotate(oldpath='%s.%d' % (path, i - 1),
84 newpath='%s.%d' % (path, i)) 99 newpath='%s.%d' % (path, i))
85 rotate(oldpath=path, 100 rotate(oldpath=path,
86 newpath=maxfiles > 0 and path + '.1') 101 newpath=maxfiles > 0 and path + '.1')
87 fp = self._bbvfs('blackbox.log', 'a') 102 fp = _openlog(self._bbvfs)
88 return fp 103 return fp
89 104
90 def log(self, event, *msg, **opts): 105 def log(self, event, *msg, **opts):
91 global lastblackbox 106 global lastblackbox
92 super(blackboxui, self).log(event, *msg, **opts) 107 super(blackboxui, self).log(event, *msg, **opts)