Added logging to print functions
This commit is contained in:
parent
5925aca3c2
commit
318f59c473
1 changed files with 39 additions and 25 deletions
|
|
@ -521,29 +521,34 @@ class TryAndPrint():
|
||||||
LOG.info('Running function: %s.%s', function.__module__, function.__name__)
|
LOG.info('Running function: %s.%s', function.__module__, function.__name__)
|
||||||
try:
|
try:
|
||||||
output = function(*args, **kwargs)
|
output = function(*args, **kwargs)
|
||||||
if print_return:
|
|
||||||
result_msg = self._format_function_output(output)
|
|
||||||
print(result_msg)
|
|
||||||
else:
|
|
||||||
print_success(self.msg_good)
|
|
||||||
except w_exceptions as _exception:
|
except w_exceptions as _exception:
|
||||||
|
# Warnings
|
||||||
result_msg = self._format_exception_message(_exception)
|
result_msg = self._format_exception_message(_exception)
|
||||||
print_warning(result_msg)
|
print_warning(result_msg, log=False)
|
||||||
f_exception = _exception
|
f_exception = _exception
|
||||||
except e_exceptions as _exception:
|
except e_exceptions as _exception:
|
||||||
|
# Exceptions
|
||||||
result_msg = self._format_exception_message(_exception)
|
result_msg = self._format_exception_message(_exception)
|
||||||
print_error(result_msg)
|
print_error(result_msg, log=False)
|
||||||
f_exception = _exception
|
f_exception = _exception
|
||||||
except Exception as _exception: # pylint: disable=broad-except
|
except Exception as _exception: # pylint: disable=broad-except
|
||||||
|
# Unexpected exceptions
|
||||||
if verbose:
|
if verbose:
|
||||||
result_msg = self._format_exception_message(_exception)
|
result_msg = self._format_exception_message(_exception)
|
||||||
else:
|
else:
|
||||||
result_msg = self.msg_bad
|
result_msg = self.msg_bad
|
||||||
print_error(result_msg)
|
print_error(result_msg, log=False)
|
||||||
f_exception = _exception
|
f_exception = _exception
|
||||||
if not catch_all:
|
if not catch_all:
|
||||||
# Re-raise error as necessary
|
# Re-raise error as necessary
|
||||||
raise
|
raise
|
||||||
|
else:
|
||||||
|
# Success
|
||||||
|
if print_return:
|
||||||
|
result_msg = self._format_function_output(output)
|
||||||
|
print(result_msg)
|
||||||
|
else:
|
||||||
|
print_success(self.msg_good, log=False)
|
||||||
|
|
||||||
# Done
|
# Done
|
||||||
self._log_result(message, result_msg)
|
self._log_result(message, result_msg)
|
||||||
|
|
@ -558,7 +563,6 @@ class TryAndPrint():
|
||||||
def abort(prompt='Aborted.', show_prompt=True, return_code=1):
|
def abort(prompt='Aborted.', show_prompt=True, return_code=1):
|
||||||
"""Abort script."""
|
"""Abort script."""
|
||||||
print_warning(prompt)
|
print_warning(prompt)
|
||||||
LOG.warning(prompt)
|
|
||||||
if show_prompt:
|
if show_prompt:
|
||||||
sleep(1)
|
sleep(1)
|
||||||
pause(prompt='Press Enter to exit... ')
|
pause(prompt='Press Enter to exit... ')
|
||||||
|
|
@ -766,7 +770,7 @@ def input_text(prompt='Enter text'):
|
||||||
def major_exception():
|
def major_exception():
|
||||||
"""Display traceback, optionally upload detailes, and exit."""
|
"""Display traceback, optionally upload detailes, and exit."""
|
||||||
LOG.critical('Major exception encountered', exc_info=True)
|
LOG.critical('Major exception encountered', exc_info=True)
|
||||||
print_error('Major exception')
|
print_error('Major exception', log=False)
|
||||||
print_warning(SUPPORT_MESSAGE)
|
print_warning(SUPPORT_MESSAGE)
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
||||||
|
|
@ -780,10 +784,10 @@ def major_exception():
|
||||||
try:
|
try:
|
||||||
upload_debug_report(report, reason='CRASH')
|
upload_debug_report(report, reason='CRASH')
|
||||||
except Exception: #pylint: disable=broad-except
|
except Exception: #pylint: disable=broad-except
|
||||||
print_error('FAILED')
|
print_error('FAILED', log=False)
|
||||||
LOG.error('Upload failed', exc_info=True)
|
LOG.error('Upload failed', exc_info=True)
|
||||||
else:
|
else:
|
||||||
print_success('SUCCESS')
|
print_success('SUCCESS', log=False)
|
||||||
LOG.info('Upload successful')
|
LOG.info('Upload successful')
|
||||||
|
|
||||||
# Done
|
# Done
|
||||||
|
|
@ -821,34 +825,45 @@ def print_colored(strings, colors, **kwargs):
|
||||||
print(msg, **print_options)
|
print(msg, **print_options)
|
||||||
|
|
||||||
|
|
||||||
def print_error(msg, **kwargs):
|
def print_error(msg, log=True, **kwargs):
|
||||||
"""Prints message in RED."""
|
"""Prints message in RED and log as ERROR."""
|
||||||
LOG.debug('msg: %s, kwargs: %s', msg, kwargs)
|
|
||||||
if 'file' not in kwargs:
|
if 'file' not in kwargs:
|
||||||
# Only set if not specified
|
# Only set if not specified
|
||||||
kwargs['file'] = sys.stderr
|
kwargs['file'] = sys.stderr
|
||||||
print_colored([msg], ['RED'], **kwargs)
|
print_colored([msg], ['RED'], **kwargs)
|
||||||
|
if log:
|
||||||
|
LOG.error(msg)
|
||||||
|
|
||||||
|
|
||||||
def print_info(msg, **kwargs):
|
def print_info(msg, log=True, **kwargs):
|
||||||
"""Prints message in BLUE."""
|
"""Prints message in BLUE and log as INFO."""
|
||||||
LOG.debug('msg: %s, kwargs: %s', msg, kwargs)
|
|
||||||
print_colored([msg], ['BLUE'], **kwargs)
|
print_colored([msg], ['BLUE'], **kwargs)
|
||||||
|
if log:
|
||||||
|
LOG.info(msg, log=True)
|
||||||
|
|
||||||
|
|
||||||
def print_success(msg, **kwargs):
|
def print_standard(msg, log=True, **kwargs):
|
||||||
"""Prints message in GREEN."""
|
"""Prints message and log as INFO."""
|
||||||
LOG.debug('msg: %s, kwargs: %s', msg, kwargs)
|
print(msg, log=True, **kwargs)
|
||||||
|
if log:
|
||||||
|
LOG.info(msg, log=True)
|
||||||
|
|
||||||
|
|
||||||
|
def print_success(msg, log=True, **kwargs):
|
||||||
|
"""Prints message in GREEN and log as INFO."""
|
||||||
print_colored([msg], ['GREEN'], **kwargs)
|
print_colored([msg], ['GREEN'], **kwargs)
|
||||||
|
if log:
|
||||||
|
LOG.info(msg, log=True)
|
||||||
|
|
||||||
|
|
||||||
def print_warning(msg, **kwargs):
|
def print_warning(msg, log=True, **kwargs):
|
||||||
"""Prints message in YELLOW."""
|
"""Prints message in YELLOW and log as WARNING."""
|
||||||
LOG.debug('msg: %s, kwargs: %s', msg, kwargs)
|
|
||||||
if 'file' not in kwargs:
|
if 'file' not in kwargs:
|
||||||
# Only set if not specified
|
# Only set if not specified
|
||||||
kwargs['file'] = sys.stderr
|
kwargs['file'] = sys.stderr
|
||||||
print_colored([msg], ['YELLOW'], **kwargs)
|
print_colored([msg], ['YELLOW'], **kwargs)
|
||||||
|
if log:
|
||||||
|
LOG.warning(msg)
|
||||||
|
|
||||||
|
|
||||||
def set_title(title):
|
def set_title(title):
|
||||||
|
|
@ -920,7 +935,6 @@ def upload_debug_report(report, compress=True, reason='DEBUG'):
|
||||||
# Check if the required server details are available
|
# Check if the required server details are available
|
||||||
if not all(CRASH_SERVER.get(key, False) for key in ('Name', 'Url', 'User')):
|
if not all(CRASH_SERVER.get(key, False) for key in ('Name', 'Url', 'User')):
|
||||||
msg = 'Server details missing, aborting upload.'
|
msg = 'Server details missing, aborting upload.'
|
||||||
LOG.error(msg)
|
|
||||||
print_error(msg)
|
print_error(msg)
|
||||||
raise RuntimeError(msg)
|
raise RuntimeError(msg)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue