Add log, and debug statements

This commit is contained in:
George Lacey 2021-05-07 10:35:22 +01:00
parent 091073cacb
commit 6a6024663f
4 changed files with 48 additions and 4 deletions

View File

@ -4,7 +4,8 @@ from pathlib import Path
class BorgDatabase(object): class BorgDatabase(object):
def __init__(self, db_path: Path): def __init__(self, db_path: Path, log):
self.log = log
self.repo_name = "repo" self.repo_name = "repo"
self.archive_name = "archive" self.archive_name = "archive"
self.cache_name = "cache" self.cache_name = "cache"
@ -27,16 +28,19 @@ class BorgDatabase(object):
# region INSERT # region INSERT
def insert_record(self, repo, archive, cache, label): def insert_record(self, repo, archive, cache, label):
self.log.debug("Inserting record")
repo_id = self.repo_conn.insert(repo) repo_id = self.repo_conn.insert(repo)
self.insert_label(label, repo_id=repo_id) self.insert_label(label, repo_id=repo_id)
archive_id = self.archive_conn.insert(archive, repo_id=repo_id) archive_id = self.archive_conn.insert(archive, repo_id=repo_id)
self.cache_conn.insert(cache, archive_id=archive_id) self.cache_conn.insert(cache, archive_id=archive_id)
def insert_error(self, borg_error, label): def insert_error(self, borg_error, label):
self.log.debug("Inserting error")
label_id = self.insert_label(label) label_id = self.insert_label(label)
self.error_conn.insert(borg_error, label_id=label_id) self.error_conn.insert(borg_error, label_id=label_id)
def insert_label(self, label, repo_id=None): def insert_label(self, label, repo_id=None):
self.log.debug("Inserting label")
return self.label_conn.insert(Label(label), repo_id=repo_id) return self.label_conn.insert(Label(label), repo_id=repo_id)
# endregion # endregion

View File

@ -0,0 +1,7 @@
from .log import Log
LEVEL_DEBUG = 1
LEVEL_INFO = 2
LEVEL_WARNING = 3
LEVEL_ERROR = 4
LEVEL_CRITICAL = 5

View File

@ -0,0 +1,25 @@
from . import LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARNING, LEVEL_ERROR, LEVEL_CRITICAL
class Log(object):
def __init__(self, level=LEVEL_WARNING):
self.level = LEVEL_WARNING
def output(self, level, message):
if self.level <= level:
print(message)
def debug(self, message):
return self.output(LEVEL_DEBUG, message)
def info(self, message):
return self.output(LEVEL_INFO, message)
def warn(self, message):
return self.output(LEVEL_WARNING, message)
def error(self, message):
return self.output(LEVEL_ERROR, message)
def critical(self, message):
return self.output(LEVEL_CRITICAL, message)

View File

@ -5,29 +5,36 @@ from pathlib import Path
import argparse import argparse
from borgmanager.borg import OutputHandler from borgmanager.borg import OutputHandler
from borgmanager.summary import Summary from borgmanager.summary import Summary
from borgmanager.logging import Log, LEVEL_DEBUG
def main(args, path: Path): def main(args, path: Path, log: Log):
if args.dir is not None: if args.dir is not None:
output_path = Path(args.dir) output_path = Path(args.dir)
if not output_path.exists(): if not output_path.exists():
output_path.mkdir() output_path.mkdir()
path = output_path path = output_path
db = BorgDatabase(path / 'borg.sqlite') log.debug(f"Path: {path}")
db = BorgDatabase(path / 'borg.sqlite', log)
if args.summary: if args.summary:
log.debug("args.summary")
summary = Summary(db) summary = Summary(db)
print(summary.repo_stats()) print(summary.repo_stats())
else: else:
log.debug("reading from stdin")
borg_output = " ".join(stdin.readlines()) borg_output = " ".join(stdin.readlines())
if args.label is None: if args.label is None:
log.error("No label supplied")
raise Exception("No label supplied") raise Exception("No label supplied")
else: else:
bo = OutputHandler(borg_output) bo = OutputHandler(borg_output)
if bo.error: if bo.error:
log.debug("processing error")
db.insert_error(bo.get_borg_error(), args.label) db.insert_error(bo.get_borg_error(), args.label)
else: else:
log.debug("processing borg json")
db.insert_record(*bo.get_borg_info(), args.label) db.insert_record(*bo.get_borg_info(), args.label)
@ -42,4 +49,5 @@ def get_args():
if __name__ == "__main__": if __name__ == "__main__":
m_args = get_args() m_args = get_args()
m_path = Path(realpath(__file__)).parent.parent m_path = Path(realpath(__file__)).parent.parent
main(m_args, m_path) m_log = Log(LEVEL_DEBUG)
main(m_args, m_path, m_log)