diff --git a/src/borgmanager/database/borgdatabase.py b/src/borgmanager/database/borgdatabase.py index 09a6798..39bda86 100644 --- a/src/borgmanager/database/borgdatabase.py +++ b/src/borgmanager/database/borgdatabase.py @@ -4,7 +4,8 @@ from pathlib import Path class BorgDatabase(object): - def __init__(self, db_path: Path): + def __init__(self, db_path: Path, log): + self.log = log self.repo_name = "repo" self.archive_name = "archive" self.cache_name = "cache" @@ -27,16 +28,19 @@ class BorgDatabase(object): # region INSERT def insert_record(self, repo, archive, cache, label): + self.log.debug("Inserting record") repo_id = self.repo_conn.insert(repo) self.insert_label(label, repo_id=repo_id) archive_id = self.archive_conn.insert(archive, repo_id=repo_id) self.cache_conn.insert(cache, archive_id=archive_id) def insert_error(self, borg_error, label): + self.log.debug("Inserting error") label_id = self.insert_label(label) self.error_conn.insert(borg_error, label_id=label_id) def insert_label(self, label, repo_id=None): + self.log.debug("Inserting label") return self.label_conn.insert(Label(label), repo_id=repo_id) # endregion diff --git a/src/borgmanager/logging/__init__.py b/src/borgmanager/logging/__init__.py new file mode 100644 index 0000000..b045466 --- /dev/null +++ b/src/borgmanager/logging/__init__.py @@ -0,0 +1,7 @@ +from .log import Log + +LEVEL_DEBUG = 1 +LEVEL_INFO = 2 +LEVEL_WARNING = 3 +LEVEL_ERROR = 4 +LEVEL_CRITICAL = 5 diff --git a/src/borgmanager/logging/log.py b/src/borgmanager/logging/log.py new file mode 100644 index 0000000..7919095 --- /dev/null +++ b/src/borgmanager/logging/log.py @@ -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) diff --git a/src/main.py b/src/main.py index e6cdd6a..58db925 100644 --- a/src/main.py +++ b/src/main.py @@ -5,29 +5,36 @@ from pathlib import Path import argparse from borgmanager.borg import OutputHandler 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: output_path = Path(args.dir) if not output_path.exists(): output_path.mkdir() path = output_path - db = BorgDatabase(path / 'borg.sqlite') + log.debug(f"Path: {path}") + db = BorgDatabase(path / 'borg.sqlite', log) if args.summary: + log.debug("args.summary") summary = Summary(db) print(summary.repo_stats()) else: + log.debug("reading from stdin") borg_output = " ".join(stdin.readlines()) if args.label is None: + log.error("No label supplied") raise Exception("No label supplied") else: bo = OutputHandler(borg_output) if bo.error: + log.debug("processing error") db.insert_error(bo.get_borg_error(), args.label) else: + log.debug("processing borg json") db.insert_record(*bo.get_borg_info(), args.label) @@ -42,4 +49,5 @@ def get_args(): if __name__ == "__main__": m_args = get_args() m_path = Path(realpath(__file__)).parent.parent - main(m_args, m_path) + m_log = Log(LEVEL_DEBUG) + main(m_args, m_path, m_log)