source: branches/polybatch/python/logging.py@ 2908

Last change on this file since 2908 was 1862, checked in by Malte Marquarding, 14 years ago

renamed print_log_dec to more explicit asaplog_post_dec

File size: 3.1 KB
Line 
1"""This module presents a logging abstraction layer on top of casa.
2"""
3__all__ = ["asaplog", "asaplog_post_dec", "AsapLogger"]
4
5import inspect
6from asap.env import is_casapy
7from asap.parameters import rcParams
8from asap._asap import LogSink, set_global_sink
9try:
10 from functools import wraps as wraps_dec
11except ImportError:
12 from asap.compatibility import wraps as wraps_dec
13
14
15class AsapLogger(object):
16 """Wrapper object to allow for both casapy and asap logging.
17
18 Inside casapy this will connect to `taskinit.casalog`. Otherwise it will
19 create its own casa log sink.
20
21 .. note:: Do instantiate a new one - use the :obj:`asaplog` instead.
22
23 """
24 def __init__(self):
25 self._enabled = True
26 self._log = ""
27 if is_casapy():
28 from taskinit import casalog
29 self.logger = casalog
30 else:
31 self.logger = LogSink()
32 set_global_sink(self.logger)
33
34 def post(self, level='INFO', origin=""):
35 """Post the messages to the logger. This will clear the buffered
36 logs.
37
38 Parameters:
39
40 level: The log level (severity). One of INFO, WARN, ERROR.
41
42 """
43 if not self._enabled:
44 return
45
46 if not origin:
47 origin = inspect.getframeinfo(inspect.currentframe().f_back)[2]
48 logs = self._log.strip()
49 if len(logs) > 0:
50 self.logger.post(logs, priority=level, origin=origin)
51 if isinstance(self.logger, LogSink):
52 logs = self.logger.pop().strip()
53 if len(logs) > 0:
54 print logs
55 self._log = ""
56
57 def push(self, msg, newline=True):
58 """Push logs into the buffer. post needs to be called to send them.
59
60 Parameters:
61
62 msg: the log message (string)
63
64 newline: should we terminate with a newline (default yes)
65
66 """
67 if self._enabled:
68 sep = ""
69 self._log = sep.join([self._log, msg])
70 if newline:
71 self._log += "\n"
72
73 def enable(self, flag=True):
74 """Enable (or disable) logging."""
75 self._enabled = flag
76
77 def disable(self, flag=False):
78 """Disable (or enable) logging"""
79 self._enabled = flag
80
81 def is_enabled(self):
82 return self._enabled
83
84asaplog = AsapLogger()
85"""Default asap logger"""
86
87def asaplog_post_dec(f):
88 """Decorator which posts log at completion of the wrapped method.
89
90 Example::
91
92 @asaplog_post_dec
93 def test(self):
94 do_stuff()
95 asaplog.push('testing...', False)
96 do_more_stuff()
97 asaplog.push('finished')
98 """
99 @wraps_dec(f)
100 def wrap_it(*args, **kw):
101 level = "INFO"
102 try:
103 val = f(*args, **kw)
104 return val
105 except Exception, ex:
106 level = "ERROR"
107 asaplog.push(str(ex))
108 if rcParams['verbose']:
109 pass
110 else:
111 raise
112 finally:
113 asaplog.post(level, f.func_name)
114 #asaplog.post(level, ".".join([f.__module__,f.func_name]))
115 return wrap_it
116
Note: See TracBrowser for help on using the repository browser.