Mercurial > hg
comparison hgext/blackbox.py @ 40799:03127e580980
loggingutil: extract openlogfile() and proxylogger to new module
This module isn't placed under the "utils" package since it needs "ui" to
process things. It's called "loggingutil", not "logutil" because the word
"log" is too obscure in our codebase.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 18 Nov 2018 18:25:37 +0900 |
parents | 644adf9c20fb |
children | 49d48489a16b |
comparison
equal
deleted
inserted
replaced
40798:644adf9c20fb | 40799:03127e580980 |
---|---|
40 | 40 |
41 """ | 41 """ |
42 | 42 |
43 from __future__ import absolute_import | 43 from __future__ import absolute_import |
44 | 44 |
45 import errno | |
46 import re | 45 import re |
47 | 46 |
48 from mercurial.i18n import _ | 47 from mercurial.i18n import _ |
49 from mercurial.node import hex | 48 from mercurial.node import hex |
50 | 49 |
51 from mercurial import ( | 50 from mercurial import ( |
52 encoding, | 51 encoding, |
53 pycompat, | 52 loggingutil, |
54 registrar, | 53 registrar, |
55 ) | 54 ) |
56 from mercurial.utils import ( | 55 from mercurial.utils import ( |
57 dateutil, | 56 dateutil, |
58 procutil, | 57 procutil, |
87 ) | 86 ) |
88 configitem('blackbox', 'date-format', | 87 configitem('blackbox', 'date-format', |
89 default='%Y/%m/%d %H:%M:%S', | 88 default='%Y/%m/%d %H:%M:%S', |
90 ) | 89 ) |
91 | 90 |
92 def _openlogfile(ui, vfs, name, maxfiles=0, maxsize=0): | 91 _lastlogger = loggingutil.proxylogger() |
93 def rotate(oldpath, newpath): | |
94 try: | |
95 vfs.unlink(newpath) | |
96 except OSError as err: | |
97 if err.errno != errno.ENOENT: | |
98 ui.debug("warning: cannot remove '%s': %s\n" % | |
99 (newpath, err.strerror)) | |
100 try: | |
101 if newpath: | |
102 vfs.rename(oldpath, newpath) | |
103 except OSError as err: | |
104 if err.errno != errno.ENOENT: | |
105 ui.debug("warning: cannot rename '%s' to '%s': %s\n" % | |
106 (newpath, oldpath, err.strerror)) | |
107 | |
108 if maxsize > 0: | |
109 try: | |
110 st = vfs.stat(name) | |
111 except OSError: | |
112 pass | |
113 else: | |
114 if st.st_size >= maxsize: | |
115 path = vfs.join(name) | |
116 for i in pycompat.xrange(maxfiles - 1, 1, -1): | |
117 rotate(oldpath='%s.%d' % (path, i - 1), | |
118 newpath='%s.%d' % (path, i)) | |
119 rotate(oldpath=path, | |
120 newpath=maxfiles > 0 and path + '.1') | |
121 return vfs(name, 'a', makeparentdirs=False) | |
122 | |
123 class proxylogger(object): | |
124 """Forward log events to another logger to be set later""" | |
125 | |
126 def __init__(self): | |
127 self.logger = None | |
128 | |
129 def tracked(self, event): | |
130 return self.logger is not None and self.logger.tracked(event) | |
131 | |
132 def log(self, ui, event, msg, opts): | |
133 assert self.logger is not None | |
134 self.logger.log(ui, event, msg, opts) | |
135 | |
136 _lastlogger = proxylogger() | |
137 | 92 |
138 class blackboxlogger(object): | 93 class blackboxlogger(object): |
139 def __init__(self, ui, repo): | 94 def __init__(self, ui, repo): |
140 self._repo = repo | 95 self._repo = repo |
141 self._trackedevents = set(ui.configlist('blackbox', 'track')) | 96 self._trackedevents = set(ui.configlist('blackbox', 'track')) |
163 else: | 118 else: |
164 src = '' | 119 src = '' |
165 try: | 120 try: |
166 fmt = '%s %s @%s%s (%s)%s> %s' | 121 fmt = '%s %s @%s%s (%s)%s> %s' |
167 args = (date, user, rev, changed, pid, src, msg) | 122 args = (date, user, rev, changed, pid, src, msg) |
168 with _openlogfile(ui, self._repo.vfs, name='blackbox.log', | 123 with loggingutil.openlogfile( |
169 maxfiles=self._maxfiles, | 124 ui, self._repo.vfs, name='blackbox.log', |
170 maxsize=self._maxsize) as fp: | 125 maxfiles=self._maxfiles, maxsize=self._maxsize) as fp: |
171 fp.write(fmt % args) | 126 fp.write(fmt % args) |
172 except (IOError, OSError) as err: | 127 except (IOError, OSError) as err: |
173 # deactivate this to avoid failed logging again | 128 # deactivate this to avoid failed logging again |
174 self._trackedevents.clear() | 129 self._trackedevents.clear() |
175 ui.debug('warning: cannot write to blackbox.log: %s\n' % | 130 ui.debug('warning: cannot write to blackbox.log: %s\n' % |