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