Last change
on this file since 1801 was 1797, checked in by Malte Marquarding, 14 years ago |
Ticket #193: added new logging class. Also factored out environment based settings from init.py into env.py. This hasn't been enabled/isn't used yet
|
File size:
1.7 KB
|
Line | |
---|
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 |
|
---|
6 | class _asaplog(object):
|
---|
7 | """Wrapper object to allow for both casapy and asap logging"""
|
---|
8 | def __init__(self):
|
---|
9 | self._enabled = False
|
---|
10 | self._log = ""
|
---|
11 | if is_casapy():
|
---|
12 | from taskinit import casalog
|
---|
13 | self.logger = casalog
|
---|
14 | else:
|
---|
15 | self.logger = LogSink()
|
---|
16 | set_global_sink(self.logger)
|
---|
17 |
|
---|
18 | def post(self, level):
|
---|
19 | """Post the messages to the logger. This will clear the buffered
|
---|
20 | logs.
|
---|
21 | """
|
---|
22 | if len(self._log) > 0:
|
---|
23 | self.logger.post(self._log, priority=level)
|
---|
24 | if isinstance(self.logger, LogSink):
|
---|
25 | logs = self.logger.pop()
|
---|
26 | if logs > 0:
|
---|
27 | print logs
|
---|
28 | self._log = ""
|
---|
29 |
|
---|
30 | def push(self, msg, newline=True):
|
---|
31 | """Push logs into the buffer. post needs to be called to send them."""
|
---|
32 | from asap import rcParams
|
---|
33 | if self._enabled:
|
---|
34 | if rcParams["verbose"]:
|
---|
35 | sep = ""
|
---|
36 | self._log = sep.join([self._log, msg])
|
---|
37 | if newline:
|
---|
38 | self._log += "\n"
|
---|
39 |
|
---|
40 | def enable(self, flag=True):
|
---|
41 | """Enable (or disable) logging."""
|
---|
42 | self._enabled = flag
|
---|
43 |
|
---|
44 | def disable(self, flag=False):
|
---|
45 | """Disable (or enable) logging"""
|
---|
46 | self._enabled = flag
|
---|
47 |
|
---|
48 | asaplog = _asaplog()
|
---|
49 |
|
---|
50 | def print_log_dec(f, level):
|
---|
51 | @wraps_dec(f)
|
---|
52 | def wrap_it(*args, **kw):
|
---|
53 | val = f(*args, **kw)
|
---|
54 | print_log(level)
|
---|
55 | return val
|
---|
56 | return wrap_it
|
---|
57 |
|
---|
58 | def print_log(level='INFO'):
|
---|
59 | """Alias for asaplog.post."""
|
---|
60 | asaplog.post(level)
|
---|
Note:
See
TracBrowser
for help on using the repository browser.