diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 793f2d7..bbc4bb7 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -1108,9 +1108,10 @@ def common(): meter.v = v meter.powerOn() + log_set = None if args.slog_out: # Setup loggers - LogSet(client, args.slog_out if args.slog_out != 'default' else None, meter) + log_set = LogSet(client, args.slog_out if args.slog_out != 'default' else None, meter) have_tunnel = platform.system() == "Linux" if ( @@ -1122,6 +1123,9 @@ def common(): except KeyboardInterrupt: logging.info("Exiting due to keyboard interrupt") + if log_set: + log_set.close() + # don't call exit, background threads might be running still # sys.exit(0) diff --git a/meshtastic/slog/slog.py b/meshtastic/slog/slog.py index d2cc41f..9bee592 100644 --- a/meshtastic/slog/slog.py +++ b/meshtastic/slog/slog.py @@ -162,12 +162,17 @@ class LogSet: else: self.power_logger = None + # Store a lambda so we can find it again to unregister + self.atexit_handler = lambda: self.close() atexit.register(self.close) def close(self) -> None: """Close the log set.""" - logging.info(f"Closing slogs in {self.dir_name}") - self.slog_logger.close() - if self.power_logger: - self.power_logger.close() + if self.slog_logger: + logging.info(f"Closing slogs in {self.dir_name}") + atexit.unregister(self.atexit_handler) # docs say it will silently ignore if not found + self.slog_logger.close() + if self.power_logger: + self.power_logger.close() + self.slog_logger = None