RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <atomic>
7#include <cstddef>
8#include <format>
9#include <memory>
10#include <string>
11#include <string_view>
12#include <utility>
13
14namespace RST::Log {
15
16 class ISink;
17 class RingBufferSink;
18 struct LogMessage;
19
24 class Logger {
25 public:
27 explicit Logger(std::string name, LogLevel level = LogLevel::Trace);
29
30 Logger(const Logger&) = delete;
31 Logger& operator=(const Logger&) = delete;
32
33 [[nodiscard]] const std::string& getName() const noexcept { return _name; }
34
35 // ── Levels ───────────────────────────────────────────────────────────
37 void setLevel(LogLevel level) noexcept { _level.store(level, std::memory_order_relaxed); }
38 [[nodiscard]] LogLevel getLevel() const noexcept { return _level.load(std::memory_order_relaxed); }
40 [[nodiscard]] bool shouldLog(LogLevel level) const noexcept { return level != LogLevel::Off && level >= getLevel(); }
41
42 // ── Flush ───────────────────────────────────────────────────────────
44 void flushOn(LogLevel level) noexcept { _flushLevel.store(level, std::memory_order_relaxed); }
45 [[nodiscard]] LogLevel getFlushLevel() const noexcept { return _flushLevel.load(std::memory_order_relaxed); }
46
47 // ── Sinks ────────────────────────────────────────────────────────────
48 void addSink(std::shared_ptr<ISink> sink);
50 void addSink(std::shared_ptr<ISink> sink, RingBufferSink& history);
52 bool removeSink(const std::shared_ptr<ISink>& sink) noexcept;
53 void clearSinks() noexcept;
54 [[nodiscard]] std::size_t sinkCount() const noexcept;
55
56 // ── Formatted logging ────────────────────────────────────────────────
58 template <class... Args>
59 void log(LogLevel level, const SourceLocation& source, std::format_string<Args...> format, Args&&... args) noexcept
60 {
61 if (shouldLog(level)) {
62 vlog(level, source, _name, format.get(), std::make_format_args(args...));
63 }
64 }
65
67 void vlog(LogLevel level, const SourceLocation& source, std::string_view category, std::string_view format, std::format_args args) noexcept;
68
70 template <class... Args> void trace(std::format_string<Args...> format, Args&&... args) noexcept { log(LogLevel::Trace, {}, format, std::forward<Args>(args)...); }
71 template <class... Args> void debug(std::format_string<Args...> format, Args&&... args) noexcept { log(LogLevel::Debug, {}, format, std::forward<Args>(args)...); }
72 template <class... Args> void info(std::format_string<Args...> format, Args&&... args) noexcept { log(LogLevel::Info, {}, format, std::forward<Args>(args)...); }
73 template <class... Args> void warn(std::format_string<Args...> format, Args&&... args) noexcept { log(LogLevel::Warn, {}, format, std::forward<Args>(args)...); }
74 template <class... Args> void error(std::format_string<Args...> format, Args&&... args) noexcept { log(LogLevel::Error, {}, format, std::forward<Args>(args)...); }
75 template <class... Args> void fatal(std::format_string<Args...> format, Args&&... args) noexcept { log(LogLevel::Fatal, {}, format, std::forward<Args>(args)...); }
76
77 // ── Pre-formatted text ───────────────────────────────────────────────
79 void log(LogLevel level, std::string_view message, const SourceLocation& source = {}) noexcept;
80
81 void trace(std::string_view message) noexcept { log(LogLevel::Trace, message); }
82 void debug(std::string_view message) noexcept { log(LogLevel::Debug, message); }
83 void info(std::string_view message) noexcept { log(LogLevel::Info, message); }
84 void warn(std::string_view message) noexcept { log(LogLevel::Warn, message); }
85 void error(std::string_view message) noexcept { log(LogLevel::Error, message); }
86 void fatal(std::string_view message) noexcept { log(LogLevel::Fatal, message); }
87
89 void flush() noexcept;
90
91 private:
92 struct SinkList;
93
94 void dispatch(const LogMessage& message) noexcept;
95
96 std::string _name;
97 std::atomic<LogLevel> _level;
98 std::atomic<LogLevel> _flushLevel{LogLevel::Off};
99 std::unique_ptr<SinkList> _sinks;
100 };
101
102}
Named logger that formats messages and sends them to its sinks; thread-safe.
Definition Logger.hpp:24
void fatal(std::string_view message) noexcept
Definition Logger.hpp:86
void trace(std::string_view message) noexcept
Definition Logger.hpp:81
void setLevel(LogLevel level) noexcept
Minimum level to log; LogLevel::Off disables the logger.
Definition Logger.hpp:37
std::size_t sinkCount() const noexcept
void log(LogLevel level, const SourceLocation &source, std::format_string< Args... > format, Args &&... args) noexcept
Logs a message at level with its source location.
Definition Logger.hpp:59
void warn(std::string_view message) noexcept
Definition Logger.hpp:84
void flushOn(LogLevel level) noexcept
Flushes the sinks after every message at level or above.
Definition Logger.hpp:44
void warn(std::format_string< Args... > format, Args &&... args) noexcept
Definition Logger.hpp:73
Logger & operator=(const Logger &)=delete
void debug(std::format_string< Args... > format, Args &&... args) noexcept
Definition Logger.hpp:71
LogLevel getFlushLevel() const noexcept
Definition Logger.hpp:45
Logger(std::string name, LogLevel level=LogLevel::Trace)
Creates a logger without sinks; add them with addSink().
void log(LogLevel level, std::string_view message, const SourceLocation &source={}) noexcept
Logs message as is, without formatting.
void info(std::string_view message) noexcept
Definition Logger.hpp:83
void info(std::format_string< Args... > format, Args &&... args) noexcept
Definition Logger.hpp:72
void trace(std::format_string< Args... > format, Args &&... args) noexcept
Logs at a fixed level: trace(), debug(), info(), warn(), error() and fatal().
Definition Logger.hpp:70
Logger(const Logger &)=delete
void vlog(LogLevel level, const SourceLocation &source, std::string_view category, std::string_view format, std::format_args args) noexcept
Same as log() with a runtime format string, for wrappers.
void error(std::format_string< Args... > format, Args &&... args) noexcept
Definition Logger.hpp:74
bool removeSink(const std::shared_ptr< ISink > &sink) noexcept
Removes sink; returns false if it was not attached.
void flush() noexcept
Flushes every sink.
bool shouldLog(LogLevel level) const noexcept
Whether a message at level would be logged.
Definition Logger.hpp:40
void addSink(std::shared_ptr< ISink > sink, RingBufferSink &history)
Adds sink after replaying the messages kept by history into it.
const std::string & getName() const noexcept
Definition Logger.hpp:33
void addSink(std::shared_ptr< ISink > sink)
LogLevel getLevel() const noexcept
Definition Logger.hpp:38
void fatal(std::format_string< Args... > format, Args &&... args) noexcept
Definition Logger.hpp:75
void debug(std::string_view message) noexcept
Definition Logger.hpp:82
void clearSinks() noexcept
void error(std::string_view message) noexcept
Definition Logger.hpp:85
Keeps the latest messages in memory, to replay them into another sink later.
Definition RingBufferSink.hpp:12
Thread-safe logging: loggers, sinks, patterns and macros.
Definition Terminal.hpp:8
LogLevel
Severity of a message, from Trace to Fatal; Off disables logging.
Definition LogLevel.hpp:11
A log record as passed to the sinks.
Definition LogMessage.hpp:18
File, function and line of a log call.
Definition SourceLocation.hpp:6