blackbox: move _openlogfile to a separate method

This removes several `stat` syscalls for accessing `_bbvfs` and makes `ui`
object cleaner.

Differential Revision: https://phab.mercurial-scm.org/D769
This commit is contained in:
Jun Wu 2017-09-21 11:10:20 -07:00
parent a18c4e0d85
commit c55a3b0058

View File

@ -73,6 +73,40 @@ configitem('blackbox', 'logsource',
lastui = None
def _openlogfile(ui, vfs):
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))
maxsize = ui.configbytes('blackbox', 'maxsize')
name = 'blackbox.log'
if maxsize > 0:
try:
st = vfs.stat(name)
except OSError:
pass
else:
if st.st_size >= maxsize:
path = vfs.join(name)
maxfiles = ui.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')
return vfs(name, 'a')
def wrapui(ui):
class blackboxui(ui.__class__):
@property
@ -89,40 +123,6 @@ def wrapui(ui):
def track(self):
return self.configlist('blackbox', 'track', ['*'])
def _openlogfile(self):
def rotate(oldpath, newpath):
try:
self._bbvfs.unlink(newpath)
except OSError as err:
if err.errno != errno.ENOENT:
self.debug("warning: cannot remove '%s': %s\n" %
(newpath, err.strerror))
try:
if newpath:
self._bbvfs.rename(oldpath, newpath)
except OSError as err:
if err.errno != errno.ENOENT:
self.debug("warning: cannot rename '%s' to '%s': %s\n" %
(newpath, oldpath, err.strerror))
maxsize = self.configbytes('blackbox', 'maxsize')
name = 'blackbox.log'
if maxsize > 0:
try:
st = self._bbvfs.stat(name)
except OSError:
pass
else:
if st.st_size >= maxsize:
path = self._bbvfs.join(name)
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')
return self._bbvfs(name, 'a')
def log(self, event, *msg, **opts):
global lastui
super(blackboxui, self).log(event, *msg, **opts)
@ -172,7 +172,7 @@ def wrapui(ui):
try:
fmt = '%s %s @%s%s (%s)%s> %s'
args = (date, user, rev, changed, pid, src, formattedmsg)
with ui._openlogfile() as fp:
with _openlogfile(ui, vfs) as fp:
fp.write(fmt % args)
except (IOError, OSError) as err:
self.debug('warning: cannot write to blackbox.log: %s\n' %