From a48b9e565a62053c6e06a939ca0f8cadf6693994 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Tue, 10 Mar 2026 12:28:07 +0100 Subject: [PATCH] PySimLogger: add parameter to set initial log-level/verbosity When we initialize a new PySimLogger, we always call the setup method first and then use the set_verbose and set_level method to configure the initial log level and the initial log verbosity. However, we initialize the PySimLogger in all our programs the same way and we end up with the same boilerplate code every time. Let's add a keyword parameter to the setup method where we can pass our opts.verbose (bool) parameter so that the setup method can do the work for the main program. In case the caller wants a different default configuration he still can call set_verbose and set_level methods as needed. Change-Id: I4b8ef1e203186878910c9614a1d900d5759236a8 --- contrib/csv-to-pgsql.py | 5 +---- pySim-shell.py | 10 ++-------- pySim/log.py | 12 +++++++++++- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/contrib/csv-to-pgsql.py b/contrib/csv-to-pgsql.py index 416d8706..a805ab8a 100755 --- a/contrib/csv-to-pgsql.py +++ b/contrib/csv-to-pgsql.py @@ -285,10 +285,7 @@ if __name__ == '__main__': option_parser.add_argument("--admin", action='store_true', help="perform action as admin", default=False) opts = option_parser.parse_args() - PySimLogger.setup(print, {logging.WARN: "\033[33m"}) - if (opts.verbose): - PySimLogger.set_verbose(True) - PySimLogger.set_level(logging.DEBUG) + PySimLogger.setup(print, {logging.WARN: "\033[33m"}, opts.verbose) # Open CSV file cr = open_csv(opts) diff --git a/pySim-shell.py b/pySim-shell.py index 50deea2a..89d011f1 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -107,12 +107,12 @@ Online manual available at https://downloads.osmocom.org/docs/pysim/master/html/ kwargs = {'include_ipy': True} self.verbose = verbose + PySimLogger.setup(self.poutput, {logging.WARN: YELLOW}) self._onchange_verbose('verbose', False, self.verbose); # pylint: disable=unexpected-keyword-arg super().__init__(persistent_history_file='~/.pysim_shell_history', allow_cli_args=False, auto_load_commands=False, startup_script=script, **kwargs) - PySimLogger.setup(self.poutput, {logging.WARN: YELLOW}) self.intro = style(self.BANNER, fg=RED) self.default_category = 'pySim-shell built-in commands' self.card = None @@ -1175,13 +1175,7 @@ if __name__ == '__main__': opts = option_parser.parse_args() # Ensure that we are able to print formatted warnings from the beginning. - PySimLogger.setup(print, {logging.WARN: YELLOW}) - if opts.verbose: - PySimLogger.set_verbose(True) - PySimLogger.set_level(logging.DEBUG) - else: - PySimLogger.set_verbose(False) - PySimLogger.set_level(logging.INFO) + PySimLogger.setup(print, {logging.WARN: YELLOW}, opts.verbose) # Register csv-file as card data provider, either from specified CSV # or from CSV file in home directory diff --git a/pySim/log.py b/pySim/log.py index 85397b6d..801ad69e 100644 --- a/pySim/log.py +++ b/pySim/log.py @@ -63,7 +63,7 @@ class PySimLogger: raise RuntimeError('static class, do not instantiate') @staticmethod - def setup(print_callback = None, colors:dict = {}): + def setup(print_callback = None, colors:dict = {}, verbose_debug:bool = False): """ Set a print callback function and color scheme. This function call is optional. In case this method is not called, default settings apply. @@ -72,10 +72,20 @@ class PySimLogger: have the following format: print_callback(message:str) colors : An optional dict through which certain log levels can be assigned a color. (e.g. {logging.WARN: YELLOW}) + verbose_debug: Enable verbose logging and set the loglevel DEBUG when set to true. Otherwise the + non-verbose logging is used and the loglevel is set to INFO. This setting can be changed + using the set_verbose and set_level methods at any time. """ PySimLogger.print_callback = print_callback PySimLogger.colors = colors + if (verbose_debug): + PySimLogger.set_verbose(True) + PySimLogger.set_level(logging.DEBUG) + else: + PySimLogger.set_verbose(False) + PySimLogger.set_level(logging.INFO) + @staticmethod def set_verbose(verbose:bool = False): """