Utility functions for all logging output based on the spdlog backend.
More...
Utility functions for all logging output based on the spdlog backend.
All output of the simulation backend is intended to be controlled via loggers. Loggers are implemented using spdlog (see https://github.com/gabime/spdlog). Regular output via the standard pipes std::cout
and std::cerr
is strongly discouraged.
Utopia generates three types of loggers: One logger for Data I/O functions, one for Core (backend) functions, and one for each model instance. The Utopia::Model base class holds a logger instance which should be used for information on the current model. To write log messages from within Data I/O or Core backend functions, the respective logger first has to be retrieved. This is achieved by using spdlog::get
(https://github.com/gabime/spdlog/wiki/2.-Creating-loggers). The names for the two loggers are exported within the Utopia namespace. All log levels are handled through the input configuration files.
The Utopia::PseudoParent automatically creates the utility loggers. For executables without Models (like tests), the loggers have to be created explicitly by manually calling Utopia::setup_loggers.
◆ init_logger()
std::shared_ptr< spdlog::logger > Utopia::init_logger |
( |
const std::string |
name, |
|
|
const spdlog::level::level_enum |
level, |
|
|
const bool |
throw_on_exist = true |
|
) |
| |
|
inline |
Initialize a logger with a certain name and log level.
If the logger already exists, issue a warning and only set the log level. Optionally, this function can throw an exception.
- Parameters
-
name | Name of the logger. This is also the registered logger name. |
level | Level of the logger. See spdlog::level::level_enum |
throw_on_exist | Throw an exception if the logger exists |
36{
37 auto logger = spdlog::get(name);
38
39
40 if (not logger || throw_on_exist) {
41 logger = spdlog::stdout_color_mt(name);
42 }
43
44 logger->set_level(level);
45 return logger;
46};
◆ setup_loggers()
void Utopia::setup_loggers |
( |
const spdlog::level::level_enum |
level_core = spdlog::level::warn , |
|
|
const spdlog::level::level_enum |
level_data_io = spdlog::level::warn , |
|
|
const spdlog::level::level_enum |
level_data_mngr = spdlog::level::warn , |
|
|
const std::string & |
log_pattern = "" |
|
) |
| |
|
inline |
Set up and register the global loggers and set the global log pattern.
Utopia employs the following global loggers:
core
: for the Core module
data_io
: for the Data I/O module in general
data_mngr
: for the DataIO::DataManager
They can be retrieved with the spdlog::get(name) function, where 'name' can be one of the 'log_' strings stored in the Utopia namespace.
This function only (re)sets the log levels if the loggers already exist.
- Parameters
-
level_core | Log level of the core logger |
level_data_io | Log level of the data_io logger |
level_data_mngr | Log level of the data_mngr logger |
log_pattern | The global log pattern. If empty, a pre-defined log pattern will be set instead of the spdlog default. |
72{
73
77 spdlog::flush_on(spdlog::level::err);
78
79
80
81 if (not log_pattern.empty()) {
82 spdlog::set_pattern(log_pattern);
83 }
84 else {
85 spdlog::set_pattern("[%T.%e] [%^%l%$] [%n] %v");
86 }
87
88 spdlog::get("core")->info("Set up loggers: core, data_io, data_mngr.");
89}
std::shared_ptr< spdlog::logger > init_logger(const std::string name, const spdlog::level::level_enum level, const bool throw_on_exist=true)
Initialize a logger with a certain name and log level.
Definition logging.hh:31
◆ log_core
const std::string Utopia::log_core = "core" |
|
inline |
◆ log_data_io
const std::string Utopia::log_data_io = "data_io" |
|
inline |
◆ log_data_mngr
const std::string Utopia::log_data_mngr = "data_mngr" |
|
inline |