Made the logger an object to be multiprocessing compatible.

The initialization of the Configuration() object can not log anymore since the Log() object initialization requires the Configuration(). Maybe I will find a better solution in the future. For now, it fixes bugs and makes my life easier.
This commit is contained in:
Johannes Findeisen 2022-10-11 02:57:13 +02:00
commit e5cc2a4596
19 changed files with 201 additions and 211 deletions

View file

@ -20,7 +20,7 @@ class Linspectord:
try:
self.__pid_file = configuration.get_option('linspector', 'pid_file')
except Exception as err:
log('critical', 'daemonize error (no pid_file set): {0}'.str(format(err)))
log.critical('daemonize error (no pid_file set): {0}'.str(format(err)))
def daemonize(self):
# daemonize the class using the UNIX double fork mechanism.
@ -32,7 +32,7 @@ class Linspectord:
# exit first parent.
sys.exit(0)
except OSError as err:
self.__log('critical', 'fork #1 failed: {0}'.str(format(err)))
self.__log.critical('fork #1 failed: {0}'.str(format(err)))
sys.exit(1)
# decouple from parent environment.
@ -47,7 +47,7 @@ class Linspectord:
# Exit from second parent.
sys.exit(0)
except OSError as err:
self.__log('critical', 'fork #2 failed: {0}'.str(format(err)))
self.__log.critical('fork #2 failed: {0}'.str(format(err)))
sys.exit(1)
# redirect standard file descriptors.
@ -73,7 +73,7 @@ class Linspectord:
def start(self):
# start the daemon. check for a pidfile to see if the daemon already runs before.
self.__log('info', 'starting daemon using pid_file: ' + str(self.__pid_file))
self.__log.info('starting daemon using pid_file: ' + str(self.__pid_file))
try:
with open(self.__pid_file, 'r') as pf:
pid = int(pf.read().strip())
@ -82,7 +82,7 @@ class Linspectord:
if pid:
message = 'pid_file {0} already exist. daemon already running?'
self.__log('critical', str(message.format(self.__pid_file)))
self.__log.critical(str(message.format(self.__pid_file)))
sys.exit(1)
# start the daemon.
@ -91,7 +91,7 @@ class Linspectord:
def stop(self):
# stop the daemon.
self.__log('info', 'stopping daemon using pid_file: ' + str(self.__pid_file))
self.__log.info('stopping daemon using pid_file: ' + str(self.__pid_file))
# get the pid from the pid file.
try:
with open(self.__pid_file, 'r') as pf:
@ -101,7 +101,7 @@ class Linspectord:
if not pid:
message = 'pid_file {0} does not exist. daemon not running?'
self.__log('error', str(message.format(self.__pid_file)))
self.__log.error(str(message.format(self.__pid_file)))
return # not an error in a restart
# try killing the daemon process.
@ -115,12 +115,12 @@ class Linspectord:
if os.path.exists(self.__pid_file):
os.remove(self.__pid_file)
else:
self.__log('critical', str(err.args))
self.__log.critical(str(err.args))
sys.exit(1)
def restart(self):
# restart the daemon.
self.__log('info', 'restarting daemon using pid_file: ' + str(self.__pid_file))
self.__log.info('restarting daemon using pid_file: ' + str(self.__pid_file))
self.stop()
self.start()