57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
'''
|
|
manages logging events of the CryptoBox
|
|
'''
|
|
import sys
|
|
|
|
class CryptoBoxLogger:
|
|
'''
|
|
handles concrete logging events and prints them e.g. to a logfile
|
|
'''
|
|
|
|
DebugLevels = {"debug":0, "info":3, "warn":6, "error":9}
|
|
DebugFacilities = {"file":0}
|
|
|
|
def __init__(self, level, facility, name=None):
|
|
self.debug_level = int(level)
|
|
if int(facility) == self.DebugFacilities["file"]:
|
|
self.logFunc = self.message2file
|
|
if name is not None:
|
|
self.logFile = name
|
|
else:
|
|
self.logFile = '/var/log/cryptobox.log'
|
|
try:
|
|
fsock = open(self.logFile, "a")
|
|
fsock.close()
|
|
return
|
|
except IOError:
|
|
sys.stderr.write("Unable to open logfile (%s) for writing.\n" % (self.logFile, ))
|
|
else:
|
|
sys.stderr.write("Invalid logging facility: %d.\n" % (facility, ))
|
|
"we will only arrive here, if an error occoured"
|
|
sys.stderr.write("Sorry - bye, bye!\n")
|
|
sys.exit(1)
|
|
|
|
|
|
def printMessage(self, text, msg_level=None):
|
|
if msg_level is None: msg_level = self.DebugLevels["debug"]
|
|
if msg_level >= self.debug_level:
|
|
self.logFunc("[CryptoBox] - %s\n" % (text, ))
|
|
|
|
|
|
def message2file(self, text):
|
|
try:
|
|
log_sock = open(self.logFile, "a")
|
|
try:
|
|
log_sock.writelines(text)
|
|
log_sock.close()
|
|
return
|
|
except IOError:
|
|
sys.stderr.write(
|
|
"Unable to write messages to logfile (%s).\n" % (self.logFile, ))
|
|
except IOError:
|
|
sys.stderr.write("Unable to open logfile (%s) for writing.\n" % (self.logFile, ))
|
|
"we will only arrive here, if an error occoured"
|
|
sys.stderr.write("Sorry - bye, bye!\n")
|
|
sys.exit(1)
|
|
|
|
|