This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
/
__init__.py
365 lines (317 loc) · 10.4 KB
/
__init__.py
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
TODO: add a docstring.
"""
from coroutine import Coroutine
from coroutine import CoroutineProcess
from coroutine import CoroutineThread
import traceback
import inspect
import time
import sys
import os
__version__ = '0.1.0'
DEBUG = "DEBUG"
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"
ALL = [DEBUG, INFO, WARNING, ERROR, CRITICAL] # shortcut
# Allow function, module, sourcecode information
# ripped from http://hg.python.org/cpython/file/74fa415dc715/Lib/logging/__init__.py#l81
if hasattr(sys, 'frozen'): #support for py2exe
_srcfile = "logging%s__init%s" % (os.sep, __file[-4:])
else:
_srcfile = __file__
_srcfile = os.path.normcase(_srcfile)
#
try:
import threading
except:
threading = None
try:
import multiprocessing as mp
except:
mp = None
class Lggr():
""" Simplified logging. Dispatches messages to any type of
logging function you want to write, all it has to support
is send() and close(). """
def __init__(self, defaultfmt="{asctime} ({levelname}) {logmessage}"):
self.defaultfmt = defaultfmt
self.config = {
CRITICAL: set(), # these are different levels of logger functions
ERROR: set(),
DEBUG: set(),
WARNING: set(),
INFO: set(),
"defaultfmt": self.defaultfmt # lets lggrname.defaulfmt act as a shortcut
}
self.history = []
self.enabled = True
# allow instance.LEVEL instead of lggr.LEVEL
self.ALL = ALL
self.DEBUG = DEBUG
self.INFO = INFO
self.WARNING = WARNING
self.ERROR = ERROR
self.CRITICAL = CRITICAL
def disable(self):
""" Turn off logging. """
self.enabled = False
def enable(self):
""" Turn on logging. Enabled by default. """
self.enabled = True
def close(self):
""" Stop and remove all logging functions
and disable this logger. """
for level in ALL:
self.clear(level)
self.disable()
def add(self, levels, logger):
""" Given a list of logging levels, add a logger
instance to each. """
if isinstance(levels, list):
for lvl in levels:
self.config[lvl].add(logger)
else:
self.config[levels].add(logger)
def remove(self, level, logger):
""" Given a level, remove a given logger function
if it is a member of that level, closing the logger
function either way."""
self.config[level].discard(logger)
logger.close()
def clear(self, level):
""" Remove all logger functions from a given level. """
for item in self.config[level]:
item.close()
self.config[level].clear()
def makeRecord(self, level, fmt, args, extra, exc_info, inc_stack_info, inc_multi_proc):
""" Create a 'record' (a dictionary) with information to be logged. """
args_dict=False
if args and len(args) == 1 and isinstance(args[0], dict) and args[0]:
# args can be a list of unnamed variables or a dict of named variables
# to be used with str.format()
args = args[0]
args_dict=True
sinfo = None
if _srcfile and inc_stack_info:
#IronPython doesn't track Python frames, so findCaller throws an
#exception on some versionf of IronPython. We trap it here so that
#IronPython can use logging.
try:
fn, lno, func, code, cc, sinfo = self.findCaller()
except ValueError:
fn, lno, func, code, cc, sinfo = "(unknown file)", 0, "(unknown function)", "(code not available)", [], None
else:
fn, lno, func, code, cc, sinfo = "(unknown file)", 0, "(unknown function)", "(code not available)", [], None
try:
fname = os.path.basename(fn)
module = os.path.splitext(fname)[0]
except (TypeError, ValueError, AttributeError):
fname = fn
module = "Unknown module"
if not exc_info or not isinstance(exc_info, tuple):
# Allow passed in exc_info, but supply it if it isn't
exc_info = sys.exc_info()
log_record = { # This is available information for logging functions.
#TODO: proc_name, thread_name
# see http://hg.python.org/cpython/file/74fa415dc715/Lib/logging/__init__.py#l279
"levelname" : level,
"levelno" : ALL.index(level),
"pathname" : fn,
"filename" : fname,
"module" : module,
"exc_info" : exc_info,
"stack_info" : sinfo,
"lineno" : lno,
"funcname" : func,
"code": code,
"codecontext": "".join(cc),
"process" : os.getpid(),
"processname" : None,
"asctime": time.asctime(), # TODO: actual specifier for format
"time" : time.time(),
"threadid" : None,
"threadname" : None,
"messagefmt" : fmt,
"logmessage" : None,
# The custom `extra` information can only be used to format the default
# format. The `logmessage` can only be passed a dictionary or a list
# (as `args`).
"defaultfmt" : self.config['defaultfmt']
}
# If the user passed a single dict, use that with format.
# Otherwise, format using the passed args
if args_dict:
log_record['logmessage'] = fmt.format(**args)
else:
log_record['logmessage'] = fmt.format(*args)
if extra:
log_record.update(extra) # add custom variables to record
if threading: # check to use threading
curthread = threading.current_thread()
log_record.update({
"threadid" : curthread.ident,
"threadname" : curthread.name
})
if not inc_multi_proc: # check to use multiprocessing
procname = None
else:
procname = "MainProcess"
if mp:
try:
procname = mp.curent_process().name
except StandardError:
pass
log_record["processname"] = procname
return log_record
def log(self, level, fmt, args=None, extra=None, exc_info=None, inc_stack_info=False, inc_multi_proc=False):
""" Send a log message to all of the logging functions
for a given level as well as adding the
message to this logger instance's history. """
if not self.enabled:
return # Fail silently so that logging can easily be removed
log_record = self.makeRecord(level, fmt, args, extra, exc_info, inc_stack_info, inc_multi_proc)
logstr = log_record['defaultfmt'].format(**log_record) #whoah.
self.history.append(logstr)
log_funcs = self.config[level]
to_remove = []
for lf in log_funcs:
try:
lf.send(logstr)
except StopIteration: # already closed, add it to the list to be deleted
to_remove.append(lf)
for lf in to_remove:
self.remove(level, lf)
self.info("Logging function {} removed from level {}", lf, level)
#debug, info, warning, error, critical
def info(self, msg, *args, **kwargs):
"""" Log a message with INFO level """
self.log(INFO, msg, args, **kwargs)
def warning(self, msg, *args, **kwargs):
""" Log a message with WARNING level """
self.log(WARNING, msg, args, **kwargs)
def debug(self, msg, *args, **kwargs):
""" Log a message with DEBUG level. Automatically
includes stack info unless it is specifically not
included. """
kwargs["inc_stack_info"] = kwargs.get("inc_stack_info", True)
self.log(DEBUG, msg, args, **kwargs)
def error(self, msg, *args, **kwargs):
""" Log a message with ERROR level. Automatically
includes stack and process info unless they
are specifically not included. """
kwargs["inc_stack_info"] = kwargs.get("inc_stack_info", True)
kwargs["inc_multi_proc"] = kwargs.get("inc_multi_proc", True)
self.log(ERROR, msg, args, **kwargs)
def critical(self, msg, *args, **kwargs):
""" Log a message with CRITICAL level. Automatically
includes stack and process info unless they are
specifically not included. """
kwargs["inc_stack_info"] = kwargs.get("inc_stack_info", True)
kwargs["inc_multi_proc"] = kwargs.get("inc_multi_proc", True)
self.log(CRITICAL, msg, args, **kwargs)
def multi(self, lvl_list, msg, *args, **kwargs):
""" Log a message at multiple levels"""
for level in lvl_list:
self.log(level, msg, args, **kwargs)
def all(self, msg, *args, **kwargs):
""" Log a message at every known log level """
self.multi(ALL, msg, args, **kwargs)
def findCaller(self):
"""
Find the stack frame of the caller so that we can note the source
file name, line number, and function name
"""
rv = ("(unknown file)", 0, "(unknown function)", "(code not available)", [], None)
f = inspect.currentframe()
while hasattr(f, "f_code"):
co = f.f_code
filename = os.path.normcase(co.co_filename)
if filename == _srcfile:
f = f.f_back # get out of this logging file
continue
sinfo = traceback.extract_stack(f)
fname, lno, fnc, cc, i = inspect.getframeinfo(f, context=10)
cc[i] = ">" + cc[i] # mark the exact line
code = cc[i]
rv = (fname, lno, fnc, code, cc, sinfo)
break
return rv
@Coroutine
def Printer(open_file=sys.stdout, closing=False):
""" Prints items with a timestamp. """
try:
while True:
logstr = (yield)
open_file.write(logstr)
open_file.write('\n') # new line
except GeneratorExit:
if closing:
try: open_file.close()
except: pass
def StderrPrinter():
""" Prints items to stderr. """
return Printer(open_file=sys.stderr, closing=False)
def FilePrinter(filename, mode='a', closing=True):
path = os.path.abspath(os.path.expanduser(filename))
""" Opens the given file and returns a printer to it. """
f = open(path, mode)
return Printer(f, closing)
import socket
@Coroutine
def SocketWriter(host, port, af=socket.AF_INET, st=socket.SOCK_STREAM):
""" Writes messages to a socket/host. """
message = "({0}): {1}"
s = socket.socket(af, st)
s.connect(host, port)
try:
while True:
logstr = (yield)
s.send(logstr)
except GeneratorExit:
s.close()
import smtplib
@Coroutine
def Emailer(recipients, sender=None):
""" Sends messages as emails to the given list
of recipients. """
hostname = socket.gethostname()
if not sender:
sender = "lggr@{}".format(hostname)
smtp = smtplib.SMTP('localhost')
try:
while True:
logstr = (yield)
try:
smtp.sendmail(sender, recipients, logstr)
except smtplib.SMTPException:
pass
except GeneratorExit:
smtp.quit()
@Coroutine
def GMailer(recipients, username, password, subject="Log message from lggr.py"):
""" Sends messages as emails to the given list
of recipients, from a GMail account. """
srvr = smtplib.SMTP("smtp.gmail.com", 587)
srvr.ehlo()
srvr.starttls()
srvr.ehlo()
srvr.login(username, password)
if not (isinstance(recipients, list) or isinstance(recipients, tuple)):
recipients = [recipients]
gmail_sender = "{}@gmail.com".format(username)
msg = 'To: {}\nFrom: '+gmail_sender+'\nSubject: '+subject+'\n'
msg = msg + '\n{}\n\n'
try:
while True:
logstr = (yield)
for rcp in recipients:
message = msg.format(rcp, logstr)
try:
srvr.sendmail(gmail_sender, rcp, message)
except smtplib.SMTPException:
pass
except GeneratorExit:
srvr.quit()