From 4c81247bc980f65fef7d8a5a9b344cd553869263 Mon Sep 17 00:00:00 2001 From: dachshund Date: Fri, 13 Sep 2013 17:34:51 -0400 Subject: [PATCH] Offer option to remove console handler; fix variable shadow bug. The rest of the integration tests will need updates due to the fix of the variable shadow bug. --- tuf/log.py | 47 ++++++++++++++++++++++++++---------- tuf/tests/util_test_tools.py | 11 +++------ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/tuf/log.py b/tuf/log.py index 77487e36..7987ec94 100755 --- a/tuf/log.py +++ b/tuf/log.py @@ -78,11 +78,11 @@ logging.Formatter.converter = time.gmtime formatter = logging.Formatter(_FORMAT_STRING) -# Set the handlers for the logger. The console handler is unset by default. A +# Set the handlers for the logger. The console handler is unset by default. A # module importing 'log.py' should explicitly set the console handler if -# outputting log messages to the screen is needed. Adding a console handler -# can be done with tuf.log.add_console_handler(). Logging messages to a file -# *is* set by default. +# outputting log messages to the screen is needed. Adding a console handler can +# be done with tuf.log.add_console_handler(). Logging messages to a file *is* +# set by default. console_handler = None # Set the built-in file handler. Messages will be logged to @@ -275,16 +275,37 @@ def add_console_handler(log_level=_DEFAULT_CONSOLE_LOG_LEVEL): # Raise 'tuf.FormatError' if there is a mismatch. tuf.formats.LENGTH_SCHEMA.check_match(log_level) - # Set the console handler for the logger. The built-in console handler will - # log messages to 'sys.stderr' and capture 'log_level' messages. - console_handler = logging.StreamHandler() - # Get our filter for the console handler. - console_filter = ConsoleFilter() + if not console_handler: + # Set the console handler for the logger. The built-in console handler will + # log messages to 'sys.stderr' and capture 'log_level' messages. + # NOTE: This is not thread-safe. + console_handler = logging.StreamHandler() + # Get our filter for the console handler. + console_filter = ConsoleFilter() - console_handler.setLevel(log_level) - console_handler.setFormatter(formatter) - console_handler.addFilter(console_filter) - logger.addHandler(console_handler) + console_handler.setLevel(log_level) + console_handler.setFormatter(formatter) + console_handler.addFilter(console_filter) + logger.addHandler(console_handler) + logger.debug('Added a console handler.') + else: + logger.warn('We already have a console handler.') + + + + + +def remove_console_handler(): + # Assign to the global console_handler object. + global console_handler + + if console_handler: + logger.removeHandler(console_handler) + # NOTE: This is not thread-safe. + console_handler = None + logger.debug('Removed a console handler.') + else: + logger.warn('We do not have a console handler.') diff --git a/tuf/tests/util_test_tools.py b/tuf/tests/util_test_tools.py index d79cfeed..c259b103 100644 --- a/tuf/tests/util_test_tools.py +++ b/tuf/tests/util_test_tools.py @@ -154,11 +154,7 @@ tuf_configurations = None -def disable_console_logging(): - tuf.log.logger.removeHandler(tuf.log.console_handler) - - -def init_repo(tuf=False, port=None): +def init_repo(using_tuf=False, port=None): # Temp root directory for regular and tuf repositories. # WARNING: tuf client stores files in '{root_repo}/downloads/' directory! # Make sure regular download are NOT stored in the that directory when @@ -188,8 +184,9 @@ def init_repo(tuf=False, port=None): time.sleep(.2) keyids = None - if tuf: - disable_console_logging() + if using_tuf: + # We remove the console handler so that tests are silent by default. + tuf.log.remove_console_handler() keyids = init_tuf(root_repo) create_interposition_config(root_repo, url)