RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
LogRegistry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "RST/log/Log.hpp"
4
5#include <atomic>
6#include <cstddef>
7#include <functional>
8#include <memory>
9#include <mutex>
10#include <string>
11#include <string_view>
12#include <unordered_map>
13#include <vector>
14
15namespace RST::Log {
16
17 class ISink;
18
20 class Registry {
21 public:
22 Registry(const Registry&) = delete;
23 Registry& operator=(const Registry&) = delete;
24
25 static Registry& instance();
26
28 [[nodiscard]] static std::shared_ptr<Logger> get(std::string_view name);
30 [[nodiscard]] static std::shared_ptr<Logger> defaultLogger();
31
33 [[nodiscard]] static Logger& defaultLoggerRef();
34
36 static void setGlobalLevel(LogLevel level);
38 static void addGlobalSink(std::shared_ptr<ISink> sink);
40 static void reset();
41
46 static std::shared_ptr<Logger> init(std::string_view appName = "app", LogLevel level = LogLevel::Info, bool toFile = false);
47
48 private:
49 struct NameHash {
50 using is_transparent = void;
51 [[nodiscard]] std::size_t operator()(std::string_view name) const noexcept { return std::hash<std::string_view>{}(name); }
52 };
53
54 Registry() = default;
55
56 std::shared_ptr<Logger> getOrCreate(std::string_view name);
57
58 std::mutex _mutex;
59 std::unordered_map<std::string, std::shared_ptr<Logger>, NameHash, std::equal_to<>> _loggers;
60 std::vector<std::shared_ptr<ISink>> _globalSinks;
61 LogLevel _globalLevel = LogLevel::Trace;
62 bool _initialized = false;
63 std::atomic<Logger*> _default{nullptr};
64 };
65
66}
67
71#define RST_LOG(level, ...) RST_LOG_L(::RST::Log::Registry::defaultLoggerRef(), level, __VA_ARGS__)
72
73#define RST_LOG_TRACE(...) RST_LOGGER_TRACE(::RST::Log::Registry::defaultLoggerRef(), __VA_ARGS__)
74#define RST_LOG_DEBUG(...) RST_LOGGER_DEBUG(::RST::Log::Registry::defaultLoggerRef(), __VA_ARGS__)
75#define RST_LOG_INFO(...) RST_LOGGER_INFO(::RST::Log::Registry::defaultLoggerRef(), __VA_ARGS__)
76#define RST_LOG_WARN(...) RST_LOGGER_WARN(::RST::Log::Registry::defaultLoggerRef(), __VA_ARGS__)
77#define RST_LOG_ERROR(...) RST_LOGGER_ERROR(::RST::Log::Registry::defaultLoggerRef(), __VA_ARGS__)
78#define RST_LOG_FATAL(...) RST_LOGGER_FATAL(::RST::Log::Registry::defaultLoggerRef(), __VA_ARGS__)
80
81#if defined(RST_LOG_LEGACY_MACROS)
82# define LOG_TRACE(...) RST_LOG_TRACE(__VA_ARGS__)
83# define LOG_DEBUG(...) RST_LOG_DEBUG(__VA_ARGS__)
84# define LOG_INFO(...) RST_LOG_INFO(__VA_ARGS__)
85# define LOG_WARN(...) RST_LOG_WARN(__VA_ARGS__)
86# define LOG_ERROR(...) RST_LOG_ERROR(__VA_ARGS__)
87# define LOG_FATAL(...) RST_LOG_FATAL(__VA_ARGS__)
88#endif
Named logger that formats messages and sends them to its sinks; thread-safe.
Definition Logger.hpp:24
Global registry of named loggers, including the default logger used by the RST_LOG_* macros.
Definition LogRegistry.hpp:20
static Registry & instance()
static void reset()
Removes every logger and global sink, and resets the global level.
Registry(const Registry &)=delete
static std::shared_ptr< Logger > defaultLogger()
The logger named default, used by the RST_LOG_* macros.
static std::shared_ptr< Logger > init(std::string_view appName="app", LogLevel level=LogLevel::Info, bool toFile=false)
Quick setup: logs from level to the console, and to <appName>.log if toFile is true.
Registry & operator=(const Registry &)=delete
static void addGlobalSink(std::shared_ptr< ISink > sink)
Adds sink to every logger, current and future.
static Logger & defaultLoggerRef()
Same as defaultLogger(), without the reference counting.
static void setGlobalLevel(LogLevel level)
Sets the level of every logger, current and future.
static std::shared_ptr< Logger > get(std::string_view name)
Logger named name, created on first use with the global sinks and level.
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