source: branches/newfiller/python/logging.py@ 2723

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

forgot to add wraps_dec import

File size: 1.8 KB
RevLine 
[1797]1__all__ = ["asaplog", "print_log", "print_log_dec"]
2
3from asap.env import is_casapy
4from asap._asap import LogSink, set_global_sink
[1808]5try:
6 from functools import wraps as wraps_dec
7except ImportError:
8 from asap.compatibility import wraps as wraps_dec
[1797]9
[1808]10
[1797]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 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
53asaplog = _asaplog()
54
[1808]55def print_log_dec(f, level='INFO'):
[1797]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
63def print_log(level='INFO'):
64 """Alias for asaplog.post."""
65 asaplog.post(level)
Note: See TracBrowser for help on using the repository browser.