Skip to content

Commit

Permalink
Move the TMT_DEBUG to click
Browse files Browse the repository at this point in the history
Also make the bootstrap_logger failsafe

Signed-off-by: Cristian Le <[email protected]>
  • Loading branch information
LecrisUT committed Aug 6, 2024
1 parent 04001ca commit 0e47e56
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
38 changes: 12 additions & 26 deletions tmt/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,6 @@ def create_decolorizer(apply_colors: bool) -> Callable[[str], str]:
return tmt.utils.remove_color


def _debug_level_from_global_envvar() -> int:
import tmt.utils

raw_value = os.getenv('TMT_DEBUG', None)

if raw_value is None:
return 0

try:
return int(raw_value)

except ValueError:
raise tmt.utils.GeneralError(f"Invalid debug level '{raw_value}', use an integer.")


def decide_colorization(no_color: bool, force_color: bool) -> tuple[bool, bool]:
"""
Decide whether the output and logging should be colorized.
Expand Down Expand Up @@ -623,19 +608,13 @@ def apply_verbosity_options(
else:
self.verbosity_level = verbosity_level

debug_level_from_global_envvar = _debug_level_from_global_envvar()
debug_level_from_option = cast(Optional[int], actual_kwargs.get('debug', None))

if debug_level_from_global_envvar not in (None, 0):
self.debug_level = debug_level_from_global_envvar
if debug_level_from_option is None or debug_level_from_option == 0:
pass

else:
debug_level_from_option = cast(Optional[int], actual_kwargs.get('debug', None))

if debug_level_from_option is None or debug_level_from_option == 0:
pass

else:
self.debug_level = debug_level_from_option
self.debug_level = debug_level_from_option

quietness_level = actual_kwargs.get('quiet', False)

Expand Down Expand Up @@ -855,7 +834,14 @@ def get_bootstrap_logger(cls) -> 'Logger':
# Stay away of our future main logger
actual_logger = Logger._normalize_logger(logging.getLogger('_tmt_bootstrap'))

cls._bootstrap_logger = Logger.create(actual_logger=actual_logger)
# The environment variables are usually handled at the click cli stage
# Here we enable safe parsing of those variables.
try:
debug = int(os.getenv('TMT_DEBUG', 0))
except ValueError:
debug = None

cls._bootstrap_logger = Logger.create(actual_logger=actual_logger, debug=debug)
cls._bootstrap_logger.add_console_handler()

return cls._bootstrap_logger
2 changes: 1 addition & 1 deletion tmt/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def option(
'-v', '--verbose', count=True, default=0,
help='Show more details. Use multiple times to raise verbosity.'),
option(
'-d', '--debug', count=True, default=0,
'-d', '--debug', count=True, default=0, envvar="TMT_DEBUG",
help='Provide debugging information. Repeat to see more details.'),
option(
'-q', '--quiet', is_flag=True,
Expand Down

0 comments on commit 0e47e56

Please sign in to comment.