laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/42304?usp=email )
Change subject: PySimLogger: add parameter to set initial log-level/verbosity ......................................................................
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 --- M contrib/csv-to-pgsql.py M pySim-shell.py M pySim/log.py 3 files changed, 14 insertions(+), 13 deletions(-)
Approvals: laforge: Looks good to me, approved fixeria: Looks good to me, but someone else must approve Jenkins Builder: Verified
diff --git a/contrib/csv-to-pgsql.py b/contrib/csv-to-pgsql.py index 416d870..a805ab8 100755 --- a/contrib/csv-to-pgsql.py +++ b/contrib/csv-to-pgsql.py @@ -285,10 +285,7 @@ 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 2bfc720..98c31d8 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -107,12 +107,12 @@ 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 @@ -1176,13 +1176,7 @@ 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 85397b6..801ad69 100644 --- a/pySim/log.py +++ b/pySim/log.py @@ -63,7 +63,7 @@ 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 @@ 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): """