Skip to content

Commit

Permalink
Changed instantiation of logger (for better import without settings)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegue committed Apr 15, 2024
1 parent baafed8 commit e87f6d0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion common/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from abc import ABCMeta

from common.ioctl import IOCTL
from logger import logger
import logger


class WatchdogError(Exception):
Expand Down
27 changes: 16 additions & 11 deletions logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
from common.helper import Singleton


warn = info = debug = error = exception = None


class Logger(object):
def __init__(self, log_file_name="main_log.log", log_format="%(asctime)s %(levelname)s %(message)s", terminator=None, print_stdout=True):
log_settings = settings.Settings().Logging
log_file_dir = log_settings.LogFileDir
log_formatter = logging.Formatter(log_format)
if log_file_dir is None:
log_file_dir = "."
if not os.path.exists(log_file_dir):
os.makedirs(log_file_dir)
my_handler = RotatingFileHandler(filename=log_file_dir + "/" + log_file_name, mode=log_settings.Mode, maxBytes=log_settings.MaxFileSize, backupCount=log_settings.MaxBackupFiles, encoding=None, delay=0)
Expand Down Expand Up @@ -64,14 +69,14 @@ def exception_to_string(exception_instance: Exception, with_stack_trace=True):


class GeneralLogger(Logger, metaclass=Singleton):
pass


warn = info = debug = error = exception = None
if warn is info is error is debug is None:
logger = GeneralLogger()
warn = logger.warn
info = logger.info
debug = logger.debug
error = logger.error
exception = logger.exception
def __init__(self, *args, **kwargs):
global warn, info, debug, error, exception
super().__init__(*args, **kwargs)

# Put message-functions on "global" scope
if warn is info is error is debug is None:
warn = self.warn
info = self.info
debug = self.debug
error = self.error
exception = self.exception
5 changes: 2 additions & 3 deletions main_file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import datetime

import curio
import logger
import json
import argparse
import inspect

import curio_wrapper
import logger
import device
from nmea import nmea
import device_io
Expand Down Expand Up @@ -113,6 +111,7 @@ async def main(devices_path):


if __name__ == '__main__':
logger.GeneralLogger() # Instantiate logger once
parser = argparse.ArgumentParser(description='NMEA-Seatalk-Multiplexer.')
parser.add_argument('--devices', default="devices.json", help='Path to json-file containing needed information for creating devices')

Expand Down
8 changes: 6 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ def _get_kernel():
return test_kernel


@pytest.fixture(scope="session")
def kernel_fixture():
return _get_kernel()


@pytest.fixture(scope="session", autouse=True)
def session_start_fixture():
logger.GeneralLogger()


@pytest.fixture(scope="function", autouse=True)
def log_function():
def log_function(session_start_fixture):
"""
Logs currently running name of test-function
"""
Expand Down

0 comments on commit e87f6d0

Please sign in to comment.