1
2
3
4
5
6
7
8
9
10
11 """
12 Emulates part of the Python2.3 logging module.
13
14 Note: This will probably not work with Python < 2.2.
15 """
16
17 __revision__ = "$Revision: 913 $"
18
19 import os
20 import sys
21 import time
22 import string
23 try:
24 from cStringIO import StringIO
25 except ImportError:
26 from StringIO import StringIO
27
28 _devlogfile = "/tmp/dev-log.log"
29 _log = None
30
31 -def dump(msg, *args):
43
44
45
46 CRITICAL = 50
47 FATAL = CRITICAL
48 ERROR = 40
49 WARNING = 30
50 WARN = WARNING
51 INFO = 20
52 DEBUG = 10
53 NOTSET = 0
54
55
56 _names = ['FATAL', 'CRITICAL', 'ERROR', 'WARN', 'WARNING', \
57 'INFO', 'DEBUG', 'NOTSET']
58 _levels = {}
59 _level_names = {}
60
62 """
63 Fills in the _levels and _level_names dictionaries.
64 """
65 for n in _names:
66 i = eval(n)
67 _levels[n] = i
68 _level_names[i] = n
69
70 __populate_levels()
71
72
73
74
75 if string.lower(__file__[-4:]) in ['.pyc', '.pyo']:
76 _srcfile = __file__[:-4] + '.py'
77 else:
78 _srcfile = __file__
79 _srcfile = os.path.normcase(_srcfile)
80
81
82 _startTime = time.time()
83
85 """
86 Return the textual representation of logging level 'level'.
87
88 @param level: Numeric representation of a logging level
89 @type level: C{int}
90
91 @return: Texutal representation of the logging level
92 @rtype: C{str}
93 """
94 return _level_names.get(level, ("Level %s" % level))
95
96
97
98 _handlers = {}
99
101 """
102 A stripped down version of the logging.Filterer class.
103 See the logging module for documentation.
104 """
107
109 if not (filter in self.filters):
110 self.filters.append(filter)
111
113 if filter in self.filters:
114 self.filters.remove(filter)
115
117 rv = 1
118 for f in self.filters:
119 if not f.filter(record):
120 rv = 0
121 break
122 return rv
123
125 """
126 A stripped down version of the logging.StreamHandler class.
127 See the logging module for documentation.
128 """
138
141
144
145 - def emit(self, record):
146 try:
147 msg = self.format(record)
148 try:
149 self.stream.write("%s\n" % msg)
150 except UnicodeError:
151 self.stream.write("%s\n" % msg.encode("UTF-8"))
152 self.flush()
153 except:
154 self.handleError(record)
155
157
158 import traceback
159 ei = sys.exc_info()
160 traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr)
161 del ei
162
164 rv = self.filter(record)
165 if rv:
166 self.emit(record)
167 return rv
168
171
174
178
179
181 """
182 A stripped down version of the logging.FileHandler class.
183 See the logging module for documentation.
184 """
185 - def __init__(self, filename, mode="a"):
186 super(FileHandler, self).__init__(open(filename, mode))
187 self.baseFilename = filename
188 self.mode = mode
189
192
193
234
236 """
237 A stripped down version of the logging.LogRecord class.
238 See the logging module for documentation.
239 """
240 - def __init__(self, name, level, pathname, lineno, msg, args, exc_info):
241 ct = time.time()
242 self.name = name
243 self.msg = msg
244 self.args = args
245 self.levelname = getLevelName(level)
246 self.levelno = level
247 self.pathname = pathname
248 try:
249 self.filename = os.path.basename(pathname)
250 self.module = os.path.splitext(self.filename)[0]
251 except:
252 self.filename = pathname
253 self.module = "Unknown module"
254 self.exc_info = exc_info
255 self.lineno = lineno
256 self.created = ct
257 self.msecs = (ct - long(ct)) * 1000
258 self.relativeCreated = (self.created - _startTime) * 1000
259 if hasattr(os, 'getpid'):
260 self.process = os.getpid()
261 else:
262 self.process = None
263
265 return '<LogRecord: %s, %s, %s, %s, "%s">' % \
266 (self.name, self.levelno, self.pathname, self.lineno, self.msg)
267
269 try:
270 msg = str(self.msg)
271 except UnicodeError:
272 msg = self.msg
273 if self.args:
274 msg = msg % self.args
275 return msg
276
277
279 """
280 A stripped down version of the logging.Logger class.
281 See the logging module for documentation.
282 """
289
290 - def _log(self, level, msg, args, exc_info=None):
291
292 if self.level > level:
293 return
294 if _srcfile:
295 fn, lno = self.__findCaller()
296 else:
297 fn, lno = "<unknown file>", 0
298 if exc_info:
299 exc_info = sys.exc_info()
300 record = self.__makeRecord(self.name, level, fn, lno, \
301 msg, args, exc_info)
302 self.__callHandlers(record)
303
305 f = sys._getframe(1)
306 while 1:
307 co = f.f_code
308 filename = os.path.normcase(co.co_filename)
309 if filename == _srcfile:
310 f = f.f_back
311 continue
312 return filename, f.f_lineno
313
314 - def __makeRecord(self, name, level, fn, lno, msg, args, exc_info):
315 return LogRecord(name, level, fn, lno, msg, args, exc_info)
316
318 for hdlr in self.handlers:
319 if record.levelno >= hdlr.level:
320 hdlr.handle(record)
321
322
323
324
327
328 - def critical(self, msg, *args, **kwargs):
330 fatal = critical
331
332 - def error(self, msg, *args, **kwargs):
333 apply(self._log, (ERROR, msg, args), kwargs)
334
335 - def warning(self, msg, *args, **kwargs):
337 warn = warning
338
339 - def info(self, msg, *args, **kwargs):
340 apply(self._log, (INFO, msg, args), kwargs)
341
342 - def debug(self, msg, *args, **kwargs):
343 apply(self._log, (DEBUG, msg, args), kwargs)
344
346 apply(self.error, (msg,) + args, {'exc_info': 1})
347
349 if not (hdlr in self.handlers):
350 self.handlers.append(hdlr)
351
353 if hdlr in self.handlers:
354 self.handlers.remove(hdlr)
355
356
357
358 _logger_registry = {}
359
372
373
385