29 #ifndef TOMOGRAPHER_PY_PYLOGGER_H 30 #define TOMOGRAPHER_PY_PYLOGGER_H 34 #include <tomographerpy/common.h> 36 #if PY_MAJOR_VERSION < 3 38 #include <pybind11/eval.h> 96 pybind11::object py_logging;
97 pybind11::object py_logger;
104 inline void initPythonLogger(
std::string logger_name =
"tomographer");
109 inline void setLevel(
int level);
112 inline py::object toPythonLevel(
int level)
const;
115 inline py::object toPythonLevelName(
int level)
const;
118 inline int fromPythonLevel(py::object pylvl)
const;
122 inline void emitLog(
int level,
const char * origin,
const std::string & msg);
175 py_logging = py::module::import(
"logging");
176 py_logger = py::getattr(py_logging,
"getLogger")(logger_name);
181 int lvl =
fromPythonLevel(py::getattr(py_logger,
"getEffectiveLevel")());
197 if (!py_logger.is_none()) {
199 int effective_level =
fromPythonLevel(py::getattr(py_logger,
"getEffectiveLevel")());
202 stream <<
"Log level LONGDEBUG set on C++ logger but Python logger only displays messages of " 204 <<
"considerably and uselessly slow down the computation as tons of messages on the " 205 <<
"C++ side will be emitted to the Python logger (where they will be ignored) instead of " 206 <<
"being filtered out immediately.";
218 fprintf(stderr,
"%s:%s:%s (bypassed python logger)\n",
224 if (py_logger.is_none()) {
226 "tomographer:PyLogger: INTERNAL ERROR: PYTHON LOGGING MODULE NOT SET.\n" 227 "In attempt to call emitLog().");
229 "Message was (%d): %s: %s\n\n", level, origin, msg.
c_str());
240 extra[
"origin"] = origin;
241 extra[
"raw_msg"] = msg;
243 kwargs[
"extra"] = extra;
244 auto logfn = py::getattr(py_logger,
"log");
249 logfn(*py::make_tuple(pylevel, full_msg), **kwargs);
260 if (py_logging.is_none()) {
262 "tomographer:PyLogger: INTERNAL ERROR: PYTHON LOGGING MODULE NOT SET.\n" 263 "In attempt to call toPythonLevel().");
268 return py::getattr(py_logging,
"ERROR");
270 return py::getattr(py_logging,
"WARNING");
272 return py::getattr(py_logging,
"INFO");
274 return py::getattr(py_logging,
"DEBUG");
277 #if PY_MAJOR_VERSION >= 3 283 return py::eval(
"1");
291 if (py_logging.is_none()) {
293 "tomographer:PyLogger: INTERNAL ERROR: PYTHON LOGGING MODULE NOT SET.\n" 294 "In attempt to call toPythonLevelName().");
297 return py_logging.attr(
"getLevelName")(
toPythonLevel(level));
303 if (py_logging.is_none()) {
305 "tomographer:PyLogger: INTERNAL ERROR: PYTHON LOGGING MODULE NOT SET.\n" 306 "In attempt to call fromPythonLevel().");
310 if (pylvl.cast<
int>() < py::getattr(py_logging,
"DEBUG").cast<
int>()) {
312 }
else if (pylvl.cast<
int>() < py::getattr(py_logging,
"INFO").cast<
int>()) {
314 }
else if (pylvl.cast<
int>() < py::getattr(py_logging,
"WARNING").cast<
int>()) {
316 }
else if (pylvl.cast<
int>() < py::getattr(py_logging,
"ERROR").cast<
int>()) {
Base namespace for the Tomographer project.
void setLevel(int level)
Change the level of the current logger. Note that this will NOT automatically change the effective le...
Logger providing transparent integration with Python's logging module.
Object which stores a log level and can initialize from a string.
py::object toPythonLevelName(int level) const
Convert a Tomographer::Logger level to a Python logging level name (Python string) ...
int fromPythonLevel(py::object pylvl) const
Convert a logging level to a Tomographer::Logger level constant.
void setLogLevel(int level)
Store a new run-time log level.
Default traits for Logger implementations.
void warning(const char *origin, const char *fmt,...)
emit a warning message
Traits template struct to be specialized for specific Logger implementations.
Long Debug logging level.
C++ Classes and Utilities for Python Modules.
void debug(const char *origin, const char *fmt,...)
emit an debug message
void emitLog(int level, const char *origin, const std::string &msg)
Callback for actually emitting log messages, don't call this manually.
int level() const
Get the log level set for this logger.
Information logging level.
py::object toPythonLevel(int level) const
Convert a Tomographer::Logger level to a Python logging level (Python integer)
Utilities for logging messages.
void initPythonLogger(std::string logger_name="tomographer")
Initialize the logger and attach it to a logger in the logging module named logger_name.