diff --git a/naxalnet.service b/naxalnet.service index 4280cfd..78cbee3 100644 --- a/naxalnet.service +++ b/naxalnet.service @@ -27,7 +27,7 @@ RestartSec=2sec # naxalnet cannot start a mesh network but exits without errors. # So, we give a 2s delay. ExecStartPre=/usr/bin/sleep 2 -ExecStart=/usr/bin/naxalnet +ExecStart=/usr/bin/naxalnet --systemd # Reload systemd-networkd after naxalnet exits ExecStartPost=/usr/bin/networkctl reload # Delete all files starting with mesh.* in /run/systemd/network diff --git a/naxalnet/__init__.py b/naxalnet/__init__.py index 2160232..15a69d5 100644 --- a/naxalnet/__init__.py +++ b/naxalnet/__init__.py @@ -36,4 +36,4 @@ See README.md for documentation. # # In case you forgot to change the version, skip the number # and put the next number in the next commit. -__version__ = "0.3.0a2.dev1" +__version__ = "0.3.0a2.dev2" diff --git a/naxalnet/config.py b/naxalnet/config.py index 711166b..2c72b5f 100644 --- a/naxalnet/config.py +++ b/naxalnet/config.py @@ -139,6 +139,13 @@ def parse_args() -> Namespace: help="volatile directory where configuration files of systemd-networkd should be copied", ) + parser.add_argument( + "--systemd", + action="store_true", + default=False, + help="send log messages to systemd journal", + ) + parser.add_argument( "--version", default=False, @@ -146,5 +153,9 @@ def parse_args() -> Namespace: help="prints the version and exit", ) + parser.add_argument( + "-v", "--verbose", action="count", default=0, help="increase output verbosity" + ) + # logger.debug("Parsing arguments") return parser.parse_args() diff --git a/naxalnet/iwd.py b/naxalnet/iwd.py index e15d660..e85e990 100644 --- a/naxalnet/iwd.py +++ b/naxalnet/iwd.py @@ -58,8 +58,11 @@ and what they mean: - node: a machine that runs naxalnet and is therefore connected to the mesh. """ +import logging from dasbus.connection import SystemMessageBus -from naxalnet.log import logger + +# from naxalnet.log import logger +logger = logging.getLogger(__name__) IWD_BUS = "net.connman.iwd" IWD_ROOT_PATH = "/" diff --git a/naxalnet/log.py b/naxalnet/log.py index 75c2a9c..1aa93a2 100644 --- a/naxalnet/log.py +++ b/naxalnet/log.py @@ -10,7 +10,22 @@ to the systemd journal import logging from systemd import journal +from naxalnet.config import parse_args -logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) -logger.addHandler(journal.JournalHandler()) + +def get_logger(): + """returns the logger object for logging""" + + args = parse_args() + if args.systemd: + logger = logging.getLogger(__name__) + logger.setLevel(logging.DEBUG) + logger.addHandler(journal.JournalHandler()) + else: + # logging.basicConfig(level=logging.DEBUG) + logger = logging.getLogger(__name__) + logger.setLevel(logging.DEBUG) + return logger + + +logger = get_logger() diff --git a/naxalnet/scripts.py b/naxalnet/scripts.py index 18086cf..6b7a6d7 100644 --- a/naxalnet/scripts.py +++ b/naxalnet/scripts.py @@ -28,13 +28,18 @@ When run from the commandline, the function here_be_dragons() is called. """ import sys +import logging from pathlib import Path from shutil import copy from dasbus.error import DBusError +from systemd import journal from naxalnet import __version__ from naxalnet.iwd import Adapter, Device, IWD from naxalnet.config import parse_args -from naxalnet.log import logger + +# from naxalnet.log import logger + +logger = logging.getLogger(__name__) def copy_files(args): @@ -144,6 +149,21 @@ def here_be_dragons(): """ args = parse_args() + # --verbose + if args.verbose >= 2: + loglevel = logging.DEBUG + elif args.verbose == 1: + loglevel = logging.INFO + else: + loglevel = logging.WARNING + + # if --systemd is given, log to systemd journal + if args.systemd: + logger.setLevel(level=logging.DEBUG) + logger.addHandler(journal.JournalHandler()) + else: + logging.basicConfig(level=loglevel) + if args.print_wifi: print_wifi(args) sys.exit(0)