1 | __all__ = ["asaplog", "print_log", "print_log_dec"]
|
---|
2 |
|
---|
3 | from asap.env import is_casapy
|
---|
4 | from asap._asap import LogSink, set_global_sink
|
---|
5 | try:
|
---|
6 | from functools import wraps as wraps_dec
|
---|
7 | except ImportError:
|
---|
8 | from asap.compatibility import wraps as wraps_dec
|
---|
9 |
|
---|
10 |
|
---|
11 | class _asaplog(object):
|
---|
12 | """Wrapper object to allow for both casapy and asap logging"""
|
---|
13 | def __init__(self):
|
---|
14 | self._enabled = False
|
---|
15 | self._log = ""
|
---|
16 | if is_casapy():
|
---|
17 | from taskinit import casalog
|
---|
18 | self.logger = casalog
|
---|
19 | else:
|
---|
20 | self.logger = LogSink()
|
---|
21 | set_global_sink(self.logger)
|
---|
22 |
|
---|
23 | def post(self, level):
|
---|
24 | """Post the messages to the logger. This will clear the buffered
|
---|
25 | logs.
|
---|
26 | """
|
---|
27 | if len(self._log) > 0:
|
---|
28 | self.logger.post(self._log, priority=level)
|
---|
29 | if isinstance(self.logger, LogSink):
|
---|
30 | logs = self.logger.pop()
|
---|
31 | if logs > 0:
|
---|
32 | print logs
|
---|
33 | self._log = ""
|
---|
34 |
|
---|
35 | def push(self, msg, newline=True):
|
---|
36 | """Push logs into the buffer. post needs to be called to send them."""
|
---|
37 | from asap import rcParams
|
---|
38 | if self._enabled:
|
---|
39 | if rcParams["verbose"]:
|
---|
40 | sep = ""
|
---|
41 | self._log = sep.join([self._log, msg])
|
---|
42 | if newline:
|
---|
43 | self._log += "\n"
|
---|
44 |
|
---|
45 | def enable(self, flag=True):
|
---|
46 | """Enable (or disable) logging."""
|
---|
47 | self._enabled = flag
|
---|
48 |
|
---|
49 | def disable(self, flag=False):
|
---|
50 | """Disable (or enable) logging"""
|
---|
51 | self._enabled = flag
|
---|
52 |
|
---|
53 | asaplog = _asaplog()
|
---|
54 |
|
---|
55 | def print_log_dec(f, level='INFO'):
|
---|
56 | @wraps_dec(f)
|
---|
57 | def wrap_it(*args, **kw):
|
---|
58 | val = f(*args, **kw)
|
---|
59 | print_log(level)
|
---|
60 | return val
|
---|
61 | return wrap_it
|
---|
62 |
|
---|
63 | def print_log(level='INFO'):
|
---|
64 | """Alias for asaplog.post."""
|
---|
65 | asaplog.post(level)
|
---|