Skip to content

Commit

Permalink
[SCons] Add custom FAILED logging level
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and bryanwweber committed Jun 12, 2022
1 parent 5c4124b commit e68c7de
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions doc/SConscript
Expand Up @@ -31,7 +31,7 @@ def extract_matlab_docstring(mfile, level):
elif level == 1:
docstring = " .. mat:function:: "
else:
print("Unknown level for MATLAB documentation.")
logger.error(f"Unknown level for MATLAB documentation.")
sys.exit(1)

# The leader is the number of spaces at the beginning of a regular line
Expand Down Expand Up @@ -81,7 +81,7 @@ def get_function_name(function_string):
elif function_string.startswith('classdef '):
sig = function_string[len('classdef '):]
else:
print("Unknown function declaration in MATLAB document", function_string)
logger.error(f"Unknown function declaration in MATLAB document: {function_string}")
sys.exit(1)

# Split the function signature on the equals sign, if it exists.
Expand Down
2 changes: 1 addition & 1 deletion samples/cxx/SConscript
Expand Up @@ -44,7 +44,7 @@ set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS})
localenv.Prepend(CPPPATH=['#include'])

if openmp and not env['HAS_OPENMP']:
print("INFO: Skipping sample {} because 'omp.h' was not found.".format(name))
logger.info(f"Skipping sample {name} because 'omp.h' was not found.")
else:
buildSample(localenv.Program, pjoin(subdir, name),
multi_glob(localenv, subdir, *extensions))
Expand Down
12 changes: 12 additions & 0 deletions site_scons/buildutils.py
Expand Up @@ -439,6 +439,11 @@ def help(
return "\n".join(message)


# Add custom logging level
LOGGING_FAILED_NUM = 42
logging.addLevelName(LOGGING_FAILED_NUM, "FAILED")


class LevelAdapter(logging.LoggerAdapter):
"""This adapter processes the ``print_level`` keyword-argument to log functions.
Expand All @@ -455,6 +460,13 @@ class LevelAdapter(logging.LoggerAdapter):
"""
def __init__(self, logger):
self.logger = logger
self.logger.failed = self.failed

def failed(self, message, *args, **kws):
# custom logger adapted from https://stackoverflow.com/questions/2183233
if self.isEnabledFor(LOGGING_FAILED_NUM):
# logger takes its '*args' as 'args'.
self._log(LOGGING_FAILED_NUM, message, args, **kws)

def process(self, msg, kwargs):
"""Pop the value of ``print_level`` into the ``extra`` dictionary.
Expand Down
12 changes: 7 additions & 5 deletions test/SConscript
Expand Up @@ -68,7 +68,7 @@ def addTestProgram(subdir, progName, env_vars={}):
code = subprocess.call(cmd, env=env['ENV'], cwd=workDir)

if code:
print("FAILED: Test '{0}' exited with code {1}".format(progName, code))
logger.failed(f"Test '{progName}' exited with code {code}")
if env["fast_fail_tests"]:
sys.exit(1)
else:
Expand Down Expand Up @@ -136,7 +136,6 @@ def addPythonTest(testname, subdir, script, interpreter,

environ = dict(env['ENV'])
for k,v in env_vars.items():
print(k,v)
environ[k] = v

cmdargs = args.split()
Expand Down Expand Up @@ -237,9 +236,12 @@ def addMatlabTest(script, testName, dependencies=None, env_vars=()):
' ***no results for entire test suite***'] = 100
return

print('-------- Matlab test results --------')
print(results)
print('------ end Matlab test results ------')
matlab_test_message = [
f"{' Matlab test results ':-^88}",
results,
f"{' end Matlab test results ':-^88}",
]
logger.info("\n".join(matlab_test_message))

passed = True
for line in results.split('\n'):
Expand Down

0 comments on commit e68c7de

Please sign in to comment.