Coverage for dynasor/logging_tools.py: 100%
22 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-03 19:46 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-03 19:46 +0000
1""" This module contains functions and variables to control dynasor's logging
3* `logger` - the module logger
4"""
6import logging
7import sys
9import numba
12# This is the root logger of dynasor.
13logger = logging.getLogger('dynasor')
15# Will process all levels of INFO or higher. This sets the default level.
16logger.setLevel(logging.INFO)
18# If you know what you are doing you may set this to True.
19logger.propagate = False
21# The dynasor logger will collect events from children and the default behavior
22# is to print it directly to stdout.
23ch = logging.StreamHandler(sys.stdout)
24ch.setFormatter(logging.Formatter(
25 r'%(levelname)s %(asctime)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
26logger.addHandler(ch)
29def set_logging_level(level: str) -> None:
30 """
31 Alters the logging verbosity logging is handled.
33 Possible values from least to most verbose:
34 `CRITICAL`, `ERROR`, `WARNING`, `INFO`, `DEBUG`.
36 Parameters
37 ----------
38 level
39 Verbosity level; see `Python logging library
40 <https://docs.python.org/3/library/logging.html>`_ for details.
41 """
42 logger.setLevel(level)
45_has_checked_numba_threading_layer = False
48def warn_if_numba_threading_layer_is_slow() -> None:
49 """
50 Emit a one-time warning if Numba has fallen back to the ``workqueue``
51 threading layer, which is available everywhere but not tuned for
52 performance; installing `TBB <https://pypi.org/project/tbb/>`_ (``pip
53 install tbb``) lets Numba pick a faster, fork-safe layer instead.
55 Numba only selects its threading layer lazily, the first time a
56 parallel (``@numba.njit(parallel=True)``) kernel actually runs, so this
57 is a no-op until that has happened at least once; call it right after
58 such a kernel has run.
59 """
60 global _has_checked_numba_threading_layer
61 if _has_checked_numba_threading_layer:
62 return
63 try:
64 layer = numba.threading_layer()
65 except ValueError:
66 # Threading layer not yet initialized; nothing to check yet.
67 return
68 _has_checked_numba_threading_layer = True
69 if layer == 'workqueue':
70 logger.warning(
71 'Numba is using the "workqueue" threading layer, which is slower '
72 'than the alternatives; install TBB (`pip install tbb`) for '
73 'faster parallel CPU kernels.'
74 )