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