Nui Engine
A game engine framework
Loading...
Searching...
No Matches
Log.h
1#pragma once
2#include "Core/Utils/Timestamp.h"
3#include <fstream>
4
5namespace Nui::Log
6{
7 namespace Internal
8 {
12 class LogFile
13 {
14 public:
15 LogFile() = default;
16
17 /*
18 * @brief Constructor for LogFile - Opens the log file
19 * @param path The path to the log file
20 */
21 LogFile(const fs::path& path);
22
23 /*
24 * @brief Destructor for LogFile - Closes the log file
25 */
26 ~LogFile();
27
28 /*
29 * @brief Writes a message to the log file
30 * @param message The message to write
31 */
32 void Write(const String& message);
33
34 /*
35 * @brief Flushes the log file buffer
36 */
37 void Flush();
38
39 private:
40 /*
41 * @brief The path to the log file
42 */
43 String m_path;
44
45 /*
46 * @brief The log file stream
47 */
48 std::ofstream m_file;
49 };
50
55 void OpenLogFile(const fs::path& path);
56
61 void CloseLogFile();
62 } // namespace Internal
63
68 enum class LogLevel
69 {
73 Debug = 0,
74
78 Info,
79
83 Warn,
84
88 Error,
89
93 Fatal,
94
98 Exception,
99 };
100
106 struct LogEntry
107 {
116 LogEntry(LogLevel level, StringView category, StringView message, Nui::Stacktrace trace = Nui::Stacktrace::current());
117
121 LogLevel Level;
122
126 String Category;
127
131 String Message;
132
137
141 Stacktrace Stacktrace;
142 };
143
144 template <typename... Args>
145 constexpr String Format(Args&&... args)
146 {
147 // Reference:
148
149 // Generate formatting string
150 std::array<char, sizeof...(Args) * 3 + 1> braces{};
151 constexpr const char c[4] = "{} ";
152
153 for (U32 i = 0u; i != braces.size(); i++)
154 {
155 braces[i] = c[i % 3];
156 }
157
158 braces.back() = '\0';
159
160 return std::vformat(StringView(braces.data()), std::make_format_args(args...));
161 }
162
163 template <typename... Args>
164 constexpr WString FormatW(Args&&... args)
165 {
166 // Reference:
167
168 // Generate formatting string
169 std::array<wchar_t, sizeof...(Args) * 3 + 1> braces{};
170 constexpr const wchar_t c[4] = L"{} ";
171
172 for (U32 i = 0u; i != braces.size(); i++)
173 {
174 braces[i] = c[i % 3];
175 }
176
177 braces.back() = L'\0';
178
179 return std::vformat(WStringView(braces.data()), std::make_wformat_args(args...));
180 }
181
187 void Log(const LogEntry& entry);
188
199 void Assert(bool condition, StringView conditionString, StringView message, StringView file, I32 line, Nui::Stacktrace trace = Nui::Stacktrace::current());
200} // namespace Nui::Log
201
202#define NUI_LOG(Level, Category, ...) Nui::Log::Log(Nui::Log::LogEntry(Nui::Log::LogLevel::Level, #Category, Nui::Log::Format(__VA_ARGS__)))
203
204#if NUI_DEBUG
205#define NUI_ASSERT(Condition, ...) Nui::Log::Assert(Condition, #Condition, Nui::Log::Format(__VA_ARGS__), __FILE__, __LINE__)
206#else
207#define NUI_ASSERT(Condition, ...)
208#endif
Represents a log file.
Definition Log.h:13
Timestamp utility class, wraps chrono::system_clock::time_point.
Definition Timestamp.h:10
Represents a log entry with its level, category, message, time, and stack trace.
Definition Log.h:107
String Message
The log message.
Definition Log.h:131
String Category
The log category.
Definition Log.h:126
LogLevel Level
The log level.
Definition Log.h:121
LogEntry(LogLevel level, StringView category, StringView message, Nui::Stacktrace trace=Nui::Stacktrace::current())
Constructor for LogEntry.
Definition Log.cpp:141
Stacktrace Stacktrace
The stack trace associated with the log entry.
Definition Log.h:141
Timestamp Timestamp
The timestamp of the log entry.
Definition Log.h:136