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]
if invalid_modules:
mlog.log(mlog.red('ERROR:'), 'Invalid Boost modules: ' + ', '.join(invalid_modules))
mlog.error('Invalid Boost modules: ' + ', '.join(invalid_modules))
return True
else:
return False

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

@ -304,7 +304,7 @@ def run(original_args, mainfile=None):
try:
return mconf.run(remaining_args)
except MesonException as e:
mlog.log(mlog.red('\nError configuring project:'), e)
mlog.exception(e)
sys.exit(1)
elif cmd_name == 'wrap':
return wraptool.run(remaining_args)
@ -324,8 +324,8 @@ def run(original_args, mainfile=None):
try:
sys.exit(run_script_command(args[1:]))
except MesonException as e:
mlog.log(mlog.red('\nError in {} helper script:'.format(script)))
mlog.log(e)
mlog.error('\nError in {} helper script:'.format(script))
mlog.exception(e)
sys.exit(1)
args = args[2:]
handshake = True
@ -368,13 +368,7 @@ def run(original_args, mainfile=None):
app.generate()
except Exception as e:
if isinstance(e, MesonException):
mlog.log()
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)
mlog.exception(e)
# Path to log file
mlog.shutdown()
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)
force_print(*arr, **kwargs)
def warning(*args, **kwargs):
def _log_error(severity, *args, **kwargs):
from . import environment
args = (yellow('WARNING:'),) + args
if severity == 'warning':
args = (yellow('WARNING:'),) + args
elif severity == 'error':
args = (red('ERROR:'),) + args
else:
assert False, 'Invalid severity ' + severity
if 'location' in kwargs:
location = kwargs['location']
del kwargs['location']
location = '{}:{}:'.format(os.path.join(location.subdir, environment.build_filename), location.lineno)
args = (location,) + args
location_str = '{}:{}:'.format(os.path.join(location.subdir,
environment.build_filename),
location.lineno)
args = (location_str,) + args
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
# all but the last item with commas, and the last with 'and'.
def format_list(list):

@ -54,11 +54,7 @@ def run(args):
sys.exit('Unknown command: ' + options.commands[0])
except Exception as e:
if isinstance(e, MesonException):
if hasattr(e, 'file') and hasattr(e, 'lineno') and hasattr(e, 'colno'):
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)
mlog.exception(e)
else:
traceback.print_exc()
return 1

Loading…
Cancel
Save