Aria 2.0.0
C++23 MVVM framework (C++20 minimum) — reactive, coroutine-first, ABI-layered
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "aria/abi/export.hpp"
4#include <functional>
5#include <memory>
6#include <string_view>
7
8namespace aria::runtime {
9
10enum class LogLevel { Trace, Debug, Info, Warn, Error, Fatal };
11
15public:
16 using Sink = std::function<void(LogLevel, std::string_view, std::string_view /*message*/)>;
17
19 static Logger& instance() noexcept;
20
21 Logger(const Logger&) = delete;
22 Logger& operator=(const Logger&) = delete;
23 Logger(Logger&&) = delete;
24 Logger& operator=(Logger&&) = delete;
25
26 void set_sink(Sink sink);
27 void set_level(LogLevel level) noexcept;
28 [[nodiscard]] LogLevel level() const noexcept;
29
32 void log(LogLevel level, std::string_view category, std::string_view message) noexcept;
33
34 // Shorthands
35 void trace(std::string_view category, std::string_view m) { log(LogLevel::Trace, category, m); }
36 void debug(std::string_view category, std::string_view m) { log(LogLevel::Debug, category, m); }
37 void info (std::string_view category, std::string_view m) { log(LogLevel::Info, category, m); }
38 void warn (std::string_view category, std::string_view m) { log(LogLevel::Warn, category, m); }
39 void error(std::string_view category, std::string_view m) { log(LogLevel::Error, category, m); }
40 void fatal(std::string_view category, std::string_view m) { log(LogLevel::Fatal, category, m); }
41
42private:
43 Logger();
44 ~Logger();
45
46 struct Impl;
47 // RAII pImpl; C4251 on the unique_ptr member is a false positive for an
48 // incomplete opaque pointee consumed only via non-template API.
49#ifdef _MSC_VER
50# pragma warning(push)
51# pragma warning(disable: 4251)
52#endif
53 std::unique_ptr<Impl> impl_;
54#ifdef _MSC_VER
55# pragma warning(pop)
56#endif
57};
58
59ARIA_RUNTIME_API const char* level_name(LogLevel l) noexcept;
60
61} // namespace aria::runtime
Process-wide logger.
Definition logger.hpp:14
static Logger & instance() noexcept
Get the singleton (resolved from the runtime shared library, never duplicated).
void log(LogLevel level, std::string_view category, std::string_view message) noexcept
Invoke the current sink without copying its captures.
void set_sink(Sink sink)
void set_level(LogLevel level) noexcept
void trace(std::string_view category, std::string_view m)
Definition logger.hpp:35
void error(std::string_view category, std::string_view m)
Definition logger.hpp:39
LogLevel level() const noexcept
void debug(std::string_view category, std::string_view m)
Definition logger.hpp:36
Logger(const Logger &)=delete
void info(std::string_view category, std::string_view m)
Definition logger.hpp:37
void warn(std::string_view category, std::string_view m)
Definition logger.hpp:38
void fatal(std::string_view category, std::string_view m)
Definition logger.hpp:40
std::function< void(LogLevel, std::string_view, std::string_view)> Sink
Definition logger.hpp:16
#define ARIA_RUNTIME_API
Definition export.hpp:30
Definition container.hpp:11
LogLevel
Definition logger.hpp:10
@ Info
Definition logger.hpp:10
@ Warn
Definition logger.hpp:10
@ Fatal
Definition logger.hpp:10
@ Error
Definition logger.hpp:10
@ Debug
Definition logger.hpp:10
@ Trace
Definition logger.hpp:10
const char * level_name(LogLevel l) noexcept
Definition validation_key.hpp:110
One uniform error record.
Definition error.hpp:129