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