1 | """This module presents a logging abstraction layer on top of casa.
|
---|
2 | """
|
---|
3 | __all__ = ["asaplog", "print_log", "print_log_dec", "AsapLogger"]
|
---|
4 |
|
---|
5 | from asap.env import is_casapy
|
---|
6 | from asap.parameters import rcParams
|
---|
7 | from asap._asap import LogSink, set_global_sink
|
---|
8 | try:
|
---|
9 | from functools import wraps as wraps_dec
|
---|
10 | except ImportError:
|
---|
11 | from asap.compatibility import wraps as wraps_dec
|
---|
12 |
|
---|
13 |
|
---|
14 | class 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 = False
|
---|
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):
|
---|
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 | if not rcParams['verbose']:
|
---|
45 | return
|
---|
46 |
|
---|
47 | logs = self._log.strip()
|
---|
48 | if len(logs) > 0:
|
---|
49 | self.logger.post(logs, priority=level)
|
---|
50 | if isinstance(self.logger, LogSink):
|
---|
51 | logs = self.logger.pop().strip()
|
---|
52 | if len(logs) > 0:
|
---|
53 | print logs
|
---|
54 | self._log = ""
|
---|
55 |
|
---|
56 | def push(self, msg, newline=True):
|
---|
57 | """Push logs into the buffer. post needs to be called to send them.
|
---|
58 |
|
---|
59 | Parameters:
|
---|
60 |
|
---|
61 | msg: the log message (string)
|
---|
62 |
|
---|
63 | newline: should we terminate with a newline (default yes)
|
---|
64 |
|
---|
65 | """
|
---|
66 | from asap import rcParams
|
---|
67 | if self._enabled:
|
---|
68 | if rcParams["verbose"]:
|
---|
69 | sep = ""
|
---|
70 | self._log = sep.join([self._log, msg])
|
---|
71 | if newline:
|
---|
72 | self._log += "\n"
|
---|
73 |
|
---|
74 | def enable(self, flag=True):
|
---|
75 | """Enable (or disable) logging."""
|
---|
76 | self._enabled = flag
|
---|
77 |
|
---|
78 | def disable(self, flag=False):
|
---|
79 | """Disable (or enable) logging"""
|
---|
80 | self._enabled = flag
|
---|
81 |
|
---|
82 | asaplog = AsapLogger()
|
---|
83 | """Default asap logger"""
|
---|
84 |
|
---|
85 | def print_log_dec(f, level='INFO'):
|
---|
86 | """Decorator which posts log at completion of the wrapped method.
|
---|
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 | val = f(*args, **kw)
|
---|
99 | print_log(level)
|
---|
100 | return val
|
---|
101 | return wrap_it
|
---|
102 |
|
---|
103 | def print_log(level='INFO'):
|
---|
104 | """Alias for asaplog.post(level)"""
|
---|
105 | asaplog.post(level)
|
---|