knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::Logger Class Reference

#include <Logger.h>

Public Types

enum  SinkType { File , Console , File , Console }
 
enum  SinkType { File , Console , File , Console }
 

Static Public Member Functions

static void initialize ()
 
static void loadConfiguration (boost::property_tree::ptree &config)
 
static void setupFileSink (const std::string &basename="knowrob.log", bool rotate=true, uint32_t max_size=1048576, uint32_t max_files=4)
 
static void setSinkLevel (SinkType sinkType, spdlog::level::level_enum log_level)
 
static void setSinkPattern (SinkType sinkType, const std::string &pattern)
 
static std::string formatGenericFailure (const std::string &name, const std::string &type)
 
static void initialize ()
 
static void loadConfiguration (boost::property_tree::ptree &config)
 
static void setupFileSink (const std::string &basename="knowrob.log", bool rotate=true, uint32_t max_size=1048576, uint32_t max_files=4)
 
static void setSinkLevel (SinkType sinkType, spdlog::level::level_enum log_level)
 
static void setSinkPattern (SinkType sinkType, const std::string &pattern)
 
static std::string formatGenericFailure (const std::string &name, const std::string &type)
 

Protected Member Functions

 Logger ()
 
void updateLogger ()
 
 Logger ()
 
void updateLogger ()
 

Static Protected Member Functions

static Loggerget ()
 
static Loggerget ()
 

Protected Attributes

std::unique_ptr< impl > pimpl_
 

Detailed Description

Interface to initialize and configure the default logger of the system. The default logger is used within the logging macros KB_INFO, KB_ERROR, ...

Definition at line 75 of file Logger.h.

Member Enumeration Documentation

◆ SinkType [1/2]

The type of a logging sink.

Enumerator
File 
Console 
File 
Console 

Definition at line 80 of file Logger.h.

80 { File, Console };

◆ SinkType [2/2]

The type of a logging sink.

Enumerator
File 
Console 
File 
Console 

Definition at line 80 of file Logger.h.

80 { File, Console };

Constructor & Destructor Documentation

◆ Logger() [1/2]

Logger::Logger ( )
protected

Definition at line 23 of file Logger.cpp.

24  : pimpl_{std::make_unique<impl>()} {
25  updateLogger();
26 }
void updateLogger()
Definition: Logger.cpp:28
std::unique_ptr< impl > pimpl_
Definition: Logger.h:128

◆ Logger() [2/2]

knowrob::Logger::Logger ( )
protected

Member Function Documentation

◆ formatGenericFailure() [1/2]

std::string Logger::formatGenericFailure ( const std::string &  name,
const std::string &  type 
)
static
Parameters
namethe name of a component.
typethe type of action that failed.
Returns
a formatted error message.

Definition at line 154 of file Logger.cpp.

154  {
155  if (name.empty()) {
156  return fmt::format(R"(Failed to execute action of type "{}")", type);
157  } else {
158  return fmt::format(R"(Component "{}" failed to execute action of type "{}")", name, type);
159  }
160 }
const IRIAtomPtr type
Definition: rdf.h:15

◆ formatGenericFailure() [2/2]

static std::string knowrob::Logger::formatGenericFailure ( const std::string &  name,
const std::string &  type 
)
static
Parameters
namethe name of a component.
typethe type of action that failed.
Returns
a formatted error message.

◆ get() [1/2]

Logger & Logger::get ( )
staticprotected

Definition at line 40 of file Logger.cpp.

40  {
41  static Logger singleton;
42  return singleton;
43 }

◆ get() [2/2]

static Logger& knowrob::Logger::get ( )
staticprotected

◆ initialize() [1/2]

void Logger::initialize ( )
static

Initialize the logging subsystem, i.e., configure it with default parameters. The configuration may be changed at a later point, e.g. when reading a settings file.

Definition at line 45 of file Logger.cpp.

45  {
46  setSinkLevel(Console, spdlog::level::info);
47  setSinkPattern(Console, "[%$%H:%M:%S.%e] [%^%l%$] %v");
48 }
static void setSinkLevel(SinkType sinkType, spdlog::level::level_enum log_level)
Definition: Logger.cpp:130
static void setSinkPattern(SinkType sinkType, const std::string &pattern)
Definition: Logger.cpp:142

◆ initialize() [2/2]

static void knowrob::Logger::initialize ( )
static

Initialize the logging subsystem, i.e., configure it with default parameters. The configuration may be changed at a later point, e.g. when reading a settings file.

◆ loadConfiguration() [1/2]

void Logger::loadConfiguration ( boost::property_tree::ptree &  config)
static

Configure logging using a property tree.

Parameters
configa property tree.

Definition at line 76 of file Logger.cpp.

