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.
# 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

View File

@ -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"

View File

@ -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()

View File

@ -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 = "/"

View File

@ -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()

View File

@ -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)