summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorRalf Gommers <ralf.gommers@gmail.com>2021-11-13 11:54:07 +0100
committerGitHub <noreply@github.com>2021-11-13 11:54:07 +0100
commite7f773cf46f5a06d934850be2e7d31330c85aadc (patch)
tree30bae779fcbd2099af82f95dc53f61a7a922eace /numpy
parentaa52dee8dcaba963faa0ec4e36a85d97bbc8e2de (diff)
parentd04c9ef75a711c9117f5c5662ca604ed5b88607d (diff)
downloadnumpy-e7f773cf46f5a06d934850be2e7d31330c85aadc.tar.gz
Merge pull request #20352 from honno/log-prefixes
ENH: Prefix log messages with their levels
Diffstat (limited to 'numpy')
-rw-r--r--numpy/distutils/log.py22
-rw-r--r--numpy/distutils/tests/test_log.py32
2 files changed, 54 insertions, 0 deletions
diff --git a/numpy/distutils/log.py b/numpy/distutils/log.py
index a8113b9c6..3347f56d6 100644
--- a/numpy/distutils/log.py
+++ b/numpy/distutils/log.py
@@ -87,3 +87,25 @@ _global_color_map = {
# don't use INFO,.. flags in set_verbosity, these flags are for set_threshold.
set_verbosity(0, force=True)
+
+
+_error = error
+_warn = warn
+_info = info
+_debug = debug
+
+
+def error(msg, *a, **kw):
+ _error(f"ERROR: {msg}", *a, **kw)
+
+
+def warn(msg, *a, **kw):
+ _warn(f"WARN: {msg}", *a, **kw)
+
+
+def info(msg, *a, **kw):
+ _info(f"INFO: {msg}", *a, **kw)
+
+
+def debug(msg, *a, **kw):
+ _debug(f"DEBUG: {msg}", *a, **kw)
diff --git a/numpy/distutils/tests/test_log.py b/numpy/distutils/tests/test_log.py
new file mode 100644
index 000000000..36f49f592
--- /dev/null
+++ b/numpy/distutils/tests/test_log.py
@@ -0,0 +1,32 @@
+import io
+import re
+from contextlib import redirect_stdout
+
+import pytest
+
+from numpy.distutils import log
+
+
+def setup_module():
+ log.set_verbosity(2, force=True) # i.e. DEBUG
+
+
+def teardown_module():
+ log.set_verbosity(0, force=True) # the default
+
+
+r_ansi = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
+
+
+@pytest.mark.parametrize("func_name", ["error", "warn", "info", "debug"])
+def test_log_prefix(func_name):
+ func = getattr(log, func_name)
+ msg = f"{func_name} message"
+ f = io.StringIO()
+ with redirect_stdout(f):
+ func(msg)
+ out = f.getvalue()
+ assert out # sanity check
+ clean_out = r_ansi.sub("", out)
+ line = next(line for line in clean_out.splitlines())
+ assert line == f"{func_name.upper()}: {msg}"