Skip to content

Commit

Permalink
Merge pull request #3195 from Flamefire/logging
Browse files Browse the repository at this point in the history
Optionally call log.warning in print_warning
  • Loading branch information
boegel authored Feb 29, 2020
2 parents e9fd9c3 + e8d7235 commit c6fa480
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions easybuild/tools/build_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,13 @@ def print_warning(msg, *args, **kwargs):
if args:
msg = msg % args

log = kwargs.pop('log', None)
silent = kwargs.pop('silent', False)
if kwargs:
raise EasyBuildError("Unknown named arguments passed to print_warning: %s", kwargs)

if log:
log.warning(msg)
if not silent:
sys.stderr.write("\nWARNING: %s\n\n" % msg)

Expand Down
12 changes: 10 additions & 2 deletions test/framework/build_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ def test_log_levels(self):

def test_print_warning(self):
"""Test print_warning"""
def run_check(args, silent=False, expected_stderr=''):
def run_check(args, silent=False, expected_stderr='', **kwargs):
"""Helper function to check stdout/stderr produced via print_warning."""
self.mock_stderr(True)
self.mock_stdout(True)
print_warning(*args, silent=silent)
print_warning(*args, silent=silent, **kwargs)
stderr = self.get_stderr()
stdout = self.get_stdout()
self.mock_stdout(False)
Expand All @@ -258,6 +258,14 @@ def run_check(args, silent=False, expected_stderr=''):

self.assertErrorRegex(EasyBuildError, "Unknown named arguments", print_warning, 'foo', unknown_arg='bar')

# test passing of logger to print_warning
tmp_logfile = os.path.join(self.test_prefix, 'test.log')
logger, _ = init_logging(tmp_logfile, silent=True)
expected = "\nWARNING: Test log message with a logger involved.\n\n"
run_check(["Test log message with a logger involved."], expected_stderr=expected, log=logger)
log_txt = read_file(tmp_logfile)
self.assertTrue("WARNING Test log message with a logger involved." in log_txt)

def test_print_error(self):
"""Test print_error"""
def run_check(args, silent=False, expected_stderr=''):
Expand Down

0 comments on commit c6fa480

Please sign in to comment.