Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Functions | Variables
Output Logging

Utility functions for all logging output based on the spdlog backend. More...

Collaboration diagram for Output Logging:

Functions

std::shared_ptr< spdlog::logger > Utopia::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.
 
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="")
 Set up and register the global loggers and set the global log pattern.
 

Variables

const std::string Utopia::log_core = "core"
 
const std::string Utopia::log_data_io = "data_io"
 
const std::string Utopia::log_data_mngr = "data_mngr"
 

Detailed Description

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.

Function Documentation

◆ 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
nameName of the logger. This is also the registered logger name.
levelLevel of the logger. See spdlog::level::level_enum
throw_on_existThrow an exception if the logger exists
36{
37 auto logger = spdlog::get(name);
38
39 // Create it if it does not exist yet; without a check, spdlog would throw
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_coreLog level of the core logger
level_data_ioLog level of the data_io logger
level_data_mngrLog level of the data_mngr logger
log_patternThe global log pattern. If empty, a pre-defined log pattern will be set instead of the spdlog default.
72{
73 // initialize
74 init_logger(log_core, level_core, false);
75 init_logger(log_data_io, level_data_io, false);
76 init_logger(log_data_mngr, level_data_mngr, false);
77 spdlog::flush_on(spdlog::level::err);
78
79 // set global pattern to
80 // "[HH:MM:SS.mmm] [level(colored)] [logger] <message>"
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

Variable Documentation

◆ 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