Refactor: Add log.error and log.exception to reduce code duplication.

pull/3214/head
Jukka Laurila 7 years ago committed by Jussi Pakkanen
parent f6fd03485e
commit 52c50da6c7
  1. 5
      docs/markdown/snippets/altered-logging.md
  2. 2
      mesonbuild/dependencies/boost.py
  3. 5
      mesonbuild/mconf.py
  4. 14
      mesonbuild/mesonmain.py
  5. 29
      mesonbuild/mlog.py
  6. 6
      mesonbuild/rewriter.py

@ -0,0 +1,5 @@
## Log output slightly changed
The format of some human-readable diagnostic messages has changed in
minor ways. In case you are parsing these messages, you may need to
adjust your code.

@ -167,7 +167,7 @@ class BoostDependency(ExternalDependency):
invalid_modules = [x for x in invalid_modules if x not in remove] invalid_modules = [x for x in invalid_modules if x not in remove]
if invalid_modules: if invalid_modules:
mlog.log(mlog.red('ERROR:'), 'Invalid Boost modules: ' + ', '.join(invalid_modules)) mlog.error('Invalid Boost modules: ' + ', '.join(invalid_modules))
return True return True
else: else:
return False return False

@ -249,9 +249,8 @@ def run(args):
if save: if save:
c.save() c.save()
except ConfException as e: except ConfException as e:
print('Meson configurator encountered an error:\n') print('Meson configurator encountered an error:')
print(e) raise e
return 1
return 0 return 0

@ -304,7 +304,7 @@ def run(original_args, mainfile=None):
try: try:
return mconf.run(remaining_args) return mconf.run(remaining_args)
except MesonException as e: except MesonException as e:
mlog.log(mlog.red('\nError configuring project:'), e) mlog.exception(e)
sys.exit(1) sys.exit(1)
elif cmd_name == 'wrap': elif cmd_name == 'wrap':
return wraptool.run(remaining_args) return wraptool.run(remaining_args)
@ -324,8 +324,8 @@ def run(original_args, mainfile=None):
try: try:
sys.exit(run_script_command(args[1:])) sys.exit(run_script_command(args[1:]))
except MesonException as e: except MesonException as e:
mlog.log(mlog.red('\nError in {} helper script:'.format(script))) mlog.error('\nError in {} helper script:'.format(script))
mlog.log(e) mlog.exception(e)
sys.exit(1) sys.exit(1)
args = args[2:] args = args[2:]
handshake = True handshake = True
@ -368,13 +368,7 @@ def run(original_args, mainfile=None):
app.generate() app.generate()
except Exception as e: except Exception as e:
if isinstance(e, MesonException): if isinstance(e, MesonException):
mlog.log() mlog.exception(e)
if hasattr(e, 'file') and hasattr(e, 'lineno') and hasattr(e, 'colno'):
mlog.log('%s:%d:%d:' % (e.file, e.lineno, e.colno), mlog.red('ERROR: '), end='')
else:
mlog.log(mlog.red('ERROR: '), end='')
# Error message
mlog.log(e)
# Path to log file # Path to log file
mlog.shutdown() mlog.shutdown()
logfile = os.path.join(app.build_dir, environment.Environment.log_dir, mlog.log_fname) logfile = os.path.join(app.build_dir, environment.Environment.log_dir, mlog.log_fname)

@ -102,19 +102,38 @@ def log(*args, **kwargs):
arr = process_markup(args, True) arr = process_markup(args, True)
force_print(*arr, **kwargs) force_print(*arr, **kwargs)
def warning(*args, **kwargs): def _log_error(severity, *args, **kwargs):
from . import environment from . import environment
if severity == 'warning':
args = (yellow('WARNING:'),) + args args = (yellow('WARNING:'),) + args
elif severity == 'error':
args = (red('ERROR:'),) + args
else:
assert False, 'Invalid severity ' + severity
if 'location' in kwargs: if 'location' in kwargs:
location = kwargs['location'] location = kwargs['location']
del kwargs['location'] del kwargs['location']
location = '{}:{}:'.format(os.path.join(location.subdir, environment.build_filename), location.lineno) location_str = '{}:{}:'.format(os.path.join(location.subdir,
args = (location,) + args environment.build_filename),
location.lineno)
args = (location_str,) + args
log(*args, **kwargs) log(*args, **kwargs)
def error(*args, **kwargs):
return _log_error('error', *args, **kwargs)
def warning(*args, **kwargs):
return _log_error('warning', *args, **kwargs)
def exception(e):
log()
if hasattr(e, 'file') and hasattr(e, 'lineno') and hasattr(e, 'colno'):
log('%s:%d:%d:' % (e.file, e.lineno, e.colno), red('ERROR: '), e)
else:
log(red('ERROR:'), e)
# Format a list for logging purposes as a string. It separates # Format a list for logging purposes as a string. It separates
# all but the last item with commas, and the last with 'and'. # all but the last item with commas, and the last with 'and'.
def format_list(list): def format_list(list):

@ -54,11 +54,7 @@ def run(args):
sys.exit('Unknown command: ' + options.commands[0]) sys.exit('Unknown command: ' + options.commands[0])
except Exception as e: except Exception as e:
if isinstance(e, MesonException): if isinstance(e, MesonException):
if hasattr(e, 'file') and hasattr(e, 'lineno') and hasattr(e, 'colno'): mlog.exception(e)
mlog.log(mlog.red('\nMeson encountered an error in file %s, line %d, column %d:' % (e.file, e.lineno, e.colno)))
else:
mlog.log(mlog.red('\nMeson encountered an error:'))
mlog.log(e)
else: else:
traceback.print_exc() traceback.print_exc()
return 1 return 1

Loading…
Cancel
Save