Skip to content

Commit

Permalink
cleanup on log initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Apr 16, 2016
1 parent 5af208a commit f66e1c8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
45 changes: 32 additions & 13 deletions libscanbuild/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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:]
Expand Down
8 changes: 4 additions & 4 deletions libscanbuild/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libscanbuild/intercept.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down

0 comments on commit f66e1c8

Please sign in to comment.