| 1 |
""" |
| 2 |
basic logging functionality based on a producer/consumer scheme. |
| 3 |
|
| 4 |
XXX implement this API: (maybe put it into slogger.py?) |
| 5 |
|
| 6 |
log = Logger( |
| 7 |
info=py.log.STDOUT, |
| 8 |
debug=py.log.STDOUT, |
| 9 |
command=None) |
| 10 |
log.info("hello", "world") |
| 11 |
log.command("hello", "world") |
| 12 |
|
| 13 |
log = Logger(info=Logger(something=...), |
| 14 |
debug=py.log.STDOUT, |
| 15 |
command=None) |
| 16 |
""" |
| 17 |
import py, sys |
| 18 |
|
| 19 |
class Message(object): |
| 20 |
def __init__(self, keywords, args): |
| 21 |
self.keywords = keywords |
| 22 |
self.args = args |
| 23 |
|
| 24 |
def content(self): |
| 25 |
return " ".join(map(str, self.args)) |
| 26 |
|
| 27 |
def prefix(self): |
| 28 |
return "[%s] " % (":".join(self.keywords)) |
| 29 |
|
| 30 |
def __str__(self): |
| 31 |
return self.prefix() + self.content() |
| 32 |
|
| 33 |
|
| 34 |
class Producer(object): |
| 35 |
""" (deprecated) Log producer API which sends messages to be logged |
| 36 |
to a 'consumer' object, which then prints them to stdout, |
| 37 |
stderr, files, etc. Used extensively by PyPy-1.1. |
| 38 |
""" |
| 39 |
|
| 40 |
Message = Message |
| 41 |
keywords2consumer = {} |
| 42 |
|
| 43 |
def __init__(self, keywords, keywordmapper=None, **kw): |
| 44 |
if hasattr(keywords, 'split'): |
| 45 |
keywords = tuple(keywords.split()) |
| 46 |
self._keywords = keywords |
| 47 |
if keywordmapper is None: |
| 48 |
keywordmapper = default_keywordmapper |
| 49 |
self._keywordmapper = keywordmapper |
| 50 |
|
| 51 |
def __repr__(self): |
| 52 |
return "<py.log.Producer %s>" % ":".join(self._keywords) |
| 53 |
|
| 54 |
def __getattr__(self, name): |
| 55 |
if '_' in name: |
| 56 |
raise AttributeError(name) |
| 57 |
producer = self.__class__(self._keywords + (name,)) |
| 58 |
setattr(self, name, producer) |
| 59 |
return producer |
| 60 |
|
| 61 |
def __call__(self, *args): |
| 62 |
""" write a message to the appropriate consumer(s) """ |
| 63 |
func = self._keywordmapper.getconsumer(self._keywords) |
| 64 |
if func is not None: |
| 65 |
func(self.Message(self._keywords, args)) |
| 66 |
|
| 67 |
class KeywordMapper: |
| 68 |
def __init__(self): |
| 69 |
self.keywords2consumer = {} |
| 70 |
|
| 71 |
def getstate(self): |
| 72 |
return self.keywords2consumer.copy() |
| 73 |
def setstate(self, state): |
| 74 |
self.keywords2consumer.clear() |
| 75 |
self.keywords2consumer.update(state) |
| 76 |
|
| 77 |
def getconsumer(self, keywords): |
| 78 |
""" return a consumer matching the given keywords. |
| 79 |
|
| 80 |
tries to find the most suitable consumer by walking, starting from |
| 81 |
the back, the list of keywords, the first consumer matching a |
| 82 |
keyword is returned (falling back to py.log.default) |
| 83 |
""" |
| 84 |
for i in range(len(keywords), 0, -1): |
| 85 |
try: |
| 86 |
return self.keywords2consumer[keywords[:i]] |
| 87 |
except KeyError: |
| 88 |
continue |
| 89 |
return self.keywords2consumer.get('default', default_consumer) |
| 90 |
|
| 91 |
def setconsumer(self, keywords, consumer): |
| 92 |
""" set a consumer for a set of keywords. """ |
| 93 |
|
| 94 |
if isinstance(keywords, str): |
| 95 |
keywords = tuple(filter(None, keywords.split())) |
| 96 |
elif hasattr(keywords, '_keywords'): |
| 97 |
keywords = keywords._keywords |
| 98 |
elif not isinstance(keywords, tuple): |
| 99 |
raise TypeError("key %r is not a string or tuple" % (keywords,)) |
| 100 |
if consumer is not None and not py.builtin.callable(consumer): |
| 101 |
if not hasattr(consumer, 'write'): |
| 102 |
raise TypeError( |
| 103 |
"%r should be None, callable or file-like" % (consumer,)) |
| 104 |
consumer = File(consumer) |
| 105 |
self.keywords2consumer[keywords] = consumer |
| 106 |
|
| 107 |
def default_consumer(msg): |
| 108 |
""" the default consumer, prints the message to stdout (using 'print') """ |
| 109 |
sys.stderr.write(str(msg)+"\n") |
| 110 |
|
| 111 |
default_keywordmapper = KeywordMapper() |
| 112 |
|
| 113 |
def setconsumer(keywords, consumer): |
| 114 |
default_keywordmapper.setconsumer(keywords, consumer) |
| 115 |
|
| 116 |
def setstate(state): |
| 117 |
default_keywordmapper.setstate(state) |
| 118 |
def getstate(): |
| 119 |
return default_keywordmapper.getstate() |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
class File(object): |
| 126 |
""" log consumer wrapping a file(-like) object """ |
| 127 |
def __init__(self, f): |
| 128 |
assert hasattr(f, 'write') |
| 129 |
|
| 130 |
self._file = f |
| 131 |
|
| 132 |
def __call__(self, msg): |
| 133 |
""" write a message to the log """ |
| 134 |
self._file.write(str(msg) + "\n") |
| 135 |
if hasattr(self._file, 'flush'): |
| 136 |
self._file.flush() |
| 137 |
|
| 138 |
class Path(object): |
| 139 |
""" log consumer that opens and writes to a Path """ |
| 140 |
def __init__(self, filename, append=False, |
| 141 |
delayed_create=False, buffering=False): |
| 142 |
self._append = append |
| 143 |
self._filename = str(filename) |
| 144 |
self._buffering = buffering |
| 145 |
if not delayed_create: |
| 146 |
self._openfile() |
| 147 |
|
| 148 |
def _openfile(self): |
| 149 |
mode = self._append and 'a' or 'w' |
| 150 |
f = open(self._filename, mode) |
| 151 |
self._file = f |
| 152 |
|
| 153 |
def __call__(self, msg): |
| 154 |
""" write a message to the log """ |
| 155 |
if not hasattr(self, "_file"): |
| 156 |
self._openfile() |
| 157 |
self._file.write(str(msg) + "\n") |
| 158 |
if not self._buffering: |
| 159 |
self._file.flush() |
| 160 |
|
| 161 |
def STDOUT(msg): |
| 162 |
""" consumer that writes to sys.stdout """ |
| 163 |
sys.stdout.write(str(msg)+"\n") |
| 164 |
|
| 165 |
def STDERR(msg): |
| 166 |
""" consumer that writes to sys.stderr """ |
| 167 |
sys.stderr.write(str(msg)+"\n") |
| 168 |
|
| 169 |
class Syslog: |
| 170 |
""" consumer that writes to the syslog daemon """ |
| 171 |
|
| 172 |
def __init__(self, priority = None): |
| 173 |
if priority is None: |
| 174 |
priority = self.LOG_INFO |
| 175 |
self.priority = priority |
| 176 |
|
| 177 |
def __call__(self, msg): |
| 178 |
""" write a message to the log """ |
| 179 |
py.std.syslog.syslog(self.priority, str(msg)) |
| 180 |
|
| 181 |
for _prio in "EMERG ALERT CRIT ERR WARNING NOTICE INFO DEBUG".split(): |
| 182 |
_prio = "LOG_" + _prio |
| 183 |
try: |
| 184 |
setattr(Syslog, _prio, getattr(py.std.syslog, _prio)) |
| 185 |
except AttributeError: |
| 186 |
pass |