Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmsg.py
More file actions
Latest commit
147 lines (114 loc) · 3.58 KB
/
Copy pathmsg.py
File metadata and controls
147 lines (114 loc) · 3.58 KB
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
importos
importtime
try:
from . importsharedasG
assertG
unicode=str
from .exc_fmtimportstr_e
python2=False
exceptImportError:
python2=True
fromexc_fmtimportstr_e
importsharedasG
LOG_LEVELS= {
'DEBUG': 1,
'MSG': 2,
'WARN': 3,
'ERROR': 4,
}
LOG_LEVELS_REVERSE= {
1: 'DEBUG',
2: 'MSG',
3: 'WARN',
4: 'ERROR',
}
LOG_LEVEL=LOG_LEVELS['MSG']
LOG_FILE=os.path.join(G.BASE_DIR, 'msgs.floobits.log')
try:
fd=open(LOG_FILE, 'w')
fd.close()
exceptExceptionase:
pass
defsafe_print(msg):
# Some environments can have trouble printing unicode:
# "When print() is not outputting to the terminal (being redirected to
# a file, for instance), print() decides that it does not know what
# locale to use for that file and so it tries to convert to ASCII instead."
# See: https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-3-inconsistent-treatment-of-output
try:
print(msg)
exceptUnicodeEncodeError:
print(msg.encode('utf-8'))
# Overridden by each editor
defeditor_log(msg):
safe_print(msg)
deffloobits_log(msg):
# TODO: ridiculously inefficient
try:
fd=open(LOG_FILE, 'ab')
fmsg=msg
try:
fmsg=fmsg.encode('utf-8')
exceptException:
pass
fd.write(fmsg)
fd.write(b'\n')
fd.close()
exceptExceptionase:
safe_print(str_e(e))
classMSG(object):
# Default to LOG_LEVEL MSG
def__init__(self, msg, timestamp=None, username=None, level=2):
self.msg=msg
self.timestamp=timestamportime.time()
self.username=username
self.level=level
defdisplay(self):
ifself.level<LOG_LEVEL:
return
msg=unicode(self)
ifG.LOG_TO_CONSOLEorG.CHAT_VIEWisNone:
floobits_log(msg)
safe_print(msg)
else:
editor_log(msg)
def__str__(self):
ifpython2:
returnself.__unicode__().encode('utf-8')
returnself.__unicode__()
def__unicode__(self):
ifself.username:
msg='[{time}] {level}: <{user}> {msg}'
else:
msg='[{time}] {level}: {msg}'
level=LOG_LEVELS_REVERSE.get(self.level, 'UNKNOWN').rjust(5)
try:
returnunicode(msg).format(level=level, user=self.username, time=time.ctime(self.timestamp), msg=self.msg)
exceptUnicodeEncodeError:
returnunicode(msg).format(level=level, user=self.username, time=time.ctime(self.timestamp), msg=self.msg.encode(
'utf-8'))
defmsg_format(message, *args, **kwargs):
try:
message=unicode(message)
exceptUnicodeEncodeError:
message=str(message)
forarginargs:
try:
message+=unicode(arg)
exceptUnicodeEncodeError:
message+=arg
ifkwargs:
message=message.format(**kwargs)
returnmessage
def_log(message, level, *args, **kwargs):
iflevel>=LOG_LEVEL:
# TODO: kill MSG class and just format and print the thing right away
MSG(msg_format(message, *args, **kwargs), level=level).display()
defdebug(message, *args, **kwargs):
_log(message, LOG_LEVELS['DEBUG'], *args, **kwargs)
deflog(message, *args, **kwargs):
_log(message, LOG_LEVELS['MSG'], *args, **kwargs)
defwarn(message, *args, **kwargs):
_log(message, LOG_LEVELS['WARN'], *args, **kwargs)
deferror(message, *args, **kwargs):
_log(message, LOG_LEVELS['ERROR'], *args, **kwargs)