-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.mpp
164 lines (145 loc) · 4.76 KB
/
Logger.mpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
module;
#include <cstdio>
export module CppUtils.Logger;
import std;
import CppUtils.String;
import CppUtils.Terminal.TextColor;
import CppUtils.Terminal.TextModifier;
import CppUtils.Terminal.Utility;
// Todo: log le datetime
// Todo: log le stacktrace ( https://en.cppreference.com/w/cpp/utility/stacktrace_entry )
export namespace CppUtils
{
template<String::Hasher loggerName = String::Hash{}>
struct Logger final
{
inline Logger() = delete;
struct Formatter final
{
Terminal::TextModifier textModifier;
std::string message;
};
template<String::Hasher logType = String::Hash{}>
static inline auto format(std::string_view message) -> Formatter
{
return {
Terminal::TextModifier{},
std::string{message}};
}
template<String::Hasher logType = String::Hash{}, class... Args>
static inline auto print(std::ostream& ostream, std::format_string<Args...> fmt, Args&&... args) -> void
{
auto [textModifier, message] = format<logType>(std::format(fmt, std::forward<Args>(args)...));
// Todo C++23: std::print(ostream, "{}", std::move(message));
ostream << message;
}
template<String::Hasher logType = String::Hash{}, class... Args>
static inline auto print(std::format_string<Args...> fmt, Args&&... args) -> void
{
using namespace String::Literals;
auto [textModifier, message] = format<logType>(std::format(fmt, std::forward<Args>(args)...));
if constexpr (logType == "error"_hash)
std::print(stderr, "{}", std::move(message));
else
std::print("{}", std::move(message));
}
template<String::Hasher logType = String::Hash{}>
static inline auto printSeparator(std::ostream& ostream) -> void
{
using namespace std::literals;
auto line = ""s;
auto terminalWidth = Terminal::getTerminalSize().width;
for (auto i = 0uz; i < terminalWidth; ++i)
line += "─";
auto [textModifier, message] = format<logType>("");
// Todo C++23: std::print(ostream, "{}", std::move(line));
ostream << line << '\n';
}
template<String::Hasher logType = String::Hash{}>
static inline auto printSeparator() -> void
{
using namespace std::literals;
auto line = ""s;
auto terminalWidth = Terminal::getTerminalSize().width;
for (auto i = 0uz; i < terminalWidth; ++i)
line += "─";
auto [textModifier, message] = format<logType>("");
std::print("{}\n", std::move(line));
}
};
template<>
template<>
inline auto Logger<"CppUtils">::format<"info">(std::string_view message) -> Formatter
{
return {
Terminal::TextModifier{stdout, Terminal::TextColor::TextColorEnum::Default},
std::format("{}\n", message)};
}
template<>
template<>
inline auto Logger<"CppUtils">::format<"important">(std::string_view message) -> Formatter
{
return {
Terminal::TextModifier{stdout, Terminal::TextColor::TextColorEnum::Cyan},
std::format("🛈 {}\n", message)};
}
template<>
template<>
inline auto Logger<"CppUtils">::format<"success">(std::string_view message) -> Formatter
{
return {
Terminal::TextModifier{stdout, Terminal::TextColor::TextColorEnum::Green},
std::format("{} ✓\n", message)};
}
template<>
template<>
inline auto Logger<"CppUtils">::format<"debug">(std::string_view message) -> Formatter
{
return {
Terminal::TextModifier{stdout, Terminal::TextColor::TextColorEnum::Magenta},
std::format("🛠 DEBUG: {}\n", message)};
}
template<>
template<>
inline auto Logger<"CppUtils">::format<"detail">(std::string_view message) -> Formatter
{
return {
Terminal::TextModifier{stdout, Terminal::TextColor::TextColorEnum::Blue},
std::format("{}\n", message)};
}
template<>
template<>
inline auto Logger<"CppUtils">::format<"warning">(std::string_view message) -> Formatter
{
return {
Terminal::TextModifier{stdout, Terminal::TextColor::TextColorEnum::Yellow},
std::format("{:^{}}\n{} ⚠\n", "🛆 WARNING 🛆", Terminal::getTerminalSize().width, message)};
}
template<>
template<>
inline auto Logger<"CppUtils">::format<"error">(std::string_view message) -> Formatter
{
return {
Terminal::TextModifier{stderr, Terminal::TextColor::TextColorEnum::Red},
std::format("{:^{}}\n{} ✕\n", "🛇 ERROR 🛇", Terminal::getTerminalSize().width, message)};
}
inline auto logException(const std::exception& exception, std::size_t depth = 0) -> void
{
using Logger = Logger<"CppUtils">;
if (depth == 0)
Logger::print<"error">("{}", exception.what());
[[maybe_unused]] auto textModifier = (depth == 0) ? Terminal::TextModifier{stdout, Terminal::TextColor::TextColorEnum::Red} : Terminal::TextModifier{};
if (depth > 0)
Logger::print("{}{}\n", std::string(depth * 2, ' '), exception.what());
try
{
std::rethrow_if_nested(exception);
}
catch (const std::exception& nestedException)
{
logException(nestedException, depth + 1);
}
catch (...)
{}
}
}