Source code for lightonopu

# Copyright (c) 2020 LightOn, All Rights Reserved.
# This file is subject to the terms and conditions defined in
# file 'LICENSE.txt', which is part of this source code package.

import warnings
import pkg_resources
import lightonopu.utils

    package = pkg_resources.get_distribution('lightonopu')
    __version__ = package.version
except pkg_resources.ResolutionError:
    __version__ = "unversioned"

    # Cleanup possibly existing global opu loggers
    import spdlog
except (ImportError, RuntimeError):

_logger = None
# Set to 0, 1, 2 or 3
_verbose_level = 0
_has_warned_spd = False

# noinspection PyUnresolvedReferences
[docs]def set_verbose_level(verbose_level): """Set the log_level for the lightonopu module. Once change, one has to re-execute the get_trace_fn and alike Levels are 0: nothing, 1: print info, 2: debug info, 3: trace info """ global _verbose_level, _logger _verbose_level = verbose_level __init_logger() if _logger: import spdlog if verbose_level <= 0: _logger.set_level(spdlog.LogLevel.OFF) elif verbose_level == 1: _logger.set_level(spdlog.LogLevel.INFO) elif verbose_level == 2: _logger.set_level(spdlog.LogLevel.DEBUG) elif verbose_level >= 3: _logger.set_level(spdlog.LogLevel.TRACE)
def get_verbose_level(): global _verbose_level return _verbose_level
[docs]def get_debug_fn(): """Returns debug logging function, or blank if logging level isn't debug""" return __get_logging_fn(2, "debug")
[docs]def get_trace_fn(): """Returns trace loggeing function, or blank if logging level isn't trace""" return __get_logging_fn(3, "trace")
def get_print_fn(): global _verbose_level from lightonopu.utils import blank_fn if _verbose_level >= 1: return print else: return lightonopu.utils.blank_fn def __get_logging_fn(threshold_level, spd_level): global _verbose_level, _logger, _has_warned_spd if _verbose_level >= threshold_level: __init_logger() if _logger: # Return the logging function (_logger.debug or _logger.trace) return getattr(_logger, spd_level) else: if not _has_warned_spd: warnings.warn("To have logging, install the spdlog package") _has_warned_spd = True return lightonopu.utils.blank_fn else: # below threshold, logger will be a blank function return lightonopu.utils.blank_fn def __init_logger(): """Init the logger, if spdlog is installed""" global _verbose_level, _logger, _has_warned_spd try: # Module is imported only once import spdlog # If spdlog package is available, instantiate a trace logger if not _logger: try: _logger = spdlog.get("opu") except RuntimeError: _logger = spdlog.ConsoleLogger("opu") except ImportError: _logger = None # Finally make OPU available at the package level from lightonopu.opu import OPU