Khaotic Engine Reborn
Loading...
Searching...
No Matches
Logger.h
1#pragma once
2#include <fstream>
3#include <string>
4#include <Windows.h>
5#include <chrono>
6#include <iomanip>
7#include <sstream>
8#include <filesystem>
9#include <deque>
10#include <unordered_set>
11#include <imgui.h>
12
13class Logger
14{
15public:
16
17 static Logger& Get()
18 {
19 static Logger instance;
20 return instance;
21 }
22
23 Logger(Logger const&) = delete;
24 void operator=(Logger const&) = delete;
25
26 enum class LogLevel
27 {
28 Info,
29 Warning,
30 Error,
31 Shutdown,
32 Initialize,
33 Update,
34 Render,
35 Input,
36 Physics,
37 Audio,
38 Network,
39 Scripting,
40 AI,
41 Resource,
42 Memory,
43 Debug,
44 Count // Do not use this, it's just to get the number of log levels it must at the end
45 };
46
47 // Return the size of the enum class LogLevel as a constant integer
48 static constexpr int LogLevelCount = static_cast<int>(LogLevel::Count);
49
50 struct LogEntry
51 {
52 std::string message;
53 LogLevel level;
54 };
55
57 {
58 const char* name;
59 int value;
60 ImVec4 color;
61 };
62
63 static const LogLevelInfo GetLogLevelInfo(LogLevel level)
64 {
65 switch (level)
66 {
67 case LogLevel::Info: return LogLevelInfo{ "Info", 0, ImVec4(0.0f, 1.0f, 0.0f, 1.0f) };
68 case LogLevel::Warning: return LogLevelInfo{ "Warning", 1, ImVec4(1.0f, 1.0f, 0.0f, 1.0f) };
69 case LogLevel::Error: return LogLevelInfo{ "Error", 2, ImVec4(1.0f, 0.0f, 0.0f, 1.0f) };
70 case LogLevel::Shutdown: return LogLevelInfo{ "shutdown", 3, ImVec4(0.5f, 0.0f, 0.0f, 1.0f) };
71 case LogLevel::Initialize: return LogLevelInfo{ "initialize", 4, ImVec4(0.0f, 1.0f, 1.0f, 1.0f) };
72 case LogLevel::Update: return LogLevelInfo{ "Update", 5, ImVec4(1.0f, 0.0f, 1.0f, 1.0f) };
73 case LogLevel::Render: return LogLevelInfo{ "render", 6, ImVec4(1.0f, 1.0f, 1.0f, 1.0f) };
74 case LogLevel::Input: return LogLevelInfo{ "Input", 7, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
75 case LogLevel::Physics: return LogLevelInfo{ "physics", 8, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
76 case LogLevel::Audio: return LogLevelInfo{ "Audio", 9, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
77 case LogLevel::Network: return LogLevelInfo{ "Network", 10, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
78 case LogLevel::Scripting: return LogLevelInfo{ "Scripting", 11, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
79 case LogLevel::AI: return LogLevelInfo{ "AI", 12, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
80 case LogLevel::Resource: return LogLevelInfo{ "Resource", 13, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
81 case LogLevel::Memory: return LogLevelInfo{ "Memory", 14, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
82 case LogLevel::Debug: return LogLevelInfo{ "Debug", 15, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
83 default: return LogLevelInfo{ "Unknown", 16, ImVec4(1.0f, 1.0f, 1.0f, 1.0f) };
84 }
85 }
86
87 Logger()
88 {
89 char* appdata = nullptr;
90 size_t len;
91 _dupenv_s(&appdata, &len, "APPDATA");
92 if (appdata == nullptr)
93 {
94 m_appdataPath = "log.log";
95 }
96 else
97 {
98 m_appdataPath = appdata;
99 }
100 free(appdata);
101 std::string directoryPath = m_appdataPath + "\\Khaotic Engine";
102 CreateDirectoryA(directoryPath.c_str(), NULL);
103
104 ManageLogFiles(directoryPath);
105
106 m_logFilePath = directoryPath + "\\" + m_logFileName;
107
108 // Enable only the Error warning and shutdown log levels
109 for (int i = 0; i < LogLevelCount; i++)
110 {
111 m_disabledLogLevels[i] = true;
112
113 if (i == static_cast<int>(LogLevel::Error) || i == static_cast<int>(LogLevel::Warning) || i == static_cast<int>(LogLevel::Shutdown))
114 {
115 m_disabledLogLevels[i] = false;
116
117 }
118 }
119
120 }
121
122 // ecrit un message dans le fichier de log et le stocke dans le buffer
123 void Log(const std::string& message, const std::string& fileName, int lineNumber, LogLevel level = LogLevel::Info)
124 {
125
126 auto now = std::chrono::system_clock::now();
127 auto in_time_t = std::chrono::system_clock::to_time_t(now);
128
129 std::tm buf;
130 localtime_s(&buf, &in_time_t);
131
132 // Obtenez les millisecondes à partir de maintenant
133 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
134
135 // Utilisez LogLevelToString pour obtenir la chaîne de caractères du niveau de log
136 std::string levelStr = GetLogLevelInfo(level).name;
137
138 std::stringstream ss;
139 ss << "[" << std::put_time(&buf, "%Y-%m-%d") << "] "
140 << "[" << std::put_time(&buf, "%X") << "." << std::setfill('0') << std::setw(3) << ms.count() << "] "
141 << "[" << levelStr << "] "
142 << "[" << fileName << ":" << lineNumber << "] "
143 << message;
144
145 Log(ss.str(), level);
146
147 std::ofstream file(m_logFilePath, std::ios::app);
148 if (file.is_open())
149 {
150 file << ss.str() << std::endl;
151 file.close();
152 }
153 }
154
155 // ecrit un message dans la console
156 void Log(const std::string& message, LogLevel level)
157 {
158
159 // Si le niveau de log est désactivé, ne faites rien
160 if (m_disabledLogLevels[GetLogLevelInfo(level).value])
161 {
162 return;
163 }
164
165 if (logBuffer.size() >= logBufferSize)
166 {
167 logBuffer.pop_front();
168 }
169 logBuffer.push_back({ message, level });
170 }
171
172 const std::deque<LogEntry>& GetLogBuffer() const { return logBuffer; }
173
174 void ManageLogFiles(const std::string& directoryPath)
175 {
176 std::vector<std::filesystem::path> logFiles;
177
178 // Parcourez tous les fichiers dans le dossier
179 for (const auto& entry : std::filesystem::directory_iterator(directoryPath))
180 {
181 // Si le fichier est un fichier de log, ajoutez-le à la liste
182 if (entry.path().extension() == ".log")
183 {
184 logFiles.push_back(entry.path());
185 }
186 }
187
188 // Si nous avons plus de trois fichiers de log, supprimez le plus ancien
189 while (logFiles.size() >= 3)
190 {
191 // Triez les fichiers par date de modification, le plus ancien en premier
192 std::sort(logFiles.begin(), logFiles.end(), [](const std::filesystem::path& a, const std::filesystem::path& b)
193 {
194 return std::filesystem::last_write_time(a) < std::filesystem::last_write_time(b);
195 });
196
197 // Supprimez le fichier le plus ancien
198 std::filesystem::remove(logFiles[0]);
199
200 // Supprimez-le de la liste
201 logFiles.erase(logFiles.begin());
202 }
203
204 // Créez un nouveau fichier de log pour cette exécution
205 auto now = std::chrono::system_clock::now();
206 auto in_time_t = std::chrono::system_clock::to_time_t(now);
207 std::tm buf;
208 localtime_s(&buf, &in_time_t);
209
210 std::stringstream ss;
211 ss << "Khaotic_log_" << std::put_time(&buf, "%Y_%m_%d_%Hh%Mm%Ss") << ".log";
212 m_logFileName = ss.str();
213 }
214
215 bool m_disabledLogLevels[LogLevelCount];
216 std::string m_logFilePath;
217
218private:
219 std::string m_filename;
220 std::string m_appdataPath;
221 std::string m_logFileName;
222
223 std::deque<LogEntry> logBuffer;
224 const size_t logBufferSize = 100;
225
226};
Definition Logger.h:51