LeviLamina
Loading...
Searching...
No Matches
Logger.h
1#pragma once
2
3#include <memory>
4#include <string>
5#include <string_view>
6#include <utility>
7
8#if _HAS_CXX23
9#include <expected>
10#endif
11
12#include "ll/api/base/Concepts.h" // IWYU pragma: keep
13#include "ll/api/base/Macro.h"
14#include "ll/api/coro/Generator.h"
15#include "ll/api/io/LogLevel.h"
16#include "ll/api/io/Sink.h"
17
18#include "fmt/chrono.h" // IWYU pragma: keep
19#include "fmt/color.h"
20#include "fmt/core.h"
21#include "fmt/format.h"
22#include "fmt/os.h" // IWYU pragma: keep
23#include "fmt/ranges.h" // IWYU pragma: keep
24#include "fmt/std.h" // IWYU pragma: keep
25
26
27namespace ll::io {
28class LoggerRegistry;
29class Logger : public std::enable_shared_from_this<Logger> {
30 LLAPI void printStr(LogLevel, std::string&&) const noexcept;
31
32 friend LoggerRegistry;
33
34 struct Impl;
35
36 std::unique_ptr<Impl> impl;
37
38 struct PrivateTag {
39 explicit PrivateTag() = default;
40 };
41
42public:
43 template <typename... Args>
44 void log(LogLevel level, fmt::format_string<Args...> fmt, Args&&... args) const {
45 if (shouldLog(level)) printStr(level, fmt::vformat(fmt.get(), fmt::make_format_args(args...)));
46 }
47 void log(LogLevel level, std::string&& msg) const {
48 if (shouldLog(level)) printStr(level, std::move(msg));
49 }
50 template <ll::concepts::IsString S>
51 void log(LogLevel level, S const& msg) const {
52 if (shouldLog(level)) printStr(level, std::string{msg});
53 }
54
55 template <typename... Args>
56 void fatal(fmt::format_string<Args...> fmt, Args&&... args) const {
57 log(LogLevel::Fatal, fmt, std::forward<Args>(args)...);
58 }
59 void fatal(std::string&& msg) const { log(LogLevel::Fatal, std::move(msg)); }
60 template <ll::concepts::IsString S>
61 void fatal(S const& msg) const {
62 log(LogLevel::Fatal, msg);
63 }
64
65 template <typename... Args>
66 void error(fmt::format_string<Args...> fmt, Args&&... args) const {
67 log(LogLevel::Error, fmt, std::forward<Args>(args)...);
68 }
69 void error(std::string&& msg) const { log(LogLevel::Error, std::move(msg)); }
70 template <ll::concepts::IsString S>
71 void error(S const& msg) const {
72 log(LogLevel::Error, msg);
73 }
74
75 template <typename... Args>
76 void warn(fmt::format_string<Args...> fmt, Args&&... args) const {
77 log(LogLevel::Warn, fmt, std::forward<Args>(args)...);
78 }
79 void warn(std::string&& msg) const { log(LogLevel::Warn, std::move(msg)); }
80 template <ll::concepts::IsString S>
81 void warn(S const& msg) const {
82 log(LogLevel::Warn, msg);
83 }
84
85 template <typename... Args>
86 void info(fmt::format_string<Args...> fmt, Args&&... args) const {
87 log(LogLevel::Info, fmt, std::forward<Args>(args)...);
88 }
89 void info(std::string&& msg) const { log(LogLevel::Info, std::move(msg)); }
90 template <ll::concepts::IsString S>
91 void info(S const& msg) const {
92 log(LogLevel::Info, msg);
93 }
94
95 template <typename... Args>
96 void debug(fmt::format_string<Args...> fmt, Args&&... args) const {
97 log(LogLevel::Debug, fmt, std::forward<Args>(args)...);
98 }
99 void debug(std::string&& msg) const { log(LogLevel::Debug, std::move(msg)); }
100 template <ll::concepts::IsString S>
101 void debug(S const& msg) const {
102 log(LogLevel::Debug, msg);
103 }
104
105 template <typename... Args>
106 void trace(fmt::format_string<Args...> fmt, Args&&... args) const {
107 log(LogLevel::Trace, fmt, std::forward<Args>(args)...);
108 }
109 void trace(std::string&& msg) const { log(LogLevel::Trace, std::move(msg)); }
110 template <ll::concepts::IsString S>
111 void trace(S const& msg) const {
112 log(LogLevel::Trace, msg);
113 }
114
115 LLAPI ~Logger();
116
117 explicit Logger(PrivateTag, std::string_view);
118
119 LLNDAPI std::string const& getTitle() const noexcept;
120
121 LLNDAPI LogLevel getLevel() const noexcept;
122
123 LLNDAPI bool shouldLog(LogLevel level) const noexcept;
124
125 LLAPI void setLevel(LogLevel level);
126
127 LLAPI void setFlushLevel(LogLevel level);
128
129 LLAPI void setFormatter(Polymorphic<Formatter> formatter);
130
131 LLAPI void flush() const;
132
133 LLAPI void clearSink() const;
134
135 LLAPI size_t addSink(std::shared_ptr<SinkBase> sink) const;
136
137 LLAPI std::shared_ptr<SinkBase> getSink(size_t index) const;
138
139 LLNDAPI coro::Generator<SinkBase&> sinks() const;
140};
141} // namespace ll::io
Definition IndirectValue.h:47
Definition LoggerRegistry.h:10
Definition Logger.h:29
Definition Generator.h:13