source: trunk/python/logging.py@ 1984

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

Ticket #210: cast to string in case we get passed unicode

File size: 3.2 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
[1797]6from asap.env import is_casapy
[1833]7from asap.parameters import rcParams
[1797]8from asap._asap import LogSink, set_global_sink
[1808]9try:
10 from functools import wraps as wraps_dec
11except ImportError:
12 from asap.compatibility import wraps as wraps_dec
[1797]13
[1808]14
[1858]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.
[1859]22
[1858]23 """
[1797]24 def __init__(self):
[1859]25 self._enabled = True
[1797]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
[1861]34 def post(self, level='INFO', origin=""):
[1797]35 """Post the messages to the logger. This will clear the buffered
36 logs.
[1858]37
38 Parameters:
39
40 level: The log level (severity). One of INFO, WARN, ERROR.
41
[1797]42 """
[1829]43 if not self._enabled:
44 return
45
[1861]46 if not origin:
47 origin = inspect.getframeinfo(inspect.currentframe().f_back)[2]
[1824]48 logs = self._log.strip()
49 if len(logs) > 0:
[1939]50 if isinstance(self.logger, LogSink):
51 #can't handle unicode in boost signature
52 logs = str(logs)
[1859]53 self.logger.post(logs, priority=level, origin=origin)
[1797]54 if isinstance(self.logger, LogSink):
[1824]55 logs = self.logger.pop().strip()
56 if len(logs) > 0:
[1797]57 print logs
58 self._log = ""
59
60 def push(self, msg, newline=True):
[1858]61 """Push logs into the buffer. post needs to be called to send them.
62
63 Parameters:
64
65 msg: the log message (string)
66
67 newline: should we terminate with a newline (default yes)
68
69 """
[1797]70 if self._enabled:
[1859]71 sep = ""
72 self._log = sep.join([self._log, msg])
73 if newline:
74 self._log += "\n"
[1797]75
76 def enable(self, flag=True):
77 """Enable (or disable) logging."""
78 self._enabled = flag
79
80 def disable(self, flag=False):
81 """Disable (or enable) logging"""
82 self._enabled = flag
83
[1859]84 def is_enabled(self):
85 return self._enabled
86
[1858]87asaplog = AsapLogger()
88"""Default asap logger"""
[1797]89
[1862]90def asaplog_post_dec(f):
[1858]91 """Decorator which posts log at completion of the wrapped method.
[1859]92
[1858]93 Example::
94
[1862]95 @asaplog_post_dec
[1858]96 def test(self):
97 do_stuff()
98 asaplog.push('testing...', False)
99 do_more_stuff()
100 asaplog.push('finished')
101 """
[1797]102 @wraps_dec(f)
103 def wrap_it(*args, **kw):
[1859]104 level = "INFO"
105 try:
106 val = f(*args, **kw)
107 return val
108 except Exception, ex:
109 level = "ERROR"
110 asaplog.push(str(ex))
111 if rcParams['verbose']:
112 pass
113 else:
114 raise
115 finally:
[1861]116 asaplog.post(level, f.func_name)
117 #asaplog.post(level, ".".join([f.__module__,f.func_name]))
[1797]118 return wrap_it
119
Note: See TracBrowser for help on using the repository browser.