source: trunk/python/logging.py@ 1830

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

Don't post message if logger is disabled

File size: 1.9 KB
Line 
1__all__ = ["asaplog", "print_log", "print_log_dec"]
2
3from asap.env import is_casapy
4from asap._asap import LogSink, set_global_sink
5try:
6 from functools import wraps as wraps_dec
7except ImportError:
8 from asap.compatibility import wraps as wraps_dec
9
10
11class _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 not self._enabled:
28 return
29 if not rcParams['verbose']:
30 return
31
32 logs = self._log.strip()
33 if len(logs) > 0:
34 self.logger.post(logs, priority=level)
35 if isinstance(self.logger, LogSink):
36 logs = self.logger.pop().strip()
37 if len(logs) > 0:
38 print logs
39 self._log = ""
40
41 def push(self, msg, newline=True):
42 """Push logs into the buffer. post needs to be called to send them."""
43 from asap import rcParams
44 if self._enabled:
45 if rcParams["verbose"]:
46 sep = ""
47 self._log = sep.join([self._log, msg])
48 if newline:
49 self._log += "\n"
50
51 def enable(self, flag=True):
52 """Enable (or disable) logging."""
53 self._enabled = flag
54
55 def disable(self, flag=False):
56 """Disable (or enable) logging"""
57 self._enabled = flag
58
59asaplog = _asaplog()
60
61def print_log_dec(f, level='INFO'):
62 @wraps_dec(f)
63 def wrap_it(*args, **kw):
64 val = f(*args, **kw)
65 print_log(level)
66 return val
67 return wrap_it
68
69def print_log(level='INFO'):
70 """Alias for asaplog.post."""
71 asaplog.post(level)
Note: See TracBrowser for help on using the repository browser.