fixed logging issue and another bug

There was a problem with indentation which made the line starting adhoc
not being run at all.
Updated CHANGELOG
Moved some things to other places
log.py is back! It isn't needed, but it doesn't do any harm anyway.
This commit is contained in:
Pranav Jerry 2021-09-07 18:45:11 +05:30
parent 68b53ccad1
commit ca1e721c9e
Signed by: pranav
GPG Key ID: F1DCDC4FED0A0C5B
8 changed files with 66 additions and 49 deletions

View File

@ -1,9 +1,11 @@
# Changelog
## [Unreleased][] - 2021-09-06
## [Unreleased][] - 2021-09-07
- naxalnet is now a daemon! naxalnet will reconfigure the WiFi network
every time a WiFi adapter is plugged in or removed
- **Logging**: logs to systemd journal when run from systemd, stderr
otherwise.
otherwise
- New dependency `python3-systemd`
- Fixed dependency order in systemd service
- Added `--verbose` argument

View File

@ -18,13 +18,12 @@ network.
<!-- NOTE TO ACTIVISTS
Running this program in the world's largest (partly-free) democracy
may result in you getting arrested under the UAPA, and not
getting bail because of false evidence planted in your phone by
may result in you getting arrested under the Act Which Must Not Be Named,
and not getting bail because of false evidence planted in your phone by
Pegasus, or by a forensic lab in Gujarat.
The author, much like the Government of India, does not wish
to take responsibility in your well-being if you get arrested under
a draconian national security law.
The author, much like the GoI, does not wish to take responsibility in your
well-being if you get arrested under a draconian national security law.
-->
<!-- UNCOMMENT WHEN NECESSARY
@ -32,7 +31,7 @@ a draconian national security law.
**Disclaimer**:
In case you are either 1) a complete idiot; or 2) a member of the saffron
brigade; or 3) both, please be aware that this project is not affiliated
with any groups designated as "terrorist" groups in India.
with any human rights groups designated as "terrorist" groups in India.
Using the name naxal does not imply any form of connection
with anyone currently at risk of death in overcrowded prisons.

View File

@ -35,4 +35,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.dev4"
__version__ = "0.3.0a2.dev5"

View File

@ -171,3 +171,6 @@ def parse_args() -> Namespace:
# logger.debug("Parsing arguments")
return parser.parse_args()
args = parse_args()

View File

@ -21,11 +21,9 @@ daemon.py
The daemon part. This is currently under construction.
"""
import logging
from dasbus.loop import EventLoop
from naxalnet.iwd import IWD, IWD_DEVICE_INTERFACE
logger = logging.getLogger(__name__)
from naxalnet.log import logger
class Daemon:

View File

@ -56,10 +56,9 @@ and what they mean:
- node: a machine that runs naxalnet and is therefore
connected to the mesh.
"""
import logging
from dasbus.connection import SystemMessageBus
logger = logging.getLogger(__name__)
from dasbus.connection import SystemMessageBus
from naxalnet.log import logger
IWD_BUS = "net.connman.iwd"
IWD_ROOT_PATH = "/"
@ -236,6 +235,7 @@ class Device:
if it isn't already on ad-hoc and power onn the device
if it is off
"""
print("Starting adhoc", name)
if self.get_mode() != "ad-hoc":
self.set_mode("ad-hoc")
@ -270,9 +270,7 @@ class Device:
# Stop ap if already started
self.stop_ap()
logger.debug(
"Starting ap on %s with ssid %s and password %s", self.name, ssid, passwd
)
logger.debug("Starting ap on %s with ssid %s", self.name, ssid)
self._proxy.Start(ssid, passwd)
def stop_ap(self):

38
naxalnet/log.py Normal file
View File

@ -0,0 +1,38 @@
"""
log.py
------
Initialise the logger for other submodules to import. Do not
import any submodules here except for naxallnet.config
"""
import logging
from systemd.journal import JournalHandler
from naxalnet.config import args
def get_logger():
"""
Initialise the logger and return it.
This function is meant to be used only by naxalnet.log.
If you want to import the logger, use:
from naxalnet.log import logger
"""
log = logging.getLogger("naxalnet")
# --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:
logging.basicConfig(level=logging.DEBUG)
log.addHandler(JournalHandler())
else:
logging.basicConfig(level=loglevel)
return log
logger = get_logger()

View File

@ -27,23 +27,16 @@ When run from the commandline, the function main() is called.
"""
import sys
import logging
from pathlib import Path
from shutil import copy
from dasbus.error import DBusError
from systemd import journal
from systemd.daemon import notify
from naxalnet import __version__
from naxalnet.log import logger
from naxalnet.iwd import Adapter, Device, IWD
from naxalnet.config import parse_args
from naxalnet.config import args
from naxalnet.daemon import Daemon
# Do not use getLogger(__name__) here.
# getLogger() without any args will give us
# the root logger, which is waht we need.
logger = logging.getLogger()
args = parse_args()
def copy_files():
"""
@ -105,16 +98,17 @@ def setup_devices():
# So we will remove adhoc_device from ap_devices if it exists there
if adhoc_device.name in ap_devices:
ap_devices.remove(adhoc_device.name)
logger.info("Starting mesh on %s", adhoc_device.name)
# Turn on adapter if it is off
# See issue #9
# Turn on adapter if it is off
# See issue #9
adhoc_adapter = Adapter(adhoc_device.adapter)
if not adhoc_adapter.is_powered_on():
logger.debug("Adapter %s is off. Turning on", adhoc_adapter.name)
adhoc_adapter.power_on()
adhoc_device.start_adhoc_open(args.adhoc_name)
# Start Access point if ap_device is not empty,
# ie, we have more devices
logger.info("Starting mesh on %s", adhoc_device.name)
adhoc_device.start_adhoc_open(args.adhoc_name)
# Start Access point if ap_device is not empty,
# ie, we have more devices
if len(ap_devices) != 0:
ap_device = Device(ap_devices.pop())
logger.info("Starting WiFi Access Point on %s", ap_device.name)
@ -125,7 +119,7 @@ def setup_devices():
if not ap_adapter.is_powered_on():
logger.debug("Adapter %s is off. Turning on", ap_adapter.name)
ap_adapter.power_on()
ap_device.start_ap(args.ap_ssid, args.ap_passwd)
ap_device.start_ap(args.ap_ssid, args.ap_passwd)
else:
logger.warning("Not setting up WiFi AP.")
else:
@ -166,21 +160,6 @@ def main():
elif args.version:
print_version()
sys.exit(0)
# --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:
logging.basicConfig(level=logging.DEBUG)
logger.addHandler(journal.JournalHandler())
else:
logging.basicConfig(level=loglevel)
copy_files()
setup_devices()