From 993baa47a79f7dbff6e76c313bb050110b253783 Mon Sep 17 00:00:00 2001 From: John Burwell Date: Thu, 27 Apr 2023 22:00:18 -0500 Subject: [PATCH] add some logging support --- rsbbs/__init__.py | 2 +- rsbbs/logging.py | 31 +++++++++++++++++++++++++++++ rsbbs/plugins/bye/plugin.py | 6 ++++-- rsbbs/plugins/delete/plugin.py | 8 +++++--- rsbbs/plugins/deletem/plugin.py | 5 +++-- rsbbs/plugins/heard/plugin.py | 5 +++-- rsbbs/plugins/list/plugin.py | 5 +++-- rsbbs/plugins/listm/plugin.py | 5 +++-- rsbbs/plugins/read/plugin.py | 7 ++++--- rsbbs/plugins/readm/plugin.py | 5 +++-- rsbbs/plugins/send/plugin.py | 9 ++++++--- rsbbs/plugins/sendp/plugin.py | 9 ++++++--- rsbbs/rsbbs.py | 35 ++++++++++++++++++++++++++++++++- 13 files changed, 106 insertions(+), 26 deletions(-) create mode 100644 rsbbs/logging.py diff --git a/rsbbs/__init__.py b/rsbbs/__init__.py index b43b2ca..555eef2 100644 --- a/rsbbs/__init__.py +++ b/rsbbs/__init__.py @@ -19,7 +19,7 @@ __all__ = ['config', 'console', 'controller', 'parser', 'pluginloader'] -__version__ = "0.2.0" +__version__ = "0.3.0" # from .config import Config # from .console import Console diff --git a/rsbbs/logging.py b/rsbbs/logging.py new file mode 100644 index 0000000..329dc53 --- /dev/null +++ b/rsbbs/logging.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# +# Really Simple BBS - a really simple BBS for ax.25 packet radio. +# Copyright (C) 2023 John Burwell +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import datetime +import logging + + +class Formatter(logging.Formatter): + def __init__(self, var): + super().__init__() + self.var = var + + def format(self, record): + now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + record.msg = f"{now} {record.levelname} {self.var} {record.msg}" + return super().format(record) diff --git a/rsbbs/plugins/bye/plugin.py b/rsbbs/plugins/bye/plugin.py index e7aab51..1465541 100644 --- a/rsbbs/plugins/bye/plugin.py +++ b/rsbbs/plugins/bye/plugin.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging + from rsbbs.console import Console from rsbbs.parser import Parser @@ -25,8 +27,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info(f"Plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -38,4 +39,5 @@ class Plugin(): def run(self, args) -> None: """Disconnect and exit.""" self.api.write_output("Bye!") + logging.info(f"{__name__} exiting") exit(0) diff --git a/rsbbs/plugins/delete/plugin.py b/rsbbs/plugins/delete/plugin.py index d7a3374..dea387f 100644 --- a/rsbbs/plugins/delete/plugin.py +++ b/rsbbs/plugins/delete/plugin.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging + import sqlalchemy import sqlalchemy.exc @@ -29,8 +31,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info(f"plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -48,10 +49,11 @@ class Plugin(): session.delete(message) session.commit() self.api.write_output(f"Deleted message #{number}") + logging.info(f"deleted message {number}") except sqlalchemy.exc.NoResultFound: self.api.write_output(f"Message not found.") except Exception as e: - print(e) + logging.error(e) def run(self, args) -> None: """Delete a message. diff --git a/rsbbs/plugins/deletem/plugin.py b/rsbbs/plugins/deletem/plugin.py index 964058d..0af85e0 100644 --- a/rsbbs/plugins/deletem/plugin.py +++ b/rsbbs/plugins/deletem/plugin.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging import sqlalchemy from rsbbs.console import Console @@ -28,8 +29,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info(f"plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -52,6 +52,7 @@ class Plugin(): count = len(messages) if count > 0: self.api.write_output(f"Deleted {count} messages") + logging.info(f"deleted {count} messages") else: self.api.write_output(f"No messages to delete.") except Exception as e: diff --git a/rsbbs/plugins/heard/plugin.py b/rsbbs/plugins/heard/plugin.py index abc9094..16b7082 100644 --- a/rsbbs/plugins/heard/plugin.py +++ b/rsbbs/plugins/heard/plugin.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging import subprocess from rsbbs.console import Console @@ -27,8 +28,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info("plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -45,6 +45,7 @@ class Plugin(): try: result = subprocess.run(['mheard'], capture_output=True, text=True) self.api.write_output(result.stdout) + logging.info("queried heard log") except FileNotFoundError: self.api.write_output(f"mheard utility not found.") except Exception as e: diff --git a/rsbbs/plugins/list/plugin.py b/rsbbs/plugins/list/plugin.py index efd48c5..81d254e 100644 --- a/rsbbs/plugins/list/plugin.py +++ b/rsbbs/plugins/list/plugin.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging import sqlalchemy from rsbbs.console import Console @@ -28,8 +29,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info("plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -52,6 +52,7 @@ class Plugin(): result = session.execute( statement, execution_options={"prebuffer_rows": True}) + logging.info("list messages") except Exception: raise return result diff --git a/rsbbs/plugins/listm/plugin.py b/rsbbs/plugins/listm/plugin.py index 40578e1..d9b54b2 100644 --- a/rsbbs/plugins/listm/plugin.py +++ b/rsbbs/plugins/listm/plugin.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging import sqlalchemy from rsbbs.console import Console @@ -28,8 +29,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info("plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -46,6 +46,7 @@ class Plugin(): result = session.execute( statement, execution_options={"prebuffer_rows": True}) + logging.info("list my messages") return result except Exception: raise diff --git a/rsbbs/plugins/read/plugin.py b/rsbbs/plugins/read/plugin.py index e88160e..a387cfe 100644 --- a/rsbbs/plugins/read/plugin.py +++ b/rsbbs/plugins/read/plugin.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging import sqlalchemy import sqlalchemy.exc @@ -29,8 +30,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info(f"plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -47,10 +47,11 @@ class Plugin(): Message.id == number) result = session.execute(statement).one() self.api.print_message(result) + logging.info(f"read message") except sqlalchemy.exc.NoResultFound: self.api.write_output(f"Message not found.") except Exception as e: - print(e) + logging.error(e) def run(self, args) -> None: """Read a message. diff --git a/rsbbs/plugins/readm/plugin.py b/rsbbs/plugins/readm/plugin.py index 27e2e24..4ee10df 100644 --- a/rsbbs/plugins/readm/plugin.py +++ b/rsbbs/plugins/readm/plugin.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging import sqlalchemy from rsbbs.console import Console @@ -28,8 +29,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info(f"plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -46,6 +46,7 @@ class Plugin(): result = session.execute( statement, execution_options={"prebuffer_rows": True}) + logging.info(f"read message") return result except Exception: raise diff --git a/rsbbs/plugins/send/plugin.py b/rsbbs/plugins/send/plugin.py index f187800..aebcf47 100644 --- a/rsbbs/plugins/send/plugin.py +++ b/rsbbs/plugins/send/plugin.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging + from rsbbs.console import Console from rsbbs.models import Message from rsbbs.parser import Parser @@ -26,8 +28,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info(f"plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -50,7 +51,9 @@ class Plugin(): is_private=is_private )) session.commit() - except Exception: + logging.info(f"sent message to {args.callsign.upper()}") + except Exception as e: + logging.error(f"error sending message: {e}") session.rollback() raise diff --git a/rsbbs/plugins/sendp/plugin.py b/rsbbs/plugins/sendp/plugin.py index 57a3b8a..e0075ce 100644 --- a/rsbbs/plugins/sendp/plugin.py +++ b/rsbbs/plugins/sendp/plugin.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import logging + from rsbbs.console import Console from rsbbs.models import Message from rsbbs.parser import Parser @@ -26,8 +28,7 @@ class Plugin(): def __init__(self, api: Console) -> None: self.api = api self.init_parser(api.parser) - if api.config.debug: - print(f"Plugin {__name__} loaded") + logging.info(f"plugin {__name__} loaded") def init_parser(self, parser: Parser) -> None: subparser = parser.subparsers.add_parser( @@ -50,7 +51,9 @@ class Plugin(): is_private=is_private )) session.commit() - except Exception: + logging.info(f"sent message to {args.callsign.upper()}") + except Exception as e: + logging.error(f"error sending message: {e}") session.rollback() raise diff --git a/rsbbs/rsbbs.py b/rsbbs/rsbbs.py index 36679fa..ab7144e 100755 --- a/rsbbs/rsbbs.py +++ b/rsbbs/rsbbs.py @@ -17,12 +17,14 @@ # along with this program. If not, see . import argparse +import logging import sys from rsbbs import __version__ from rsbbs.config import Config from rsbbs.console import Console from rsbbs.controller import Controller +from rsbbs.logging import Formatter def parse_args(): @@ -48,6 +50,14 @@ def parse_args(): group = argv_parser.add_mutually_exclusive_group(required=True) + # Log level: + group.add_argument( + '--log-level', + action='store', + default='ERROR', + dest='log_level', + help="Logging level") + # Show config option: group.add_argument( '--show-config', @@ -77,10 +87,31 @@ def parse_args(): def main(): + # Grab the invocation arguments + args = parse_args() + + # Start logging + if args.debug: + log_level = logging.DEBUG + else: + log_level = getattr(logging, args.log_level.upper()) + + logging.basicConfig(format='%(asctime)s %(message)s', + level=log_level) + logging.info(f"{__name__} started") + # Load configuration config = Config( app_name='rsbbs', - args=parse_args()) + args=args) + + # Add the calling station to the logs + logger = logging.getLogger() + handler = logging.StreamHandler() + handler.setFormatter(Formatter(config.calling_station)) + logger.addHandler(handler) + + logger.info("connected") # Init the controller controller = Controller(config) @@ -91,6 +122,8 @@ def main(): # Start the app console.run() + logging.info(f"{__name__} exiting") + if __name__ == "__main__": main()