From f66e1c8de98ce5bfb4ba6d73fdd9b9998fe24b99 Mon Sep 17 00:00:00 2001 From: Laszlo Nagy Date: Sat, 16 Apr 2016 23:36:05 +1000 Subject: [PATCH] cleanup on log initialization --- libscanbuild/__init__.py | 45 ++++++++++++++++++++++++++++----------- libscanbuild/analyze.py | 8 +++---- libscanbuild/intercept.py | 4 ++-- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/libscanbuild/__init__.py b/libscanbuild/__init__.py index d0049b6..1eada86 100644 --- a/libscanbuild/__init__.py +++ b/libscanbuild/__init__.py @@ -40,28 +40,45 @@ def tempdir(): return os.getenv('TMPDIR', os.getenv('TEMP', os.getenv('TMP', '/tmp'))) -def initialize_logging(verbose_level): - """ Output content controlled by the verbosity level. """ +def reconfigure_logging(verbose_level): + """ Logging level and format reconfigured based on the verbose flag. """ - level = logging.WARNING - min(logging.WARNING, (10 * verbose_level)) + # exit when nothing to do + if verbose_level == 0: + return + root = logging.getLogger() + # tune level + level = logging.WARNING - min(logging.WARNING, (10 * verbose_level)) + root.setLevel(level) + # be verbose with messages if verbose_level <= 3: - fmt_string = '{0}: %(levelname)s: %(message)s' + fmt_string = '%(name)s: %(levelname)s: %(message)s' else: - fmt_string = '{0}: %(levelname)s: %(funcName)s: %(message)s' - - program = os.path.basename(sys.argv[0]) - logging.basicConfig(format=fmt_string.format(program), level=level) + fmt_string = '%(name)s: %(levelname)s: %(funcName)s: %(message)s' + handler = logging.StreamHandler() + handler.setFormatter(logging.Formatter(fmt=fmt_string)) + root.handlers = [handler] def command_entry_point(function): - """ Decorator for command entry methods. """ + """ Decorator for command entry methods. + + The decorator initialize/shutdown logging and guard on programming + errors (catch exceptions). + + The decorated method can have arbitrary parameters, the return value will + be the exit code of the process. """ @functools.wraps(function) def wrapper(*args, **kwargs): + """ Do housekeeping tasks and execute the wrapped method. """ exit_code = 127 try: + logging.basicConfig(format='%(name)s: %(message)s', + level=logging.WARNING) + logging.getLogger().name = os.path.basename(sys.argv[0]) exit_code = function(*args, **kwargs) except KeyboardInterrupt: logging.warning('Keyboard interupt') @@ -72,8 +89,9 @@ def wrapper(*args, **kwargs): "to the bug report") else: logging.error("Please run this command again and turn on " - "verbose mode (add '-vvv' as argument).") + "verbose mode (add '-vvvv' as argument).") finally: + logging.shutdown() return exit_code return wrapper @@ -98,12 +116,13 @@ def wrapper_entry_point(function): def wrapper(): """ It executes the compilation and calls the wrapped method. """ + compiler_wrapper_name = os.path.basename(sys.argv[0]) # initialize wrapper logging - wrapper_name = os.path.basename(sys.argv[0]) - logging.basicConfig(format='{0}: %(message)s'.format(wrapper_name), + logging.basicConfig(format='%(name)s: %(message)s', level=os.getenv('INTERCEPT_BUILD_VERBOSE', 'INFO')) + logging.getLogger().name = compiler_wrapper_name # execute with real compiler - language = 'c++' if wrapper_name[-2:] == '++' else 'c' + language = 'c++' if compiler_wrapper_name[-2:] == '++' else 'c' compiler = os.getenv('INTERCEPT_BUILD_CC', 'cc') if language == 'c' \ else os.getenv('INTERCEPT_BUILD_CXX', 'c++') compilation = [compiler] + sys.argv[1:] diff --git a/libscanbuild/analyze.py b/libscanbuild/analyze.py index c765cc9..b597391 100644 --- a/libscanbuild/analyze.py +++ b/libscanbuild/analyze.py @@ -19,7 +19,7 @@ import logging import subprocess import multiprocessing -from libscanbuild import initialize_logging, tempdir +from libscanbuild import reconfigure_logging, tempdir from libscanbuild import command_entry_point, wrapper_entry_point from libscanbuild.runner import run, logging_analyzer_output from libscanbuild.intercept import capture @@ -39,12 +39,12 @@ def analyze_build_main(bin_dir, from_build_command): parser = create_parser(from_build_command) args = parser.parse_args() - validate(parser, args, from_build_command) - # setup logging - initialize_logging(args.verbose) + reconfigure_logging(args.verbose) logging.debug('Parsed arguments: %s', args) + validate(parser, args, from_build_command) + with report_directory(args.output, args.keep_empty) as target_dir: if not from_build_command: # run analyzer only and generate cover report diff --git a/libscanbuild/intercept.py b/libscanbuild/intercept.py index 73769a5..4e3df09 100644 --- a/libscanbuild/intercept.py +++ b/libscanbuild/intercept.py @@ -32,7 +32,7 @@ import subprocess from libear import build_libear, TemporaryDirectory from libscanbuild import command_entry_point, wrapper_entry_point -from libscanbuild import duplicate_check, tempdir, initialize_logging +from libscanbuild import duplicate_check, tempdir, reconfigure_logging from libscanbuild.compilation import split_command from libscanbuild.shell import encode, decode @@ -53,7 +53,7 @@ def intercept_build_main(bin_dir): parser = create_parser() args = parser.parse_args() - initialize_logging(args.verbose) + reconfigure_logging(args.verbose) logging.debug('Parsed arguments: %s', args) if not args.build: