forked from Matir/scorebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.py
executable file
·120 lines (92 loc) · 3.43 KB
/
Logger.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
'''
Created on Dec 27, 2011
@author: [email protected]
Logger.py is a module in the scorebot program. It's purpose is to write logs for all other modules.
Copyright (C) 2011 Dichotomy
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
'''
import os
import os.path
import time
import Queue
import threading
class Logger(object):
def __init__(self, basename, path="logs"):
self.basename = basename
if os.path.exists(path):
pass
else:
os.mkdir(path)
self.outfilename = "%s/%s.out" % (path, self.basename)
self.errfilename = "%s/%s.err" % (path, self.basename)
self.outfile = open(self.outfilename, "a")
self.errfile = open(self.errfilename, "a")
def out(self, message):
now = time.strftime('%X %x %Z')
self.outfile.write("%s|%s" % (now, message))
self.outfile.flush()
def write(self, message):
self.err(message)
def err(self, message):
now = time.strftime('%X %x %Z')
self.errfile.write("%s|%s" % (now, message))
self.errfile.flush()
def get_err_file(self):
return self.errfile
def get_out_file(self):
return self.outfile
class QueueP(Queue.Queue):
def __init__(self):
Queue.Queue.__init__(self, "QueueP")
def write(self, msg):
self.put(msg)
class ThreadedLogger(threading.Thread):
def __init__(self, basename, oqueue, equeue, path="logs"):
threading.Thread.__init__(self)
self.basename = basename
self.oqueue = oqueue
self.equeue = equeue
if os.path.exists(path):
pass
else:
os.mkdir(path)
self.outfilename = "%s/%s.out" % (path, self.basename)
self.errfilename = "%s/%s.err" % (path, self.basename)
self.outfile = open(self.outfilename, "a")
self.errfile = open(self.errfilename, "a")
def run(self):
while True:
try:
e_item = self.equeue.get(False)
if e_item:
self.err(e_item)
o_item = self.oqueue.get(False)
if o_item:
self.out(o_item)
except Queue.Empty:
time.sleep(0.01)
pass
def out(self, message):
now = time.strftime('%X %x %Z')
self.outfile.write("%s|%s" % (now, message))
self.outfile.flush()
def write(self, message):
self.err(message)
def err(self, message):
now = time.strftime('%X %x %Z')
self.errfile.write("%s|%s" % (now, message))
self.errfile.flush()
def get_err_file(self):
return self.errfile
def get_out_file(self):
return self.outfile