9#include <boost/python.hpp>
11#include <framework/pybasf2/LogPythonInterface.h>
13#include <framework/logging/LogConnectionFilter.h>
14#include <framework/logging/LogConnectionTxtFile.h>
15#include <framework/logging/LogConnectionJSON.h>
16#include <framework/logging/LogConnectionUDP.h>
17#include <framework/logging/LogConnectionConsole.h>
18#include <framework/logging/LogVariableStream.h>
19#include <framework/logging/LogSystem.h>
21#include <framework/core/Environment.h>
29using namespace boost::python;
35 level = overrideLevel;
173#if !defined(__GNUG__) || defined(__ICC)
175#pragma GCC diagnostic push
176#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
179 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(addLogConsole_overloads, addLogConsole, 0, 1)
180#if !defined(__GNUG__) || defined(__ICC)
182#pragma GCC diagnostic pop
185 bool terminalSupportsColors()
195 namespace bp = boost::python;
197 docstring_options options(
true,
true,
false);
200 enum_<LogConfig::ELogLevel>(
"LogLevel", R
"DOCSTRING(Class for all possible log levels
204 The lowest possible severity meant for expert only information and disabled
205 by default. In contrast to all other log levels DEBUG messages have an
206 additional numeric indication of their priority called the ``debug_level`` to
207 allow for different levels of verbosity.
209 The agreed values for ``debug_level`` are
211 * **0-9** for user code. These numbers are reserved for user analysis code and
212 may not be used by any part of basf2.
213 * **10-19** for analysis package code. The use case is that a user wants to debug
214 problems in analysis jobs with the help of experts.
216 * **20-29** for simulation/reconstruction code.
217 * **30-39** for core framework code.
219 .. note:: The default maximum debug level which will be shown when
220 running ``basf2 --debug`` without any argument for ``--debug`` is **10**
225 Used for informational messages which are of use for the average user but not
226 very important. Should be used very sparsely, everything which is of no
227 interest to the average user should be a debug message.
231 Informational message which don't indicate an error condition but are more
232 important than a mere information. For example the calculated cross section
233 or the output file name.
235 .. deprecated:: release-01-00-00
236 use `INFO <basf2.LogLevel.INFO>` messages instead
238.. attribute:: WARNING
240 For messages which indicate something which is not correct but not fatal to
241 the processing. This should **not** be used to make informational messages
242 more prominent and they should not be ignored by the user but they are not
247 For messages which indicate a clear error condition which needs to be
248 recovered. If error messages are produced before event processing is started
249 the processing will be aborted. During processing errors don't lead to a stop
250 of the processing but still indicate a problem.
254 For errors so severe that no recovery is possible. Emitting a fatal error
255 will always stop the processing and the `B2FATAL` function is guaranteed to
268 enum_<LogConfig::ELogInfo>(
"LogInfo", R
"DOCSTRING(The different fields of a log message.
270These fields can be used as a bitmask to configure the appearance of log messages.
274 The severity of the log message, one of `basf2.LogLevel`
276.. attribute:: MESSAGE
278 The actual log message
282 The name of the module active when the message was emitted. Can be empty if
283 no module was active (before/after processing or outside of the normal event
286.. attribute:: PACKAGE
288 The package the code that emitted the message belongs to. This is empty for
289 messages emitted by python scripts
291.. attribute:: FUNCTION
293 The function name that emitted the message
297 The filename containing the code emitting the message
301 The line number in the file emitting the message
314 class_<LogConfig>(
"LogConfig",
315 R
"(Defines logging settings (log levels and items included in each message) for a certain context, e.g. a module or package.
317.. seealso:: `logging.package(str) <basf2.LogPythonInterface.package>`)")
318 .def(init<bp::optional<LogConfig::ELogLevel, int> >())
322 "set or get the severity which causes program abort")
324Set the minimum log level to be shown. Messages with a log level below this value will not be shown at all.
326.. warning: Message with a level of `ERROR <LogLevel.ERROR>` or higher will always be shown and cannot be silenced.
329Set the maximum debug level to be shown. Any messages with log level `DEBUG <LogLevel.DEBUG>` and a larger debug level will not be shown.
331.. seealso: the documentation of `DEBUG <LogLevel.DEBUG>` for suitable values
334Set the severity which causes program abort.
336This can be set to a `LogLevel` which will cause the processing to be aborted if
337a message with the given level or higher is encountered. The default is
338`FATAL <LogLevel.FATAL>`. It cannot be set any higher but can be lowered.
341 "set the bitmask of LogInfo members to show when printing messages for a given log level")
343 "get the current bitmask of which parts of the log message will be printed for a given log level")
349 class_<LogPythonInterface, std::shared_ptr<LogPythonInterface>, boost::noncopyable>(
"LogPythonInterface", R
"(
350Logging configuration (for messages generated from C++ or Python), available as a global `basf2.logging` object in Python. See also `basf2.set_log_level()` and `basf2.set_debug_level()`.
352This class exposes a object called `logging <basf2.logging>` to the python interface. With
353this object it is possible to set all properties of the logging system
354directly in the steering file in a consistent manner This class also
355exposes the `LogConfig` class as well as the `LogLevel`
356and `LogInfo` enums to make setting of properties more transparent
357by using the names and not just the values. To set or get the log level,
360>>> logging.log_level = LogLevel.FATAL
361>>> print("Logging level set to", logging.log_level)
364This module also allows to send log messages directly from python to ease
365consistent error reporting throughout the framework
367>>> B2WARNING("This is a warning message")
371 For all features, see :download:`b2logging.py </framework/examples/b2logging.py>`)")
373Attribute for setting/getting the current `log level <basf2.LogLevel>`.
374Messages with a lower level are ignored.
376.. warning: Message with a level of `ERROR <LogLevel.ERROR>` or higher will always be shown and cannot be silenced.
379 "Attribute for getting/setting the debug level. If debug messages are enabled, their level needs to be at least this high to be printed. Defaults to 100.")
381 "Attribute for setting/getting the `log level <basf2.LogLevel>` at which to abort processing. Defaults to `FATAL <LogLevel.FATAL>` but can be set to a lower level in rare cases.")
383Set the maximum amount of times log messages with the same level and message text
384(excluding variables) will be repeated before it is suppressed. Suppressed messages
385will still be counted but not shown for the remainder of the processing.
387This affects messages with the same text but different ref:`logging_logvariables`.
388If the same log message is repeated frequently with different variables all of
389these will be suppressed after the given amount of repetitions.
391.. versionadded:: release-05-00-00
395 "Set `basf2.LogConfig` for given package, see also `package() <basf2.LogPythonInterface.package>`.")
397 R
"(Get the `LogConfig` for given package to set detailed logging pararameters for this package.
399 >>> logging.package('svd').debug_level = 10
400 >>> logging.package('svd').set_info(LogLevel.INFO, LogInfo.LEVEL | LogInfo.MESSAGE | LogInfo.FILE)
403 R
"(Get the `LogConfig` for given package to set detailed logging pararameters for this module.
405 >>> logging.package('svd').debug_level = 10
406 >>> logging.package('svd').set_info(LogLevel.INFO, LogInfo.LEVEL | LogInfo.MESSAGE | LogInfo.FILE)
409 R
"DOCSTRING(Set info to print for given log level. Should be an OR combination of `basf2.LogInfo` constants.
410As an example, to show only the level and text for all debug messages one could use
412>>> basf2.logging.set_info(basf2.LogLevel.DEBUG, basf2.LogInfo.LEVEL | basf2.LogInfo.MESSAGE)
415 log_level (LogLevel): log level for which to set the display info
416 log_info (int): Bitmask of `basf2.LogInfo` constants.)DOCSTRING")
418 "Parameters:\n log_level (basf2.LogLevel): Log level for which to get the display info")
420 R
"DOCSTRING(Write log output to given file. (In addition to existing outputs)\n\n"
423 filename (str): Filename to to write log messages into
424 append (bool): If set to True the file will be truncated before writing new messages.)DOCSTRING")
426 addLogConsole_overloads(args(
"enable_color"),
"Write log output to console. (In addition to existing outputs). "
427 "If ``enable_color`` is not specified color will be enabled if supported"))
429Write log output to console, but format log messages as json objects for
430simplified parsing by other tools. Each log message will be printed as a one
433.. versionadded:: release-03-00-00
436 complete_info (bool): If this is set to True the complete log information is printed regardless of the `LogInfo` setting.
439 `add_console()`, `set_info()`
442 Send the log output as a JSON object to the given hostname and port via UDP.
444.. versionadded:: release-04-00-00
447 hostname (str): The hostname to send the message to. If it can not be resolved, an exception will be thrown.
448 port (int): The port on the host to send the message via UDP.
453 .def("terminal_supports_colors", &terminalSupportsColors,
"Returns true if the terminal supports colored output")
454 .staticmethod(
"terminal_supports_colors")
456 "You can then configure your own via `add_file() <basf2.LogPythonInterface.add_file>` "
457 "or `add_console() <basf2.LogPythonInterface.add_console>`")
461 "Enable or disable the error summary printed at the end of processing. "
462 "Expects one argument whether or not the summary should be shown")
465Enable or disable logging via python. If this is set to true than log messages
466will be sent via `sys.stdout`. This is probably slightly slower but is useful
467when running in jupyter notebooks or when trying to redirect stdout in python
468to a buffer. This setting affects all log connections to the
471.. versionadded:: release-03-00-00)DOCSTRING")
474Enable or disable escaping of newlines in log messages to the console. If this
475is set to true than any newline character in log messages printed to the console
476will be replaced by a "\n" to ensure that every log messages fits exactly on one line.
478.. versionadded:: release-04-02-00)DOCSTRING")
483 scope().attr(
"logging") = initguard;
490 const std::string common_doc = R
"DOCSTRING(
491All additional positional arguments are converted to strings and concatenated
492to the log message. All keyword arguments are added to the function as
493:ref:`logging_logvariables`.)DOCSTRING";
497 setattr(
logDebug,
"__doc__",
"B2DEBUG(debugLevel, message, *args, **kwargs)\n\n"
498 "Print a `DEBUG <basf2.LogLevel.DEBUG>` message. "
499 "The first argument is the `debug_level <basf2.LogLevel.DEBUG>`. " +
504 setattr(
logInfo,
"__doc__",
"B2INFO(message, *args, **kwargs)\n\n"
505 "Print a `INFO <basf2.LogLevel.INFO>` message. " + common_doc);
509 setattr(
logResult,
"__doc__",
"B2RESULT(message, *args, **kwargs)\n\n"
510 "Print a `RESULT <basf2.LogLevel.RESULT>` message. " + common_doc
511 +
"\n\n.. deprecated:: release-01-00-00\n use `B2INFO()` instead");
515 setattr(
logWarning,
"__doc__",
"B2WARNING(message, *args, **kwargs)\n\n"
516 "Print a `WARNING <basf2.LogLevel.WARNING>` message. " + common_doc);
520 setattr(
logError,
"__doc__",
"B2ERROR(message, *args, **kwargs)\n\n"
521 "Print a `ERROR <basf2.LogLevel.ERROR>` message. " + common_doc);
525 setattr(
logFatal,
"__doc__",
"B2FATAL(message, *args, **kwargs)\n\n"
526 "Print a `FATAL <basf2.LogLevel.FATAL>` message. " + common_doc +
527 "\n\n.. note:: This also exits the program with an error and is "
528 "guaranteed to not return.");
533 std::string pythonObjectToString(
const boost::python::object& obj)
535 return boost::python::extract<std::string>(obj.attr(
"__str__")());
542 auto pythonDictToMap(
const dict& d)
544 std::map<std::string, std::string> result;
545 if (d.is_none())
return result;
546 const auto items = d.items();
547 const int size = len(d);
548 for (
int i = 0; i < size; ++i) {
549 const auto key = pythonObjectToString(items[i][0]);
550 const auto val = pythonObjectToString(items[i][1]);
551 result.emplace(std::make_pair(key, val));
561 void dispatchMessage(
LogConfig::ELogLevel logLevel, boost::python::tuple args,
const boost::python::dict& kwargs)
565 const int argSize = len(args);
566 if (argSize - firstArg <= 0) {
567 PyErr_SetString(PyExc_TypeError, (
"At least " + std::to_string(firstArg + 1) +
" positional arguments required").c_str());
568 boost::python::throw_error_already_set();
571 boost::python::extract<int> proxy(args[0]);
572 if (!proxy.check()) {
573 PyErr_SetString(PyExc_TypeError,
"First argument `debugLevel` must be an integer");
574 boost::python::throw_error_already_set();
581 stringstream message;
582 int size = len(args);
583 for (
int i = firstArg; i < size; ++i) {
584 message << pythonObjectToString(args[i]);
586 const auto cppKwArgs = pythonDictToMap(kwargs);
591 object inspect =
import(
"inspect");
592 auto frame = inspect.attr(
"currentframe")();
593 const std::string function = extract<std::string>(frame.attr(
"f_code").attr(
"co_name"));
594 const std::string file = extract<std::string>(frame.attr(
"f_code").attr(
"co_filename"));
595 int line = extract<int>(frame.attr(
"f_lineno"));
599 function, file, line, debugLevel));
606#ifndef LOG_NO_B2DEBUG
609 return boost::python::object();
617 return boost::python::object();
622#ifndef LOG_NO_B2RESULT
625 return boost::python::object();
630#ifndef LOG_NO_B2WARNING
633 return boost::python::object();
639 return boost::python::object();
646 return boost::python::object();
static Environment & Instance()
Static method to get a reference to the Environment instance.
int getDebugLevel() const
Returns the configured debug messaging level.
ELogLevel getLogLevel() const
Returns the configured log level.
void setDebugLevel(int debugLevel)
Configure the debug messaging level.
ELogLevel
Definition of the supported log levels.
@ c_Error
Error: for things that went wrong and have to be fixed.
@ c_Info
Info: for informational messages, e.g.
@ c_Debug
Debug: for code development.
@ c_Fatal
Fatal: for situations were the program execution can not be continued.
@ c_Warning
Warning: for potential problems that the user should pay attention to.
@ c_Result
Result: for informational summary messages, e.g.
@ c_Default
Default: use globally configured log level.
unsigned int getLogInfo(ELogLevel logLevel) const
Returns the configured log information for the given level.
ELogLevel getAbortLevel() const
Returns the configured abort level.
void setAbortLevel(ELogLevel abortLevel)
Configure the abort level.
@ c_Module
Module in which the message was emitted.
@ c_File
Source file in which the message was emitted.
@ c_Function
Function in which the message was emitted.
@ c_Line
Line in source file in which the message was emitted.
@ c_Level
Log level of the message.
@ c_Package
Package in which the message was emitted.
@ c_Message
Log message text.
@ c_Timestamp
Time at which the message was emitted.
void setLogLevel(ELogLevel logLevel)
Configure the log level.
void setLogInfo(ELogLevel logLevel, unsigned int logInfo)
Configure the printed log information for the given level.
static const char * logLevelToString(ELogLevel logLevelType)
Converts a log level type to a string.
Implements a log connection to an IO Stream.
static bool getEscapeNewlinesEnabled()
Check whether we want to escape newlines on console.
static bool getPythonLoggingEnabled()
Check whether console logging via python is enabled.
static void setEscapeNewlinesEnabled(bool enabled)
Set whether we want to escape newlines on console.
static void setPythonLoggingEnabled(bool enabled)
Set whether console logging via python is enabled.
static bool terminalSupportsColors(int fileDescriptor)
Returns true if the given file descriptor is a tty and supports colors.
Implements a log connection that filters repeated messages.
Implements a log connection to stdout but with messages formatted as json objects to allow easy parsi...
Implements a log connection to a text file.
Log Connection to send the log message as JSON to a UDP server.
bool getEscapeNewlinesEnabled() const
Get flag if newlines in log messages to console should be replaced by ' '`.
void setDebugLevel(int level)
Set the debug messaging level.
void setAbortLevel(LogConfig::ELogLevel level)
Set the abort log level.
static boost::python::object logFatal(boost::python::tuple args, const boost::python::dict &kwargs)
Produce fatal message.
int getLogInfo(LogConfig::ELogLevel level)
Get the printed log information for the given level.
boost::python::dict getLogStatistics()
return dict with log statistics
void setPackageLogConfig(const std::string &package, const LogConfig &config)
Set LogConfig for a given package.
void addLogFile(const std::string &filename, bool append=false)
Add a file as output connection.
void setPythonLoggingEnabled(bool enabled) const
Set flag if logging should be done via python sys.stdout
LogConfig::ELogLevel getAbortLevel()
Get the abort level.
static boost::python::object logWarning(boost::python::tuple args, const boost::python::dict &kwargs)
Produce warning message.
static void exposePythonAPI()
expose python API
bool getPythonLoggingEnabled() const
Get flag if logging should be done via python sys.stdout
LogConfig & getModuleLogConfig(const std::string &module)
Get the LogConfig for the given module.
void enableErrorSummary(bool on)
Enable/Disable error summary.
void setMaxMessageRepetitions(unsigned repetitions)
Set maximum number of repetitions before silencing "identical" log messages.
static boost::python::object logDebug(boost::python::tuple args, const boost::python::dict &kwargs)
Produce debug message.
void addLogConsole()
Add the console as output connection.
void addLogUDP(const std::string &hostname, unsigned short port)
Add a UDP server as an output connection.
void setLogLevel(LogConfig::ELogLevel level)
Set the log level.
int getDebugLevel()
Get the debug level.
void reset()
Reset logging connections.
static boost::python::object logInfo(boost::python::tuple args, const boost::python::dict &kwargs)
Produce info message.
void addLogJSON(bool complete)
Add the console as output connection but print the log messages as json objects so that they can be p...
void setLogInfo(LogConfig::ELogLevel level, int info)
Set the printed log information for the given level.
unsigned getMaxMessageRepetitions() const
Get maximum number of repetitions before silencing "identical" log messages.
void setEscapeNewlinesEnabled(bool enabled) const
Set flag if newlines in log messages to console should be replaced by ' '.
LogConfig & getPackageLogConfig(const std::string &package)
Get the LogConfig for the given package.
void zeroCounters()
Reset logging counters.
static boost::python::object logError(boost::python::tuple args, const boost::python::dict &kwargs)
Produce error message.
static boost::python::object logResult(boost::python::tuple args, const boost::python::dict &kwargs)
Produce result message.
LogConfig::ELogLevel getLogLevel()
Get the log level.
Class for logging debug, info and error messages.
void addPackageLogConfig(const std::string &package, const LogConfig &logConfig)
Add the per package log configuration.
void resetMessageCounter()
Resets the message counter and error log by setting all message counts to 0.
LogConfig * getLogConfig()
Returns global log system configuration.
LogConfig & getModuleLogConfig(const std::string &module)
Get the log configuration for the module with the given name.
void enableErrorSummary(bool on)
enable/disable error/warning summary after successful execution and B2FATAL.
bool sendMessage(LogMessage &&message)
Sends a log message using the log connection object.
void setMaxMessageRepetitions(unsigned repetitions)
Set maximum number of repetitions before silencing "identical" log messages.
int getMessageCounter(LogConfig::ELogLevel logLevel) const
Returns the number of logging calls per log level.
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
void resetLogConnections()
Removes all log connections.
unsigned getMaxMessageRepetitions() const
Get maximum number of repetitions before silencing "identical" log messages.
LogConfig & getPackageLogConfig(const std::string &package)
Get the log configuration for the package with the given name.
void addLogConnection(LogConnectionBase *logConnection)
Adds a log connection object which is used to the send the logging messages.
Specialized implementation of an ostream-like class where the << operator can be used to insert value...
Abstract base class for different kinds of events.