Initial revision
[pli.git] / pli / aspect / log.py
blob014fd089f6d701c05d887245946c5327ac3e0373
1 #=======================================================================
2 '''
3 this module defines the logger aspects.
4 '''
6 #-----------------------------------------------------------------------
8 __version__ = '''0.1.03'''
9 __sub_version__ = '''20040212153738'''
10 __copyright__ = '''(c) Alex A. Naanou 2003'''
14 #-----------------------------------------------------------------------
16 import sys
17 import time
18 import aspect
22 #-----------------------------------------------------------------------
23 # LoggerAspect flags:
24 # make the aspect pre-condirioned
25 LOG_PRE = 1
26 # make the aspect post-conditioned.
27 LOG_POST = 2
30 #--------------------------------------------------------LoggerAspect---
31 # TODO make this thread-safe...
33 class LoggerAspect(aspect.Aspect):
34 '''
35 '''
36 def __init__(self, file=None, mode=LOG_POST, format=None, verbosity=1):
37 '''
38 '''
39 self.file = file
40 self.mode = mode
41 self.verbosity = verbosity
42 if format == None:
43 self.format = '[%(time)s] [%(mode)s] --'
44 else:
45 self.format = format
46 # this is the part that is not thread safe...
47 def out(self, *pargs, **nargs):
48 '''
49 this is the output method...
50 '''
51 # do dynamic output file linking...
52 if self.file == None:
53 output = sys.stderr
54 else:
55 output = self.file
56 for i in pargs:
57 print >> output, i,
58 print >> output
59 def pre(self, obj, meth, *pargs, **nargs):
60 '''
61 '''
62 if self.mode&LOG_PRE:
63 v = self.verbosity
64 if v < 1:
65 pass
66 elif v == 1:
67 self.out(self.format % {'time':time.strftime('%Y%m%d%H%M%S'), 'mode': 'pre'},\
68 meth.__name__)
69 elif v > 1:
70 self.out(self.format % {'time':time.strftime('%Y%m%d%H%M%S'), 'mode': 'pre'},\
71 meth.__name__, *pargs, **nargs)
72 def post(self, obj, meth, ret, rexcept, *pargs, **nargs):
73 '''
74 '''
75 if self.mode&LOG_POST:
76 v = self.verbosity
77 if v < 1:
78 pass
79 elif v == 1:
80 self.out(self.format % {'time':time.strftime('%Y%m%d%H%M%S'), 'mode': 'post'},\
81 meth.__name__, ret, rexcept)
82 elif v > 1:
83 self.out(self.format % {'time':time.strftime('%Y%m%d%H%M%S'), 'mode': 'post'},\
84 meth.__name__, ret, rexcept, *pargs, **nargs)
88 #=======================================================================
89 # vim:set ts=4 sw=4 nowrap :