76  {
77  auto consoleConfig = config.get_child_optional("console-sink");
78  if (consoleConfig) {
79  auto level = consoleConfig.value().get_optional<std::string>("level");
80  auto pattern = consoleConfig.value().get_optional<std::string>("pattern");
81  if (level.has_value()) {
82  setSinkLevel(Console, spdlog::level::from_str(level.value()));
83  }
84  if (pattern.has_value()) {
85  setSinkPattern(Console, pattern.value());
86  }
87  }
88 
89  auto fileConfig = config.get_child_optional("file-sink");
90  if (fileConfig) {
91  auto &fileConfig0 = fileConfig.value();
92  auto path = getLoggingPath(fileConfig0.get_optional<std::string>("path"));
93  auto basename = fileConfig0.get<std::string>("basename", "knowrob.log");
94 
95  KB_INFO("[KnowRob] Writing logs to {}", path.native());
96 
97  setupFileSink((path / basename).native(),
98  fileConfig0.get<bool>("rotate", true),
99  fileConfig0.get<uint32_t>("max_size", 1048576),
100  fileConfig0.get<uint32_t>("max_files", 4));
101 
102  auto level = fileConfig0.get_optional<std::string>("level");
103  auto pattern = fileConfig0.get_optional<std::string>("pattern");
104  if (level.has_value()) {
105  setSinkLevel(File, spdlog::level::from_str(level.value()));
106  }
107  if (pattern.has_value()) {
108  setSinkPattern(File, pattern.value());
109  }
110  }
111 
112  auto flushInterval = config.get_optional<int64_t>("flush-interval");
113  if (flushInterval.has_value()) {
114  spdlog::flush_every(std::chrono::seconds(flushInterval.value()));
115  }
116 }
#define KB_INFO
Definition: Logger.h:26
static void setupFileSink(const std::string &basename="knowrob.log", bool rotate=true, uint32_t max_size=1048576, uint32_t max_files=4)
Definition: Logger.cpp:118
GraphTermRule & pattern()
Definition: graph.cpp:23
TermRule & string()
Definition: terms.cpp:63

◆ loadConfiguration() [2/2]

static void knowrob::Logger::loadConfiguration ( boost::property_tree::ptree &  config)
static

Configure logging using a property tree.

Parameters
configa property tree.

◆ setSinkLevel() [1/2]

void Logger::setSinkLevel ( SinkType  sinkType,
spdlog::level::level_enum  log_level 
)
static
Parameters
sinkTypethe type of the sink to configure.
log_levelthe logging level for the console sink.

Definition at line 130 of file Logger.cpp.

130  {
131  switch (sinkType) {
132  case Console:
133  get().pimpl_->console_sink->set_level(log_level);
134  break;
135  case File:
136  if (get().pimpl_->file_sink)
137  get().pimpl_->file_sink->set_level(log_level);
138  break;
139  }
140 }
static Logger & get()
Definition: Logger.cpp:40

◆ setSinkLevel() [2/2]

static void knowrob::Logger::setSinkLevel ( SinkType  sinkType,
spdlog::level::level_enum  log_level 
)
static
Parameters
sinkTypethe type of the sink to configure.
log_levelthe logging level for the console sink.

◆ setSinkPattern() [1/2]

void Logger::setSinkPattern ( SinkType  sinkType,
const std::string &  pattern 
)
static
Parameters
sinkTypethe type of the sink to configure.
patternthe logging pattern for the console sink.

Definition at line 142 of file Logger.cpp.

142  {
143  switch (sinkType) {
144  case Console:
145  get().pimpl_->console_sink->set_pattern(pattern);
146  break;
147  case File:
148  if (get().pimpl_->file_sink)
149  get().pimpl_->file_sink->set_pattern(pattern);
150  break;
151  }
152 }

◆ setSinkPattern() [2/2]

static void knowrob::Logger::setSinkPattern ( SinkType  sinkType,
const std::string &  pattern 
)
static
Parameters
sinkTypethe type of the sink to configure.
patternthe logging pattern for the console sink.

◆ setupFileSink() [1/2]

void Logger::setupFileSink ( const std::string &  basename = "knowrob.log",
bool  rotate = true,
uint32_t  max_size = 1048576,
uint32_t  max_files = 4 
)
static

Create a new fie sink. Any existing file sink will be replaced by the new one.

Parameters
basenamethe base name of log files
rotatetrue if files should be rotated on opening
max_sizemax size of log files before rotation
max_filesmax number of stored files

Definition at line 118 of file Logger.cpp.

121  {
122  auto &self = get();
123  self.pimpl_->file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
124  basename, max_size, max_files, rotate);
125  setSinkLevel(File, spdlog::level::trace);
126  setSinkPattern(File, "[%c] [thread:%t] [%^%l%$] %v (%s:%#)");
127  self.updateLogger();
128 }

◆ setupFileSink() [2/2]

static void knowrob::Logger::setupFileSink ( const std::string &  basename = "knowrob.log",
bool  rotate = true,
uint32_t  max_size = 1048576,
uint32_t  max_files = 4 
)
static

Create a new fie sink. Any existing file sink will be replaced by the new one.

Parameters
basenamethe base name of log files
rotatetrue if files should be rotated on opening
max_sizemax size of log files before rotation
max_filesmax number of stored files

◆ updateLogger() [1/2]

void Logger::updateLogger ( )
protected

Definition at line 28 of file Logger.cpp.

28  {
29  if (pimpl_->file_sink)
30  pimpl_->logger = std::make_shared<spdlog::logger>(
31  "multi_sink", spdlog::sinks_init_list({pimpl_->console_sink, pimpl_->file_sink}));
32  else
33  pimpl_->logger = std::make_shared<spdlog::logger>(
34  "multi_sink", spdlog::sinks_init_list({pimpl_->console_sink}));
35  spdlog::set_default_logger(pimpl_->logger);
36  spdlog::set_level(spdlog::level::trace);
37  spdlog::flush_every(std::chrono::seconds(2));
38 }

◆ updateLogger() [2/2]

void knowrob::Logger::updateLogger ( )
protected

Member Data Documentation

◆ pimpl_

std::unique_ptr< impl > knowrob::Logger::pimpl_
protected

Definition at line 129 of file Logger.h.


The documentation for this class was generated from the following files: