Logging.cxx
Go to the documentation of this file.
1 #include "WireCellUtil/Logging.h"
6 
7 #include <vector>
8 
9 using namespace WireCell;
10 
11 
12 static
14 {
15  const std::string name = "wct";
16  static Log::logptr_t base_logger = nullptr;
17  if (base_logger) {
18  return base_logger;
19  }
20  base_logger = spdlog::get(name);
21  if (base_logger) {
22  return base_logger;
23  }
24 
25  std::vector<spdlog::sink_ptr> sv;
26  base_logger = std::make_shared<spdlog::logger>(name, sv.begin(), sv.end());
27  spdlog::register_logger(base_logger);
28  base_logger->debug("create default logger \"wct\"");
29  spdlog::set_default_logger(base_logger);
30 
31  return base_logger;
32 }
33 
34 
36 {
37  if (!level.empty()) {
38  sink->set_level(spdlog::level::from_str(level));
39  }
40  wct_base_logger()->sinks().push_back(sink);
41 }
43 {
44  auto s = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename);
45  Log::add_sink(s, level);
46 }
47 
49 {
50  if (color) {
51  auto s = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
52  Log::add_sink(s, level);
53  }
54  else {
55  auto s = std::make_shared<spdlog::sinks::stdout_sink_mt>();
56  Log::add_sink(s, level);
57  }
58 }
60 {
61  if (color) {
62  auto s = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
63  Log::add_sink(s, level);
64  }
65  else {
66  auto s = std::make_shared<spdlog::sinks::stderr_sink_mt>();
67  Log::add_sink(s, level);
68  }
69 }
70 
72 {
73  wct_base_logger(); // make sure base logger is installed.
74  auto l = spdlog::get(name);
75  if (!l) {
76  auto& sinks = wct_base_logger()->sinks();
77  l = std::make_shared<spdlog::logger>(name, sinks.begin(), sinks.end());
78 
79  // peak under the hood of spdlog. We want shared loggers to
80  // get configured with the default level.
82  if (!spdlog::get(name)) {
84  }
85  }
86 
87  return l;
88 }
89 
91 {
92  auto lvl = spdlog::level::from_str(level);
93 
94  if (which.empty()) {
95  spdlog::set_level(lvl);
96  return;
97  }
98  logger(which)->set_level(lvl);
99 }
101 {
102  if (which.empty()) {
103  spdlog::set_pattern(pattern);
104  return;
105  }
106  logger(which)->set_pattern(pattern);
107 }
static QCString name
Definition: declinfo.cpp:673
spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
Definition: common.h:142
std::shared_ptr< logger > get(const std::string &name)
Definition: spdlog.h:53
void add_stdout(bool color=true, std::string level="")
Definition: Logging.cxx:48
std::string string
Definition: nybbler.cc:12
std::shared_ptr< spdlog::sinks::sink > sinkptr_t
Definition: Logging.h:25
static registry & instance()
Definition: registry.h:228
void set_level(std::string level, std::string which="")
Definition: Logging.cxx:90
void set_pattern(std::string pattern, std::string which="")
Definition: Logging.cxx:100
string filename
Definition: train.py:213
static QStrList * l
Definition: config.cpp:1044
static Log::logptr_t wct_base_logger()
Definition: Logging.cxx:13
void set_default_logger(std::shared_ptr< spdlog::logger > default_logger)
Definition: spdlog.h:159
void initialize_logger(std::shared_ptr< logger > new_logger)
Definition: registry.h:48
void add_stderr(bool color=true, std::string level="")
Definition: Logging.cxx:59
logptr_t logger(std::string name)
Definition: Logging.cxx:71
std::shared_ptr< spdlog::logger > logptr_t
Definition: Logging.h:24
Definition: Main.h:22
color
Definition: color.h:50
void set_pattern(std::string pattern, pattern_time_type time_type=pattern_time_type::local)
Definition: spdlog.h:66
void add_file(std::string filename, std::string level="")
Definition: Logging.cxx:42
void register_logger(std::shared_ptr< logger > logger)
Definition: spdlog.h:97
std::string pattern
Definition: regex_t.cc:33
void add_sink(sinkptr_t sink, std::string level="")
Definition: Logging.cxx:35
static QCString * s
Definition: config.cpp:1042
void set_level(level::level_enum log_level)
Definition: spdlog.h:72