Utopia  2
Framework for studying models of complex & adaptive systems.
logging.hh
Go to the documentation of this file.
1 #ifndef UTOPIA_CORE_LOGGING_HH
2 #define UTOPIA_CORE_LOGGING_HH
3 
4 #include <string>
5 
6 #include <spdlog/spdlog.h>
7 #include <spdlog/sinks/stdout_color_sinks.h>
8 
9 #include "ostream.hh"
10 
11 namespace Utopia {
12 
18 inline const std::string log_core = "core";
19 inline const std::string log_data_io = "data_io";
20 inline const std::string log_data_mngr = "data_mngr";
21 
23 
31 inline std::shared_ptr<spdlog::logger> init_logger (
32  const std::string name,
33  const spdlog::level::level_enum level,
34  const bool throw_on_exist = true
35 )
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 };
47 
49 
66 inline void setup_loggers (
67  const spdlog::level::level_enum level_core = spdlog::level::warn,
68  const spdlog::level::level_enum level_data_io = spdlog::level::warn,
69  const spdlog::level::level_enum level_data_mngr = spdlog::level::warn,
70  const std::string& log_pattern = ""
71 )
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 }
90 
91 // end group logging
96 } // namespace Utopia
97 
98 #endif // UTOPIA_CORE_LOGGING_HH
const std::string log_data_mngr
Definition: logging.hh:20
void 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.
Definition: logging.hh:66
const std::string log_core
Definition: logging.hh:18
const std::string log_data_io
Definition: logging.hh:19
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
Definition: agent.hh:11