407
|
1 |
#!/bin/env python
|
494
|
2 |
#copyright ReportLab Inc. 2000
|
|
3 |
#see license.txt for license details
|
|
4 |
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/lib/logger.py?cvsroot=reportlab
|
|
5 |
#$Header: /tmp/reportlab/reportlab/lib/logger.py,v 1.2 2000/10/25 08:57:45 rgbecker Exp $
|
|
6 |
__version__=''' $Id: logger.py,v 1.2 2000/10/25 08:57:45 rgbecker Exp $ '''
|
407
|
7 |
|
|
8 |
from sys import stderr
|
|
9 |
class Logger:
|
|
10 |
'''
|
|
11 |
An extended file type thing initially equivalent to sys.stderr
|
|
12 |
You can add/remove file type things; it has a write method
|
|
13 |
'''
|
|
14 |
def __init__(self):
|
|
15 |
self._fps = [stderr]
|
|
16 |
self._fns = {}
|
|
17 |
|
|
18 |
def add(self,fp):
|
|
19 |
'''add the file/string fp to the destinations'''
|
|
20 |
if type(fp) is StringType:
|
|
21 |
if fp in self._fns: return
|
|
22 |
fp = open(fn,'wb')
|
|
23 |
self._fns[fn] = fp
|
|
24 |
self._fps.append(fp)
|
|
25 |
|
|
26 |
def remove(self,fp):
|
|
27 |
'''remove the file/string fp from the destinations'''
|
|
28 |
if type(fp) is StringType:
|
|
29 |
if fp not in self._fns: return
|
|
30 |
fn = fp
|
|
31 |
fp = self._fns[fn]
|
|
32 |
del self.fns[fn]
|
|
33 |
if fp in self._fps:
|
|
34 |
del self._fps[self._fps.index(fp)]
|
|
35 |
|
|
36 |
def write(self,text):
|
|
37 |
'''write text to all the destinations'''
|
|
38 |
if text[-1]!='\n': text=text+'\n'
|
|
39 |
map(lambda fp,t=text: fp.write(t),self._fps)
|
|
40 |
|
|
41 |
def __call__(self,text):
|
|
42 |
self.write(text)
|
|
43 |
|
|
44 |
logger=Logger()
|
|
45 |
|
|
46 |
class WarnOnce:
|
|
47 |
|
|
48 |
def __init__(self,kind='Warn'):
|
|
49 |
self.uttered = {}
|
|
50 |
self.pfx = '%s: '%kind
|
|
51 |
|
|
52 |
def once(self,warning):
|
|
53 |
if not self.uttered.has_key(warning):
|
|
54 |
logger.write(self.pfx + warning)
|
|
55 |
self.uttered[warning] = 1
|
|
56 |
|
|
57 |
def __call__(self,warning):
|
|
58 |
self.once(warning)
|
|
59 |
|
|
60 |
warnOnce=WarnOnce()
|
|
61 |
infoOnce=WarnOnce('Info')
|