src/reportlab/lib/logger.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4370 823a8c33ce43
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
     1
#!/bin/env python
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2017
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 407
diff changeset
     3
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
     4
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/logger.py
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3800
diff changeset
     5
__version__='3.3.0'
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     6
__doc__="Logging and warning framework, predating Python's logging package"
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
     7
from sys import stderr
8c089804207e Initial version
rgbecker
parents:
diff changeset
     8
class Logger:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
     9
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    10
    An extended file type thing initially equivalent to sys.stderr
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    11
    You can add/remove file type things; it has a write method
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    12
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    13
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    14
        self._fps = [stderr]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    15
        self._fns = {}
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
    16
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    17
    def add(self,fp):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    18
        '''add the file/string fp to the destinations'''
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3617
diff changeset
    19
        if isinstance(fp,str):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    20
            if fp in self._fns: return
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    21
            fp = open(fn,'wb')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    22
            self._fns[fn] = fp
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    23
        self._fps.append(fp)
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
    24
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    25
    def remove(self,fp):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    26
        '''remove the file/string fp from the destinations'''
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3617
diff changeset
    27
        if isinstance(fp,str):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    28
            if fp not in self._fns: return
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    29
            fn = fp
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    30
            fp = self._fns[fn]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    31
            del self.fns[fn]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    32
        if fp in self._fps:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    33
            del self._fps[self._fps.index(fp)]
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
    34
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    35
    def write(self,text):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    36
        '''write text to all the destinations'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    37
        if text[-1]!='\n': text=text+'\n'
3328
4d7f836cd947 reportlab: fix 2to3 warnings
rgbecker
parents: 3326
diff changeset
    38
        for fp in self._fps: fp.write(text)
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
    39
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    40
    def __call__(self,text):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    41
        self.write(text)
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
    42
8c089804207e Initial version
rgbecker
parents:
diff changeset
    43
logger=Logger()
8c089804207e Initial version
rgbecker
parents:
diff changeset
    44
8c089804207e Initial version
rgbecker
parents:
diff changeset
    45
class WarnOnce:
8c089804207e Initial version
rgbecker
parents:
diff changeset
    46
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    47
    def __init__(self,kind='Warn'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    48
        self.uttered = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    49
        self.pfx = '%s: '%kind
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    50
        self.enabled = 1
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
    51
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    52
    def once(self,warning):
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3029
diff changeset
    53
        if warning not in self.uttered:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    54
            if self.enabled: logger.write(self.pfx + warning)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    55
            self.uttered[warning] = 1
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
    56
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    57
    def __call__(self,warning):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 811
diff changeset
    58
        self.once(warning)
407
8c089804207e Initial version
rgbecker
parents:
diff changeset
    59
8c089804207e Initial version
rgbecker
parents:
diff changeset
    60
warnOnce=WarnOnce()
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3029
diff changeset
    61
infoOnce=WarnOnce('Info')