From 1ac5eb1db0dd3950904660c5a4317445d9bbf1ac Mon Sep 17 00:00:00 2001 From: Bruno Faria Date: Wed, 2 Jul 2025 12:58:01 -0300 Subject: [PATCH] update: Added logging facility to the simulator * Use logging + log level insted of prints * Use debug messages for critial computations --- sharc/main_cli.py | 36 +++++++++++++++++++---------------- sharc/simulation.py | 7 +++++++ sharc/support/logging.yaml | 8 ++++---- sharc/support/sharc_logger.py | 4 +++- sharc/thread_simulation.py | 2 +- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/sharc/main_cli.py b/sharc/main_cli.py index bd2cba75c..7d2e5ddb6 100644 --- a/sharc/main_cli.py +++ b/sharc/main_cli.py @@ -12,6 +12,8 @@ import sys import getopt import os +import argparse +import logging sys.path.append(os.path.join(os.path.dirname(__file__), "..")) @@ -31,23 +33,25 @@ def main(argv): param_file = '' - try: - opts, args = getopt.getopt(argv, "hp:") - except getopt.GetoptError: - print("usage: main_cli.py -p ") - sys.exit(2) + parser = argparse.ArgumentParser(description="Run the SHARC command-line interface.") + parser.add_argument( + "-p", "--param_file", + type=str, + required=True, + help="Path to the parameter file (required)" + ) + parser.add_argument( + "-l", "--log_level", + type=str, + choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], + default="INFO", + help="Set the logging level (default: INFO)" + ) + args = parser.parse_args(argv) + param_file = os.path.abspath(args.param_file) - if not opts: - param_file = os.path.join(os.getcwd(), "input", "parameters.yaml") - else: - for opt, arg in opts: - if opt == "-h": - print("usage: main_cli.py -p ") - sys.exit() - elif opt == "-p": - param_file = param_file = os.path.join(os.getcwd(), arg) - - Logging.setup_logging() + log_level = getattr(logging, args.log_level.upper(), logging.INFO) + Logging.setup_logging(default_level=log_level) model = Model() view_cli = ViewCli() diff --git a/sharc/simulation.py b/sharc/simulation.py index b1ce5249b..60fd0befb 100644 --- a/sharc/simulation.py +++ b/sharc/simulation.py @@ -12,6 +12,7 @@ import math import sys import matplotlib.pyplot as plt +from logging import getLogger from sharc.support.enumerations import StationType from sharc.topology.topology_factory import TopologyFactory @@ -21,6 +22,8 @@ from sharc.results import Results from sharc.propagation.propagation_factory import PropagationFactory +logger = getLogger(__name__) + class Simulation(ABC, Observable): """ @@ -218,6 +221,8 @@ def initialize(self, *args, **kwargs): This method is executed only once to initialize the simulation variables. """ + logger.debug("Initializing the simulation") + self.topology.calculate_coordinates() self.initialize_topology_dependant_variables() @@ -263,6 +268,8 @@ def finalize(self, *args, **kwargs): snapshot_number = kwargs["snapshot_number"] self.results.write_files(snapshot_number) + logger.debug("Finalizing the simulation") + def calculate_coupling_loss_system_imt( self, system_station: StationManager, diff --git a/sharc/support/logging.yaml b/sharc/support/logging.yaml index 0e04fc413..595736cb1 100644 --- a/sharc/support/logging.yaml +++ b/sharc/support/logging.yaml @@ -4,13 +4,14 @@ disable_existing_loggers: False formatters: simple: format: "%(message)s" -# format: "%(asctime)s - %(name)s - %(message)s" + detailed: + class: logging.Formatter + format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s" handlers: console: class: logging.StreamHandler - level: INFO - formatter: simple + formatter: detailed stream: ext://sys.stdout file: @@ -20,7 +21,6 @@ handlers: maxBytes: 10485760 # 10MB backupCount: 20 encoding: utf8 - root: level: INFO diff --git a/sharc/support/sharc_logger.py b/sharc/support/sharc_logger.py index 78dccbaab..07b686902 100644 --- a/sharc/support/sharc_logger.py +++ b/sharc/support/sharc_logger.py @@ -17,7 +17,8 @@ class Logging: @staticmethod def setup_logging( default_path='support/logging.yaml', - default_level=logging.INFO, env_key='LOG_CFG', + default_level=logging.INFO, + env_key='LOG_CFG', ): """Set up logging configuration for the application.""" path = default_path @@ -27,6 +28,7 @@ def setup_logging( if os.path.exists(path): with open(path, 'rt') as f: config = yaml.safe_load(f.read()) + config['root']['level'] = logging.getLevelName(default_level) logging.config.dictConfig(config) else: logging.basicConfig(level=default_level) diff --git a/sharc/thread_simulation.py b/sharc/thread_simulation.py index 192570daa..9ddfb877f 100644 --- a/sharc/thread_simulation.py +++ b/sharc/thread_simulation.py @@ -43,7 +43,7 @@ def is_stopped(self) -> bool: ------- True if simulation is stopped """ - return self.stop_flag.isSet() + return self.stop_flag.is_set() def run(self): """