1 | """This module presents a logging abstraction layer on top of casa.
|
---|
2 | """
|
---|
3 | __all__ = ["asaplog", "asaplog_post_dec", "AsapLogger"]
|
---|
4 |
|
---|
5 | import inspect
|
---|
6 | from asap.env import is_casapy
|
---|
7 | from asap.parameters import rcParams
|
---|
8 | from asap._asap import LogSink, set_global_sink
|
---|
9 | try:
|
---|
10 | from functools import wraps as wraps_dec
|
---|
11 | except ImportError:
|
---|
12 | from asap.compatibility import wraps as wraps_dec
|
---|
13 |
|
---|
14 |
|
---|
15 | class 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 not 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 | if isinstance(self.logger, LogSink):
|
---|
51 | #can't handle unicode in boost signature
|
---|
52 | logs = str(logs)
|
---|
53 | self.logger.post(logs, priority=level, origin=origin)
|
---|
54 | if isinstance(self.logger, LogSink):
|
---|
55 | logs = self.logger.pop().strip()
|
---|
56 | if len(logs) > 0:
|
---|
57 | print logs
|
---|
58 | self._log = ""
|
---|
59 |
|
---|
60 | def clear(self):
|
---|
61 | if isinstance(self.logger, LogSink):
|
---|
62 | logs = self.logger.pop()
|
---|
63 |
|
---|
64 | def push(self, msg, newline=True):
|
---|
65 | """Push logs into the buffer. post needs to be called to send them.
|
---|
66 |
|
---|
67 | Parameters:
|
---|
68 |
|
---|
69 | msg: the log message (string)
|
---|
70 |
|
---|
71 | newline: should we terminate with a newline (default yes)
|
---|
72 |
|
---|
73 | """
|
---|
74 | if self._enabled:
|
---|
75 | sep = ""
|
---|
76 | self._log = sep.join([self._log, msg])
|
---|
77 | if newline:
|
---|
78 | self._log += "\n"
|
---|
79 |
|
---|
80 | def enable(self, flag=True):
|
---|
81 | """Enable (or disable) logging."""
|
---|
82 | self._enabled = flag
|
---|
83 |
|
---|
84 | def disable(self, flag=False):
|
---|
85 | """Disable (or enable) logging"""
|
---|
86 | self._enabled = flag
|
---|
87 |
|
---|
88 | def is_enabled(self):
|
---|
89 | return self._enabled
|
---|
90 |
|
---|
91 | asaplog = AsapLogger()
|
---|
92 | """Default asap logger"""
|
---|
93 |
|
---|
94 | def asaplog_post_dec(f):
|
---|
95 | """Decorator which posts log at completion of the wrapped method.
|
---|
96 |
|
---|
97 | Example::
|
---|
98 |
|
---|
99 | @asaplog_post_dec
|
---|
100 | def test(self):
|
---|
101 | do_stuff()
|
---|
102 | asaplog.push('testing...', False)
|
---|
103 | do_more_stuff()
|
---|
104 | asaplog.push('finished')
|
---|
105 | """
|
---|
106 | @wraps_dec(f)
|
---|
107 | def wrap_it(*args, **kw):
|
---|
108 | level = "INFO"
|
---|
109 | try:
|
---|
110 | val = f(*args, **kw)
|
---|
111 | return val
|
---|
112 | except Exception, ex:
|
---|
113 | level = "ERROR"
|
---|
114 | asaplog.push(str(ex))
|
---|
115 | if rcParams['verbose']:
|
---|
116 | pass
|
---|
117 | else:
|
---|
118 | raise
|
---|
119 | finally:
|
---|
120 | asaplog.post(level, f.func_name)
|
---|
121 | #asaplog.post(level, ".".join([f.__module__,f.func_name]))
|
---|
122 | return wrap_it
|
---|
123 |
|
---|