knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Logger.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022, Daniel Beßler
3  * All rights reserved.
4  *
5  * This file is part of KnowRob, please consult
6  * https://github.com/knowrob/knowrob for license details.
7  */
8 
9 #ifndef KNOWROB_LOGGING_H_
10 #define KNOWROB_LOGGING_H_
11 
12 // note: must be defined before including spdlog.h
13 //#ifdef _DEBUG
14 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
15 //#else
16 //#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
17 //#endif
18 
19 #include <spdlog/spdlog.h>
20 #include <spdlog/fmt/ostr.h>
21 // BOOST
22 #include <boost/property_tree/ptree.hpp>
23 
24 #define KB_TRACE SPDLOG_TRACE
25 #define KB_DEBUG SPDLOG_DEBUG
26 #define KB_INFO SPDLOG_INFO
27 #define KB_WARN SPDLOG_WARN
28 #define KB_ERROR SPDLOG_ERROR
29 #define KB_CRITICAL SPDLOG_CRITICAL
30 
31 #define KB_LOGGER_CALL(file, line, level, ...) \
32  spdlog::default_logger_raw()->log(spdlog::source_loc{file, line, SPDLOG_FUNCTION}, level, __VA_ARGS__)
33 
34 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
35 # define KB_TRACE1(file, line, ...) KB_LOGGER_CALL(file, line, spdlog::level::trace, __VA_ARGS__)
36 #else
37 # define KB_TRACE(file, line, ...) (void)0
38 #endif
39 
40 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
41 # define KB_DEBUG1(file, line, ...) KB_LOGGER_CALL(file, line, spdlog::level::debug, __VA_ARGS__)
42 #else
43 # define KB_DEBUG(file, line, ...) (void)0
44 #endif
45 
46 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
47 # define KB_INFO1(file, line, ...) KB_LOGGER_CALL(file, line, spdlog::level::info, __VA_ARGS__)
48 #else
49 # define KB_INFO(file, line, ...) (void)0
50 #endif
51 
52 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
53 # define KB_WARN1(file, line, ...) KB_LOGGER_CALL(file, line, spdlog::level::warn, __VA_ARGS__)
54 #else
55 # define KB_WARN(file, line, ...) (void)0
56 #endif
57 
58 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
59 # define KB_ERROR1(file, line, ...) KB_LOGGER_CALL(file, line, spdlog::level::err, __VA_ARGS__)
60 #else
61 # define KB_ERROR(file, line, ...) (void)0
62 #endif
63 
64 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
65 # define KB_CRITICAL1(file, line, ...) KB_LOGGER_CALL(file, line, spdlog::level::critical, __VA_ARGS__)
66 #else
67 # define KB_CRITICAL(file, line, ...) (void)0
68 #endif
69 
70 namespace knowrob {
75  class Logger {
76  public:
80  enum SinkType { File, Console };
81 
86  static void initialize();
87 
92  static void loadConfiguration(boost::property_tree::ptree &config);
93 
102  static void setupFileSink(const std::string &basename="knowrob.log",
103  bool rotate=true,
104  uint32_t max_size=1048576,
105  uint32_t max_files=4);
106 
111  static void setSinkLevel(SinkType sinkType, spdlog::level::level_enum log_level);
112 
117  static void setSinkPattern(SinkType sinkType, const std::string &pattern);
118 
125 
126  protected:
127  // hide implementation details
128  struct impl;
129  std::unique_ptr<impl> pimpl_;
130 
132 
133  void updateLogger();
134 
135  static Logger& get();
136  };
137 }
138 
142 #define KB_LOGGED_TRY_CATCH(name, type, goal) do { \
143  try { goal } \
144  catch (const boost::python::error_already_set&) \
145  { LOG_KNOWROB_ERROR(PythonError(), Logger::formatGenericFailure(name,type)); } \
146  catch (KnowRobError &e) \
147  { LOG_KNOWROB_ERROR(e, Logger::formatGenericFailure(name,type)); } \
148  catch (std::exception &e) \
149  { KB_ERROR("{}. {}", Logger::formatGenericFailure(name,type), e.what()); } \
150  catch (...) \
151  { KB_ERROR("{}. Unknown failure!", Logger::formatGenericFailure(name,type)); } \
152 } while(0)
153 
157 #define KB_LOGGED_TRY_EXCEPT(name, type, goal, except) do { \
158  try { goal } \
159  catch (const boost::python::error_already_set&) \
160  { LOG_KNOWROB_ERROR(PythonError(), Logger::formatGenericFailure(name,type)); except } \
161  catch (KnowRobError &e) \
162  { LOG_KNOWROB_ERROR(e, Logger::formatGenericFailure(name,type)); except } \
163  catch (std::exception &e) \
164  { KB_ERROR("{}: {}", Logger::formatGenericFailure(name,type), e.what()); except } \
165  catch (...) \
166  { KB_ERROR("{}: unknown failure.", Logger::formatGenericFailure(name,type)); except } \
167 } while(0)
168 
169 #endif //KNOWROB_LOGGING_H_
static void setSinkPattern(SinkType sinkType, const std::string &pattern)
static void setSinkLevel(SinkType sinkType, spdlog::level::level_enum log_level)
static void setupFileSink(const std::string &basename="knowrob.log", bool rotate=true, uint32_t max_size=1048576, uint32_t max_files=4)
static std::string formatGenericFailure(const std::string &name, const std::string &type)
static void initialize()
static void loadConfiguration(boost::property_tree::ptree &config)
std::unique_ptr< impl > pimpl_
Definition: Logger.h:128
static Logger & get()
GraphTermRule & pattern()
Definition: graph.cpp:23
TermRule & string()
Definition: terms.cpp:63
const IRIAtomPtr type
Definition: rdf.h:15