##// END OF EJS Templates
blackbox: automatically rotate log files...
Bryan O'Sullivan -
r19066:2cad301a default
parent child Browse files
Show More
@@ -21,11 +21,17 b' Examples:'
21 [blackbox]
21 [blackbox]
22 track = incoming
22 track = incoming
23
23
24 [blackbox]
25 # limit the size of a log file
26 maxsize = 1.5 MB
27 # rotate up to N log files when the current one gets too big
28 maxfiles = 3
29
24 """
30 """
25
31
26 from mercurial import util, cmdutil
32 from mercurial import util, cmdutil
27 from mercurial.i18n import _
33 from mercurial.i18n import _
28 import os, re
34 import errno, os, re
29
35
30 cmdtable = {}
36 cmdtable = {}
31 command = cmdutil.command(cmdtable)
37 command = cmdutil.command(cmdtable)
@@ -38,6 +44,38 b' def wrapui(ui):'
38 def track(self):
44 def track(self):
39 return self.configlist('blackbox', 'track', ['*'])
45 return self.configlist('blackbox', 'track', ['*'])
40
46
47 def _openlogfile(self):
48 def rotate(oldpath, newpath):
49 try:
50 os.unlink(newpath)
51 except OSError, err:
52 if err.errno != errno.ENOENT:
53 self.debug("warning: cannot remove '%s': %s\n" %
54 (newpath, err.strerror))
55 try:
56 if newpath:
57 os.rename(oldpath, newpath)
58 except OSError, err:
59 if err.errno != errno.ENOENT:
60 self.debug("warning: cannot rename '%s' to '%s': %s\n" %
61 (newpath, oldpath, err.strerror))
62
63 fp = self._bbopener('blackbox.log', 'a')
64 maxsize = self.configbytes('blackbox', 'maxsize', 1048576)
65 if maxsize > 0:
66 st = os.fstat(fp.fileno())
67 if st.st_size >= maxsize:
68 path = fp.name
69 fp.close()
70 maxfiles = self.configint('blackbox', 'maxfiles', 7)
71 for i in xrange(maxfiles - 1, 1, -1):
72 rotate(oldpath='%s.%d' % (path, i - 1),
73 newpath='%s.%d' % (path, i))
74 rotate(oldpath=path,
75 newpath=maxfiles > 0 and path + '.1')
76 fp = self._bbopener('blackbox.log', 'a')
77 return fp
78
41 def log(self, event, *msg, **opts):
79 def log(self, event, *msg, **opts):
42 global lastblackbox
80 global lastblackbox
43 super(blackboxui, self).log(event, *msg, **opts)
81 super(blackboxui, self).log(event, *msg, **opts)
@@ -49,7 +87,7 b' def wrapui(ui):'
49 blackbox = self._blackbox
87 blackbox = self._blackbox
50 elif util.safehasattr(self, '_bbopener'):
88 elif util.safehasattr(self, '_bbopener'):
51 try:
89 try:
52 self._blackbox = self._bbopener('blackbox.log', 'a')
90 self._blackbox = self._openlogfile()
53 except (IOError, OSError), err:
91 except (IOError, OSError), err:
54 self.debug('warning: cannot write to blackbox.log: %s\n' %
92 self.debug('warning: cannot write to blackbox.log: %s\n' %
55 err.strerror)
93 err.strerror)
@@ -131,5 +131,20 b' extension and python hooks - use the eol'
131 1970/01/01 00:00:00 bob> exthook-update: echo hooked finished in * seconds (glob)
131 1970/01/01 00:00:00 bob> exthook-update: echo hooked finished in * seconds (glob)
132 1970/01/01 00:00:00 bob> update exited False after * seconds (glob)
132 1970/01/01 00:00:00 bob> update exited False after * seconds (glob)
133
133
134 log rotation
135
136 $ echo '[blackbox]' >> .hg/hgrc
137 $ echo 'maxsize = 20 b' >> .hg/hgrc
138 $ echo 'maxfiles = 3' >> .hg/hgrc
139 $ hg status
140 $ hg status
141 $ hg status
142 $ hg tip -q
143 2:d02f48003e62
144 $ ls .hg/blackbox.log*
145 .hg/blackbox.log
146 .hg/blackbox.log.1
147 .hg/blackbox.log.2
148
134 cleanup
149 cleanup
135 $ cd ..
150 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now