source: trunk/python/logging.py@ 1835

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

import asap.parameters

File size: 2.0 KB
Line 
1__all__ = ["asaplog", "print_log", "print_log_dec"]
2
3from asap.env import is_casapy
4from asap.parameters import rcParams
5from asap._asap import LogSink, set_global_sink
6try:
7 from functools import wraps as wraps_dec
8except ImportError:
9 from asap.compatibility import wraps as wraps_dec
10
11
12class _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 """
28 if not self._enabled:
29 return
30 if not rcParams['verbose']:
31 return
32
33 logs = self._log.strip()
34 if len(logs) > 0:
35 self.logger.post(logs, priority=level)
36 if isinstance(self.logger, LogSink):
37 logs = self.logger.pop().strip()
38 if len(logs) > 0:
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
60asaplog = _asaplog()
61
62def print_log_dec(f, level='INFO'):
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
70def print_log(level='INFO'):
71 """Alias for asaplog.post."""
72 asaplog.post(level)
Note: See TracBrowser for help on using the repository browser.