1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
import datetime
import logging
import os
import sys
import threading
import CPL_recipe
class NullHandler(logging.Handler):
def emit(self, record):
pass
logging.getLogger('cpl').addHandler(NullHandler())
level = { "DEBUG":logging.DEBUG, "INFO":logging.INFO, "WARNING":logging.WARN,
"ERROR":logging.ERROR }
class LogServer(threading.Thread):
def __init__(self, filename, name, level):
threading.Thread.__init__(self)
self.logfile = filename
self.name = name
self.logger = logging.getLogger(name)
self.level = CplLogger.verbosity.index(level)
self.entries = LogList()
os.mkfifo(self.logfile)
self.start()
def run(self):
try:
logfile = open(self.logfile, buffering = 0)
except:
pass
try:
line = logfile.readline()
while line:
self.log(line)
line = logfile.readline()
except:
pass
def log(self, s):
try:
creation_date = datetime.datetime.combine(
datetime.date.today(),
datetime.time(int(s[0:2]),int(s[3:5]),int(s[6:8])))
lvl = level.get(s[10:17].strip(), logging.NOTSET)
func = s[19:].split(':', 1)[0]
msg = s[19:].split(':', 1)[1][1:-1]
if msg.startswith('[tid='):
threadid = int(msg[5:8])
msg = msg[10:] if threadid > 0 else msg[12:]
else:
threadid = None
log = logging.getLogger('%s.%s' % (self.logger.name, func))
record = logging.LogRecord(log.name, lvl, None, None, msg,
None, None, func)
created = float(creation_date.strftime('%s'))
if record.created < created:
created -= 86400
record.relativeCreated -= record.msecs
record.relativeCreated += 1000*(created - record.created + 1)
record.created = created
record.msecs = 0.0
record.threadid = threadid
record.threadName = ('Cpl-%03i' % threadid) if threadid else 'CplThread'
self.entries.append(record)
if log.isEnabledFor(lvl) and log.filter(record):
log.handle(record)
except:
pass
class LogList(list):
'''List of log messages.
Accessing this :class:`list` directly will return the
:class:`logging.LogRecord` instances.
Example::
res = muse_bias(bias_frames)
for logrecord in res.log:
print '%s: %s' % (entry.funcname, entry.msg)
To get them formatted as string, use the :attr:`error`, :attr:`warning`,
:attr:`info` or :attr:`debug` attributes::
res = muse_bias(bias_frames)
for line in res.log.info:
print line
'''
def filter(self, level):
return [ '%s: %s' % (entry.funcName, entry.msg) for entry in self
if entry.levelno >= level ]
@property
def error(self):
'''Error messages as list of :class:`str`
'''
return self.filter(logging.ERROR)
@property
def warning(self):
'''Warnings and error messages as list of :class:`str`
'''
return self.filter(logging.WARN)
@property
def info(self):
'''Info, warning and error messages as list of :class:`str`
'''
return self.filter(logging.INFO)
@property
def debug(self):
'''Debug, info, warning, and error messages as list of :class:`str`
'''
return self.filter(logging.DEBUG)
class CplLogger(object):
DEBUG = logging.DEBUG
INFO = logging.INFO
WARN = logging.WARN
ERROR = logging.ERROR
OFF = 101
verbosity = [ DEBUG, INFO, WARN, ERROR, OFF ]
_time_enabled = False
def __init__(self, name = 'cpl'):
self.name = name
@property
def level(self):
'''Log level for output to the terminal. Any of
[ DEBUG, INFO, WARN, ERROR, OFF ]
.. deprecated:: 0.3
Use :func:`logging.Logger.setLevel`
'''
return CplLogger.verbosity[CPL_recipe.get_msg_level()]
@level.setter
def level(self, level):
CPL_recipe.set_msg_level(CplLogger.verbosity.index(level))
@property
def time(self):
'''Specify whether time tag shall be included in the terminal output
.. deprecated:: 0.3
Use :func:`logging.Handler.setFormatter`
'''
return CplLogger._time_enabled
@time.setter
def time(self, enable):
CPL_recipe.set_msg_time(enable);
CplLogger._time_enabled = not not enable
@property
def domain(self):
'''The domain tag in the header of the log file.
.. deprecated:: 0.3
Use :func:`logging.getLogger`
'''
return CPL_recipe.get_log_domain()
@domain.setter
def domain(self, domain):
CPL_recipe.set_log_domain(domain)
def log(self, level, msg, caller = None):
if caller == None:
caller = CPL_recipe.get_log_domain()
logging.getLogger('%s.%s' % (self.name, caller)).log(level, msg)
CPL_recipe.log(CplLogger.verbosity.index(level), caller, msg)
def debug(self, msg, caller = None):
'''Put a 'debug' message to the log.
:param msg: Message to put
:type msg: :class:`str`
:param caller: Name of the function generating the message.
:type caller: :class:`str`
.. deprecated:: 0.3
Use :func:`logging.Logger.debug`
'''
self.log(CplLogger.DEBUG, msg, caller)
def info(self, msg, caller = None):
'''Put an 'info' message to the log.
:param msg: Message to put
:type msg: :class:`str`
:param caller: Name of the function generating the message.
:type caller: :class:`str`
.. deprecated:: 0.3
Use :func:`logging.Logger.info`
'''
self.log(CplLogger.INFO, msg, caller)
def warn(self, msg, caller = None):
'''Put a 'warn' message to the log.
:param msg: Message to put
:type msg: :class:`str`
:param caller: Name of the function generating the message.
:type caller: :class:`str`
.. deprecated:: 0.3
Use :func:`logging.Logger.warn`
'''
self.log(CplLogger.WARN, msg, caller)
def error(self, msg, caller = None):
'''Put an 'error' message to the log.
:param msg: Message to put
:type msg: :class:`str`
:param caller: Name of the function generating the message.
:type caller: :class:`str`
.. deprecated:: 0.3
Use :func:`logging.Logger.error`
'''
self.log(CplLogger.ERROR, msg, caller)
def indent_more(self):
'''Indent the output more.'''
CPL_recipe.log_indent_more()
def indent_less(self):
'''Indent the output less.'''
CPL_recipe.log_indent_less()
msg = CplLogger()
lib_version = CPL_recipe.version()
lib_description = CPL_recipe.description()
|