mirror of
https://github.com/facebook/sapling.git
synced 2024-10-08 07:49:11 +03:00
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:
parent
a18c4e0d85
commit
c55a3b0058
@ -73,6 +73,40 @@ configitem('blackbox', 'logsource',
|
|||||||
|
|
||||||
lastui = None
|
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):
|
def wrapui(ui):
|
||||||
class blackboxui(ui.__class__):
|
class blackboxui(ui.__class__):
|
||||||
@property
|
@property
|
||||||
@ -89,40 +123,6 @@ def wrapui(ui):
|
|||||||
def track(self):
|
def track(self):
|
||||||
return self.configlist('blackbox', 'track', ['*'])
|
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):
|
def log(self, event, *msg, **opts):
|
||||||
global lastui
|
global lastui
|
||||||
super(blackboxui, self).log(event, *msg, **opts)
|
super(blackboxui, self).log(event, *msg, **opts)
|
||||||
@ -172,7 +172,7 @@ def wrapui(ui):
|
|||||||
try:
|
try:
|
||||||
fmt = '%s %s @%s%s (%s)%s> %s'
|
fmt = '%s %s @%s%s (%s)%s> %s'
|
||||||
args = (date, user, rev, changed, pid, src, formattedmsg)
|
args = (date, user, rev, changed, pid, src, formattedmsg)
|
||||||
with ui._openlogfile() as fp:
|
with _openlogfile(ui, vfs) as fp:
|
||||||
fp.write(fmt % args)
|
fp.write(fmt % args)
|
||||||
except (IOError, OSError) as err:
|
except (IOError, OSError) as err:
|
||||||
self.debug('warning: cannot write to blackbox.log: %s\n' %
|
self.debug('warning: cannot write to blackbox.log: %s\n' %
|
||||||
|
Loading…
Reference in New Issue
Block a user