source: trunk/python/logging.py@ 2823

Last change on this file since 2823 was 2448, checked in by Malte Marquarding, 13 years ago

Fix rcParamsverbose handling. This was ignored.

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