bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3#ifndef BITSIM_UTILS_LOGGER_H
4#define BITSIM_UTILS_LOGGER_H
5
33
34#include <ostream>
35#include <sstream>
36#include <string>
37#include <string_view>
38
39namespace bitsim::log
40{
41
53
55void set_level(Level level);
56
58void set_level(const std::string &logger, Level level);
59
61Level get_level(const std::string &logger);
62
68void add_file_sink(const std::string &filename, bool truncate = true);
69
75void set_pattern(const std::string &pattern);
76
78void flush();
79
80namespace detail
81{
82
84struct LoggerHandle;
85
88LoggerHandle *handle(const std::string &logger);
89
91bool enabled(LoggerHandle *logger, Level level) noexcept;
92
94void write(LoggerHandle *logger, Level level, std::string_view message) noexcept;
95
99{
100 public:
101 LogLine(LoggerHandle *logger, Level level) noexcept : logger_(logger), level_(level) {}
102
103 LogLine(const LogLine &) = delete;
104 LogLine &operator=(const LogLine &) = delete;
105
106 ~LogLine() { write(logger_, level_, stream_.view()); }
107
108 template <typename T> LogLine &operator<<(const T &value)
109 {
110 stream_ << value;
111 return *this;
112 }
113
115 LogLine &operator<<(std::ostream &(*manipulator)(std::ostream &))
116 {
117 manipulator(stream_);
118 return *this;
119 }
120
121 private:
122 LoggerHandle *logger_;
123 Level level_;
124 std::ostringstream stream_;
125};
126
127} // namespace detail
128} // namespace bitsim::log
129
135#define BITSIM_LOG_TO(logger_name, level) \
136 if (auto *const bitsim_log_logger = \
137 []() \
138 { \
139 static auto *const handle = ::bitsim::log::detail::handle(logger_name); \
140 return handle; \
141 }(); \
142 !::bitsim::log::detail::enabled(bitsim_log_logger, level)) \
143 { \
144 } \
145 else \
146 ::bitsim::log::detail::LogLine(bitsim_log_logger, level)
147
148#define BITSIM_LOG_TRACE BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::trace)
149#define BITSIM_LOG_DEBUG BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::debug)
150#define BITSIM_LOG_INFO BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::info)
151#define BITSIM_LOG_WARN BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::warn)
152#define BITSIM_LOG_ERROR BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::error)
153#define BITSIM_LOG_CRITICAL BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::critical)
154
155#endif // BITSIM_UTILS_LOGGER_H
LogLine & operator=(const LogLine &)=delete
LogLine & operator<<(const T &value)
Definition logger.h:108
~LogLine()
Definition logger.h:106
LogLine(LoggerHandle *logger, Level level) noexcept
Definition logger.h:101
LogLine & operator<<(std::ostream &(*manipulator)(std::ostream &))
For std::endl and the other manipulators.
Definition logger.h:115
LogLine(const LogLine &)=delete
void write(LoggerHandle *logger, Level level, std::string_view message) noexcept
Write a message, without its trailing new lines.
Definition logger.cpp:141
bool enabled(LoggerHandle *logger, Level level) noexcept
Whether the logger writes messages of the given level.
Definition logger.cpp:136
LoggerHandle * handle(const std::string &logger)
The logger with the given name, made the first time. It lives until the program ends.
Definition logger.cpp:129
Definition logger.cpp:16
void add_file_sink(const std::string &filename, bool truncate)
Write the messages of every logger to a file as well.
Definition logger.cpp:189
Level
The severity of a message, and the lowest severity a logger writes.
Definition logger.h:44
@ trace
Definition logger.h:45
@ warn
Definition logger.h:48
@ off
Definition logger.h:51
@ critical
Definition logger.h:50
@ debug
Definition logger.h:46
@ info
Definition logger.h:47
@ error
Definition logger.h:49
Level get_level(const std::string &logger)
The level of the logger with the given name, making it if needed.
Definition logger.cpp:182
void set_level(Level level)
Set the level of every logger, and of the loggers made later.
Definition logger.cpp:165
void flush()
Write out what the sinks hold.
Definition logger.cpp:210
void set_pattern(const std::string &pattern)
Set the format of the messages of every sink, in spdlog's pattern syntax. The default is "[%Y-%m-%d %...
Definition logger.cpp:198
Definition logger.cpp:21