Go to the documentation of this file.00001 """This module presents a logging abstraction layer on top of casa.
00002 """
00003 __all__ = ["asaplog", "asaplog_post_dec", "AsapLogger"]
00004
00005 import inspect
00006 import sys
00007 from asap.env import is_casapy
00008 from asap.parameters import rcParams
00009 from asap._asap import LogSink, set_global_sink
00010 try:
00011 from functools import wraps as wraps_dec
00012 except ImportError:
00013 from asap.compatibility import wraps as wraps_dec
00014
00015
00016 class AsapLogger(object):
00017 """Wrapper object to allow for both casapy and asap logging.
00018
00019 Inside casapy this will connect to `taskinit.casalog`. Otherwise it will
00020 create its own casa log sink.
00021
00022 .. note:: Do not instantiate a new one - use the :obj:`asaplog` instead.
00023
00024 """
00025 def __init__(self):
00026 self._enabled = True
00027 self._log = ""
00028 if is_casapy():
00029 from taskinit import casalog
00030 self.logger = casalog
00031 else:
00032 self.logger = LogSink()
00033 set_global_sink(self.logger)
00034
00035 def post(self, level='INFO', origin=""):
00036 """Post the messages to the logger. This will clear the buffered
00037 logs.
00038
00039 Parameters:
00040
00041 level: The log level (severity). One of INFO, WARN, ERROR.
00042
00043 """
00044 if not self._enabled:
00045 return
00046
00047 if not origin:
00048 origin = inspect.getframeinfo(inspect.currentframe().f_back)[2]
00049 logs = self._log.strip()
00050 if len(logs) > 0:
00051 if isinstance(self.logger, LogSink):
00052
00053 logs = str(logs)
00054 self.logger.post(logs, priority=level, origin=origin)
00055 if isinstance(self.logger, LogSink):
00056 logs = self.logger.pop().strip()
00057 if len(logs) > 0:
00058 if rcParams['verbose']:
00059 print >>sys.stdout, logs
00060 if hasattr(sys.stdout, "flush"):
00061 sys.stdout.flush()
00062 self._log = ""
00063
00064 def clear(self):
00065 if isinstance(self.logger, LogSink):
00066 logs = self.logger.pop()
00067
00068 def push(self, msg, newline=True):
00069 """Push logs into the buffer. post needs to be called to send them.
00070
00071 Parameters:
00072
00073 msg: the log message (string)
00074
00075 newline: should we terminate with a newline (default yes)
00076
00077 """
00078 if self._enabled:
00079 sep = ""
00080 self._log = sep.join([self._log, msg])
00081 if newline:
00082 self._log += "\n"
00083
00084 def enable(self, flag=True):
00085 """Enable (or disable) logging."""
00086 self._enabled = flag
00087
00088 def disable(self, flag=False):
00089 """Disable (or enable) logging"""
00090 self._enabled = flag
00091
00092 def is_enabled(self):
00093 return self._enabled
00094
00095 asaplog = AsapLogger()
00096 """Default asap logger"""
00097
00098 def asaplog_post_dec(f):
00099 """Decorator which posts log at completion of the wrapped method.
00100
00101 Example::
00102
00103 @asaplog_post_dec
00104 def test(self):
00105 do_stuff()
00106 asaplog.push('testing...', False)
00107 do_more_stuff()
00108 asaplog.push('finished')
00109 """
00110 @wraps_dec(f)
00111 def wrap_it(*args, **kw):
00112 level = "INFO"
00113 try:
00114 try:
00115 val = f(*args, **kw)
00116 return val
00117 except Exception, ex:
00118 level = "ERROR"
00119 asaplog.push(str(ex))
00120 if rcParams['verbose']:
00121 pass
00122 else:
00123 raise
00124 finally:
00125 asaplog.post(level, f.func_name)
00126 return wrap_it
00127