bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
logger.h File Reference

Logging for bitsim and the libraries built on it, on top of spdlog. More...

#include <ostream>
#include <sstream>
#include <string>
#include <string_view>
Include dependency graph for logger.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  bitsim::log::detail::LogLine
 Collects one message with operator<< and writes it when it is destroyed, at the end of the statement. More...

Namespaces

namespace  bitsim
namespace  bitsim::log
namespace  bitsim::log::detail

Macros

#define BITSIM_LOG_TO(logger_name, level)
 Log a message to the logger with the given name, a string literal, at the given level: BITSIM_LOG_TO("mylib", bitsim::log::Level::info) << "..."; The logger is looked up once for every place this is used.
#define BITSIM_LOG_TRACE   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::trace)
#define BITSIM_LOG_DEBUG   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::debug)
#define BITSIM_LOG_INFO   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::info)
#define BITSIM_LOG_WARN   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::warn)
#define BITSIM_LOG_ERROR   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::error)
#define BITSIM_LOG_CRITICAL   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::critical)

Enumerations

enum class  bitsim::log::Level {
  bitsim::log::trace , bitsim::log::debug , bitsim::log::info , bitsim::log::warn ,
  bitsim::log::error , bitsim::log::critical , bitsim::log::off
}
 The severity of a message, and the lowest severity a logger writes. More...

Functions

void bitsim::log::set_level (Level level)
 Set the level of every logger, and of the loggers made later.
void bitsim::log::set_level (const std::string &logger, Level level)
 Set the level of the logger with the given name, making it if needed.
Level bitsim::log::get_level (const std::string &logger)
 The level of the logger with the given name, making it if needed.
void bitsim::log::add_file_sink (const std::string &filename, bool truncate=true)
 Write the messages of every logger to a file as well.
void bitsim::log::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 %H:%M:%S.%e] [%n] [%l] %v": the time, the logger, the level and the message.
void bitsim::log::flush ()
 Write out what the sinks hold.
LoggerHandle * bitsim::log::detail::handle (const std::string &logger)
 The logger with the given name, made the first time. It lives until the program ends.
bool bitsim::log::detail::enabled (LoggerHandle *logger, Level level) noexcept
 Whether the logger writes messages of the given level.
void bitsim::log::detail::write (LoggerHandle *logger, Level level, std::string_view message) noexcept
 Write a message, without its trailing new lines.

Detailed Description

Logging for bitsim and the libraries built on it, on top of spdlog.

A message is written with the stream operator, so anything that has an operator<< can be logged:

BITSIM_LOG_INFO << "Loaded sensor backend: " << name;
BITSIM_LOG_WARN << "Ignoring an invalid IMU message: " << text;
#define BITSIM_LOG_INFO
Definition logger.h:150
#define BITSIM_LOG_WARN
Definition logger.h:151

Messages go to named loggers. bitsim logs to "bitsim"; bitrl logs to "bitrl"; BITSIM_LOG_TO(name, level) logs to any other name, and the logger is made the first time it is used. Every logger writes to the same sinks: standard error, in colour, and the files added with add_file_sink().

The level of every logger is info unless it is changed with set_level(), or with the SPDLOG_LEVEL environment variable, read when the first logger is made: SPDLOG_LEVEL=debug for all of them, SPDLOG_LEVEL=warn,bitrl=debug for warnings and above except for bitrl. A message below the level of its logger is not formatted at all, so its stream expressions are not evaluated.

spdlog is used only in the implementation, so projects that use bitsim do not need its headers. bitsim's own messages are compiled in only when it is built with the BITSIM_LOG option, which is on by default.

Macro Definition Documentation

◆ BITSIM_LOG_CRITICAL

#define BITSIM_LOG_CRITICAL   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::critical)

◆ BITSIM_LOG_DEBUG

#define BITSIM_LOG_DEBUG   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::debug)

◆ BITSIM_LOG_ERROR

#define BITSIM_LOG_ERROR   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::error)

◆ BITSIM_LOG_INFO

#define BITSIM_LOG_INFO   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::info)

◆ BITSIM_LOG_TO

#define BITSIM_LOG_TO ( logger_name,
level )
Value:
if (auto *const bitsim_log_logger = \
[]() \
{ \
static auto *const handle = ::bitsim::log::detail::handle(logger_name); \
return handle; \
}(); \
!::bitsim::log::detail::enabled(bitsim_log_logger, level)) \
{ \
} \
else \
::bitsim::log::detail::LogLine(bitsim_log_logger, level)
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

Log a message to the logger with the given name, a string literal, at the given level: BITSIM_LOG_TO("mylib", bitsim::log::Level::info) << "..."; The logger is looked up once for every place this is used.

◆ BITSIM_LOG_TRACE

#define BITSIM_LOG_TRACE   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::trace)

◆ BITSIM_LOG_WARN

#define BITSIM_LOG_WARN   BITSIM_LOG_TO("bitsim", ::bitsim::log::Level::warn)