-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.py
64 lines (56 loc) · 1.82 KB
/
log.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
from time import strftime
from sys import stdout
class Log:
"""
Nifty log file-wrapping object
Used for user logs
f: the file-like object to wrap
"""
def __init__(self):
self.files = {}
self.fixes = {}
#suffixes and prefixes
self.lnbuff = ""
def open(self, name, prefix="", suffix=""):
if type(name) is str:
f = open("Logs/" + name, "a")
else:
f = name
name = f.name
self.fixes[f] = (prefix, suffix)
self.files[name] = f
self.write("Log opened", f=name)
def write(self, *msgs, **options):
defaults = {"sep": " ", "end": "\n", "f": None}
options = dict(defaults, **options)
if not options["f"]:
options["f"] = self.files.keys()
else:
options["f"] = [options["f"]]
msg = options["sep"].join(map(str, msgs)) + options["end"]
for char in msg:
if char == "\n":
if self.lnbuff:
for f in options["f"]:
f = self.files[f]
prefix, suffix = self.fixes[f]
f.write("\r" + strftime("[%r] ") + prefix + self.lnbuff + suffix)
self.lnbuff = ""
else:
self.lnbuff += char
for f in self.files.values():
f.flush()
def close(self, names=None):
if not names:
names = self.files.keys()
else:
names = [names]
for name in names:
try:
self.write("Log closed.", f=name)
f = self.files.pop(name)
if f.name != "<stdout>":
f.close()
except KeyError:
out.write("[closing file] No file with name, %s exists" % name )
out = Log()