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): """