##// END OF EJS Templates
loggingutil: document openlogfile()...
Yuya Nishihara -
r40831:69847777 default
parent child Browse files
Show More
@@ -1,59 +1,63 b''
1 # loggingutil.py - utility for logging events
1 # loggingutil.py - utility for logging events
2 #
2 #
3 # Copyright 2010 Nicolas Dumazet
3 # Copyright 2010 Nicolas Dumazet
4 # Copyright 2013 Facebook, Inc.
4 # Copyright 2013 Facebook, Inc.
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8
8
9 from __future__ import absolute_import
9 from __future__ import absolute_import
10
10
11 import errno
11 import errno
12
12
13 from . import (
13 from . import (
14 pycompat,
14 pycompat,
15 )
15 )
16
16
17 def openlogfile(ui, vfs, name, maxfiles=0, maxsize=0):
17 def openlogfile(ui, vfs, name, maxfiles=0, maxsize=0):
18 """Open log file in append mode, with optional rotation
19
20 If maxsize > 0, the log file will be rotated up to maxfiles.
21 """
18 def rotate(oldpath, newpath):
22 def rotate(oldpath, newpath):
19 try:
23 try:
20 vfs.unlink(newpath)
24 vfs.unlink(newpath)
21 except OSError as err:
25 except OSError as err:
22 if err.errno != errno.ENOENT:
26 if err.errno != errno.ENOENT:
23 ui.debug("warning: cannot remove '%s': %s\n" %
27 ui.debug("warning: cannot remove '%s': %s\n" %
24 (newpath, err.strerror))
28 (newpath, err.strerror))
25 try:
29 try:
26 if newpath:
30 if newpath:
27 vfs.rename(oldpath, newpath)
31 vfs.rename(oldpath, newpath)
28 except OSError as err:
32 except OSError as err:
29 if err.errno != errno.ENOENT:
33 if err.errno != errno.ENOENT:
30 ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
34 ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
31 (newpath, oldpath, err.strerror))
35 (newpath, oldpath, err.strerror))
32
36
33 if maxsize > 0:
37 if maxsize > 0:
34 try:
38 try:
35 st = vfs.stat(name)
39 st = vfs.stat(name)
36 except OSError:
40 except OSError:
37 pass
41 pass
38 else:
42 else:
39 if st.st_size >= maxsize:
43 if st.st_size >= maxsize:
40 path = vfs.join(name)
44 path = vfs.join(name)
41 for i in pycompat.xrange(maxfiles - 1, 1, -1):
45 for i in pycompat.xrange(maxfiles - 1, 1, -1):
42 rotate(oldpath='%s.%d' % (path, i - 1),
46 rotate(oldpath='%s.%d' % (path, i - 1),
43 newpath='%s.%d' % (path, i))
47 newpath='%s.%d' % (path, i))
44 rotate(oldpath=path,
48 rotate(oldpath=path,
45 newpath=maxfiles > 0 and path + '.1')
49 newpath=maxfiles > 0 and path + '.1')
46 return vfs(name, 'a', makeparentdirs=False)
50 return vfs(name, 'a', makeparentdirs=False)
47
51
48 class proxylogger(object):
52 class proxylogger(object):
49 """Forward log events to another logger to be set later"""
53 """Forward log events to another logger to be set later"""
50
54
51 def __init__(self):
55 def __init__(self):
52 self.logger = None
56 self.logger = None
53
57
54 def tracked(self, event):
58 def tracked(self, event):
55 return self.logger is not None and self.logger.tracked(event)
59 return self.logger is not None and self.logger.tracked(event)
56
60
57 def log(self, ui, event, msg, opts):
61 def log(self, ui, event, msg, opts):
58 assert self.logger is not None
62 assert self.logger is not None
59 self.logger.log(ui, event, msg, opts)
63 self.logger.log(ui, event, msg, opts)
General Comments 0
You need to be logged in to leave comments. Login now