mirror of
https://github.com/gryf/wicd.git
synced 2025-12-20 04:48:00 +01:00
Convert FlushWriter into LogWriter and optimise log writing.
FlushWriter looped through all the characters provided, writing them one at a time. This is not strictly necessary, so modify the algorithm to use a smart substitution and keep the timestamps correct. Name change to better match its behaviour.
This commit is contained in:
79
daemon.py
79
daemon.py
@@ -57,46 +57,65 @@ import misc
|
|||||||
|
|
||||||
logging_enabled = True
|
logging_enabled = True
|
||||||
|
|
||||||
class FlushWriter:
|
class LogWriter:
|
||||||
|
""" A class to provide timestamped logging. """
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
print os.getcwd()
|
self.file = open(wpath.log + 'wicd.log','a')
|
||||||
self.file = open(wpath.log + 'wicd.log','w')
|
self.eol = True
|
||||||
self.file.write(self.__getPrettyTime() + ' :: ')
|
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.file.close()
|
||||||
|
|
||||||
|
|
||||||
def write(self, data):
|
def write(self, data):
|
||||||
'''prepends a timestamp, writes the data, and then flushes the buffer'''
|
""" Writes the data to the log with a timestamp.
|
||||||
|
|
||||||
|
This function handles writing of data to a log file. In order to
|
||||||
|
handle output redirection, we need to be careful with how we
|
||||||
|
handle the addition of timestamps. In any set of data that is
|
||||||
|
written, we replace the newlines with a timestamp + new line,
|
||||||
|
except for newlines that are the final character in data.
|
||||||
|
|
||||||
|
When a newline is the last character in data, we set a flag to
|
||||||
|
indicate that the next write should have a timestamp prepended
|
||||||
|
as well, which ensures that the timestamps match the time at
|
||||||
|
which the data is written, rather than the previous write.
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
data -- The string to write to the log.
|
||||||
|
|
||||||
|
"""
|
||||||
global logging_enabled
|
global logging_enabled
|
||||||
|
if len(data) <= 0: return
|
||||||
if logging_enabled:
|
if logging_enabled:
|
||||||
|
if self.eol:
|
||||||
|
self.file.write(self.get_time() + ' :: ')
|
||||||
|
self.eol = False
|
||||||
|
|
||||||
#it appears that only one character at a time is written, but I don't trust it
|
if data[-1] == '\n':
|
||||||
#so if it isn't always one letter, make it so
|
self.eol = True
|
||||||
#this code should never run
|
data = data[:-1]
|
||||||
if len(data) > 1:
|
|
||||||
for letter in data:
|
|
||||||
self.write(letter)
|
|
||||||
return
|
|
||||||
|
|
||||||
if data == '\n':
|
|
||||||
self.file.write('\n' + self.__getPrettyTime() + ' :: ')
|
|
||||||
else:
|
|
||||||
self.file.write(str(data))
|
|
||||||
|
|
||||||
|
self.file.write(
|
||||||
|
data.replace('\n', '\n' + self.get_time() + ' :: '))
|
||||||
|
if self.eol: self.file.write('\n')
|
||||||
self.file.flush()
|
self.file.flush()
|
||||||
|
|
||||||
def __getPrettyTime(self):
|
|
||||||
'''generate a string with the time, and space numbers with 0s'''
|
def get_time(self):
|
||||||
|
""" Return a string with the current time nicely formatted.
|
||||||
|
|
||||||
|
The format of the returned string is yyyy/mm/dd HH:MM:SS
|
||||||
|
|
||||||
|
"""
|
||||||
x = time.localtime()
|
x = time.localtime()
|
||||||
#year/month/day hours:minutes:seconds
|
return ''.join([
|
||||||
pretty_time = str(x[0]).rjust(4,'0') + '/' + str(x[1]).rjust(2,'0') + '/'+str(x[2]).rjust(2,'0') + ' '+str(x[3]).rjust(2,'0') + ':' + str(x[4]).rjust(2,'0') + ':' + str(x[5]).rjust(2,'0')
|
str(x[0]).rjust(4,'0'), '/', str(x[1]).rjust(2,'0'), '/',
|
||||||
#probably don't need to pad the year, but it makes it consistent
|
str(x[2]).rjust(2,'0'), ' ', str(x[3]).rjust(2,'0'), ':',
|
||||||
return pretty_time
|
str(x[4]).rjust(2,'0'), ':', str(x[5]).rjust(2,'0')])
|
||||||
|
|
||||||
|
|
||||||
def flush(self):
|
|
||||||
'''flushes the buffer'''
|
|
||||||
self.file.flush()
|
|
||||||
|
|
||||||
class ConnectionWizard(dbus.service.Object):
|
class ConnectionWizard(dbus.service.Object):
|
||||||
|
|
||||||
########## VARIABLES AND STUFF
|
########## VARIABLES AND STUFF
|
||||||
@@ -1095,8 +1114,8 @@ if True: #for easy disabling
|
|||||||
|
|
||||||
#kill output
|
#kill output
|
||||||
#POI:500 stdout redirection
|
#POI:500 stdout redirection
|
||||||
output = FlushWriter()
|
output = LogWriter()
|
||||||
sys.stdout = output #open("data/wicd.log","w")
|
sys.stdout = output
|
||||||
sys.stderr = output
|
sys.stderr = output
|
||||||
|
|
||||||
print "---------------------------"
|
print "---------------------------"
|
||||||
|
|||||||
Reference in New Issue
Block a user