added --systemd and --verbose arguments

When run without --systemd, naxalnet will log to stderr.
Otherwise, it will log to systemd journal.

log.py is no longer needed.
This commit is contained in:
Pranav Jerry 2021-09-06 14:57:54 +05:30
parent a3e9b45050
commit 2d9aee4d3a
No known key found for this signature in database
GPG Key ID: F1DCDC4FED0A0C5B
6 changed files with 56 additions and 7 deletions

View File

@ -27,7 +27,7 @@ RestartSec=2sec
# naxalnet cannot start a mesh network but exits without errors. # naxalnet cannot start a mesh network but exits without errors.
# So, we give a 2s delay. # So, we give a 2s delay.
ExecStartPre=/usr/bin/sleep 2 ExecStartPre=/usr/bin/sleep 2
ExecStart=/usr/bin/naxalnet ExecStart=/usr/bin/naxalnet --systemd
# Reload systemd-networkd after naxalnet exits # Reload systemd-networkd after naxalnet exits
ExecStartPost=/usr/bin/networkctl reload ExecStartPost=/usr/bin/networkctl reload
# Delete all files starting with mesh.* in /run/systemd/network # Delete all files starting with mesh.* in /run/systemd/network

View File

@ -36,4 +36,4 @@ See README.md for documentation.
# #
# In case you forgot to change the version, skip the number # In case you forgot to change the version, skip the number
# and put the next number in the next commit. # and put the next number in the next commit.
__version__ = "0.3.0a2.dev1" __version__ = "0.3.0a2.dev2"

View File

@ -139,6 +139,13 @@ def parse_args() -> Namespace:
help="volatile directory where configuration files of systemd-networkd should be copied", 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( parser.add_argument(
"--version", "--version",
default=False, default=False,
@ -146,5 +153,9 @@ def parse_args() -> Namespace:
help="prints the version and exit", help="prints the version and exit",
) )
parser.add_argument(
"-v", "--verbose", action="count", default=0, help="increase output verbosity"
)
# logger.debug("Parsing arguments") # logger.debug("Parsing arguments")
return parser.parse_args() return parser.parse_args()

View File

@ -58,8 +58,11 @@ and what they mean:
- node: a machine that runs naxalnet and is therefore - node: a machine that runs naxalnet and is therefore
connected to the mesh. connected to the mesh.
""" """
import logging
from dasbus.connection import SystemMessageBus 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_BUS = "net.connman.iwd"
IWD_ROOT_PATH = "/" IWD_ROOT_PATH = "/"

View File

@ -10,7 +10,22 @@ to the systemd journal
import logging import logging
from systemd import journal from systemd import journal
from naxalnet.config import parse_args
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) def get_logger():
logger.addHandler(journal.JournalHandler()) """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()

View File

@ -28,13 +28,18 @@ When run from the commandline, the function here_be_dragons() is called.
""" """
import sys import sys
import logging
from pathlib import Path from pathlib import Path
from shutil import copy from shutil import copy
from dasbus.error import DBusError from dasbus.error import DBusError
from systemd import journal
from naxalnet import __version__ from naxalnet import __version__
from naxalnet.iwd import Adapter, Device, IWD from naxalnet.iwd import Adapter, Device, IWD
from naxalnet.config import parse_args from naxalnet.config import parse_args
from naxalnet.log import logger
# from naxalnet.log import logger
logger = logging.getLogger(__name__)
def copy_files(args): def copy_files(args):
@ -144,6 +149,21 @@ def here_be_dragons():
""" """
args = parse_args() 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: if args.print_wifi:
print_wifi(args) print_wifi(args)
sys.exit(0) sys.exit(0)