WIP add proper logging

This commit is contained in:
Marcel Otte 2020-01-06 13:26:52 +01:00
parent b12d85e7f1
commit 52f52a61c7
4 changed files with 57 additions and 23 deletions

View File

@ -3,11 +3,26 @@
""" """
Service startup script. Service startup script.
""" """
import sys
import os
import asyncio import asyncio
import logging
from backive.core.events import EventInterface from backive.core.events import EventInterface
from backive.config.config import Config from backive.config.config import Config
rootlogger = logging.getLogger()
rootlogger.setLevel(logging.DEBUG)
consoleHandler = logging.StreamHandler(sys.stdout)
fileHandler = logging.FileHandler("/var/log/backive/backive.log")
if not os.path.exists("/var/log/backive"):
os.makedirs("/var/log/backive")
rootlogger.addHandler(consoleHandler)
rootlogger.addHandler(fileHandler)
logging.info("Backive starting.")
class Backive: class Backive:
def __init__(self): def __init__(self):
self._config = Config() self._config = Config()
@ -22,7 +37,17 @@ class Backive:
loop.run_forever() loop.run_forever()
pass pass
def __del__(self):
del self._events
if __name__ == "__main__": if __name__ == "__main__":
backive = None
try:
backive = Backive() backive = Backive()
backive.serve() backive.serve()
except Exception as e:
raise e
finally:
logging.info("Backive exited.")
del backive

View File

@ -1,3 +1,4 @@
import logging
import os import os
import pwd import pwd
from ruamel.yaml import YAML from ruamel.yaml import YAML
@ -9,11 +10,13 @@ from backive.core.device import Device
class Config: class Config:
__shared_state = dict __shared_state = dict()
_config = dict()
def __init__(self): def __init__(self):
self.__dict__ = self.__shared_state self.__dict__ = self.__shared_state
self._config = dict() if not self._config:
logging.info("Loading configuration...")
self._schema = dict() self._schema = dict()
self._backups = list() self._backups = list()
self._devices = list() self._devices = list()

View File

@ -5,17 +5,18 @@ import asyncio
class EventInterface: class EventInterface:
def __init__(self, event_callback, unix_socket_path=None, loop=None): def __init__(self, event_callback, unix_socket_path=None, loop=None):
self.event_callback = event_callback self.event_callback = event_callback
if not unix_socket_path: self.unix_socket_path = unix_socket_path
unix_socket_path = "/tmp/backive/backive.sock" if not self.unix_socket_path:
if not os.path.exists(os.path.dirname(unix_socket_path)): self.unix_socket_path = "/tmp/backive/backive.sock"
os.makedirs(os.path.dirname(unix_socket_path)) if not os.path.exists(os.path.dirname(self.unix_socket_path)):
os.makedirs(os.path.dirname(self.unix_socket_path))
try: try:
os.remove(unix_socket_path) os.remove(self.unix_socket_path)
except OSError: except OSError:
pass pass
if not loop: if not loop:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.create_task(asyncio.start_unix_server(self.client_connected, unix_socket_path)) loop.create_task(asyncio.start_unix_server(self.client_connected, self.unix_socket_path))
async def client_connected(self, reader, writer): async def client_connected(self, reader, writer):
print("client_connected") print("client_connected")
@ -23,4 +24,6 @@ class EventInterface:
data = (await reader.read()).decode('utf8') data = (await reader.read()).decode('utf8')
await self.event_callback(data) await self.event_callback(data)
def __del__(self):
print("Removing socket file...")
os.remove(self.unix_socket_path)

View File

@ -1,5 +1,6 @@
import os import os
import json import json
import logging
from datetime import datetime from datetime import datetime
@ -12,8 +13,10 @@ class Scheduler():
# who are we? # who are we?
uid = os.getuid() uid = os.getuid()
if uid == 0: if uid == 0:
logging.info("Executing as root.")
self._data_file = "/var/lib/backive/data.json" self._data_file = "/var/lib/backive/data.json"
else: else:
logging.info("Executing as user.")
self._data_file = os.path.join( self._data_file = os.path.join(
os.path.expanduser("~"), os.path.expanduser("~"),
".config", ".config",