source: trunk/python/logging.py@ 1860

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

Ticket #193: the rcParamsverbose flag is only used in standard asap cli mode. Otherwise log messages are always send to the logger and one needs to call asaplog.disable()/asaplog.enable() to controls this. I have also added the function name as the log origin.

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