Khaotic Engine Reborn
Loading...
Searching...
No Matches
Logger Class Reference

Classes

struct  LogEntry
 
struct  LogLevelInfo
 

Public Types

enum class  LogLevel {
  Info , Warning , Error , Shutdown ,
  Initialize , Update , Render , Input ,
  Physics , Audio , Network , Scripting ,
  AI , Resource , Memory , Debug ,
  Count
}
 

Public Member Functions

 Logger (Logger const &)=delete
 
void operator= (Logger const &)=delete
 
 Logger ()
 
void Log (const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
 
void Log (const std::string &message, LogLevel level)
 
const std::deque< LogEntry > & GetLogBuffer () const
 
void ManageLogFiles (const std::string &directoryPath)
 

Static Public Member Functions

static LoggerGet ()
 
static const LogLevelInfo GetLogLevelInfo (LogLevel level)
 

Public Attributes

bool m_disabledLogLevels [LogLevelCount]
 
std::string m_logFilePath
 

Static Public Attributes

static constexpr int LogLevelCount = static_cast<int>(LogLevel::Count)
 

Detailed Description

Definition at line 13 of file Logger.h.

Member Enumeration Documentation

◆ LogLevel

enum class Logger::LogLevel
strong

Enum class representing different log levels. Each log level has a corresponding color for display purposes. The last entry, Count, is used to determine the number of log levels.

Definition at line 37 of file Logger.h.

38 {
39 Info,
40 Warning,
41 Error,
42 Shutdown,
43 Initialize,
44 Update,
45 Render,
46 Input,
47 Physics,
48 Audio,
49 Network,
50 Scripting,
51 AI,
52 Resource,
53 Memory,
54 Debug,
55 Count // Do not use this, it's just to get the number of log levels it must at the end
56 };

Constructor & Destructor Documentation

◆ Logger() [1/2]

Logger::Logger ( Logger const & )
delete

Delete the copy constructor and assignment operator to prevent copying.

◆ Logger() [2/2]

Logger::Logger ( )
inline

Constructor for the Logger class. Initializes the logger, sets up the log file path, and manages log files.

Definition at line 116 of file Logger.h.

117 {
118 char* appdata = nullptr;
119 size_t len;
120 _dupenv_s(&appdata, &len, "APPDATA");
121 if (appdata == nullptr)
122 {
123 m_appdataPath = "log.log";
124 }
125 else
126 {
127 m_appdataPath = appdata;
128 }
129 free(appdata);
130 std::string directoryPath = m_appdataPath + "\\Khaotic Engine";
131 CreateDirectoryA(directoryPath.c_str(), NULL);
132
133 ManageLogFiles(directoryPath);
134
135 m_logFilePath = directoryPath + "\\" + m_logFileName;
136
137 // Enable only the Error warning and shutdown log levels
138 for (int i = 0; i < LogLevelCount; i++)
139 {
140 m_disabledLogLevels[i] = true;
141
142 if (i == static_cast<int>(LogLevel::Error) || i == static_cast<int>(LogLevel::Warning) || i == static_cast<int>(LogLevel::Shutdown))
143 {
144 m_disabledLogLevels[i] = false;
145
146 }
147 }
148
149 }
void ManageLogFiles(const std::string &directoryPath)
Definition Logger.h:230

Member Function Documentation

◆ Get()

static Logger & Logger::Get ( )
inlinestatic

Get the singleton instance of the Logger class.

Returns
A reference to the Logger instance.

Definition at line 20 of file Logger.h.

21 {
22 static Logger instance;
23 return instance;
24 }

◆ GetLogBuffer()

const std::deque< LogEntry > & Logger::GetLogBuffer ( ) const
inline

Get the loggBuffer deque.

Returns
A constant reference to the logBuffer deque containing log entries.

Definition at line 220 of file Logger.h.

220{ return logBuffer; }

◆ GetLogLevelInfo()

static const LogLevelInfo Logger::GetLogLevelInfo ( LogLevel level)
inlinestatic

Get the LogLevelInfo for a given log level. This function returns a LogLevelInfo struct containing the name, value, and color of the log level.

Parameters
levelThe log level for which to get the information.
Returns
A LogLevelInfo struct containing the information for the specified log level.

Definition at line 88 of file Logger.h.

89 {
90 switch (level)
91 {
92 case LogLevel::Info: return LogLevelInfo{ "Info", 0, ImVec4(0.0f, 1.0f, 0.0f, 1.0f) };
93 case LogLevel::Warning: return LogLevelInfo{ "Warning", 1, ImVec4(1.0f, 1.0f, 0.0f, 1.0f) };
94 case LogLevel::Error: return LogLevelInfo{ "Error", 2, ImVec4(1.0f, 0.0f, 0.0f, 1.0f) };
95 case LogLevel::Shutdown: return LogLevelInfo{ "shutdown", 3, ImVec4(0.5f, 0.0f, 0.0f, 1.0f) };
96 case LogLevel::Initialize: return LogLevelInfo{ "initialize", 4, ImVec4(0.0f, 1.0f, 1.0f, 1.0f) };
97 case LogLevel::Update: return LogLevelInfo{ "Update", 5, ImVec4(1.0f, 0.0f, 1.0f, 1.0f) };
98 case LogLevel::Render: return LogLevelInfo{ "render", 6, ImVec4(1.0f, 1.0f, 1.0f, 1.0f) };
99 case LogLevel::Input: return LogLevelInfo{ "Input", 7, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
100 case LogLevel::Physics: return LogLevelInfo{ "physics", 8, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
101 case LogLevel::Audio: return LogLevelInfo{ "Audio", 9, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
102 case LogLevel::Network: return LogLevelInfo{ "Network", 10, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
103 case LogLevel::Scripting: return LogLevelInfo{ "Scripting", 11, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
104 case LogLevel::AI: return LogLevelInfo{ "AI", 12, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
105 case LogLevel::Resource: return LogLevelInfo{ "Resource", 13, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
106 case LogLevel::Memory: return LogLevelInfo{ "Memory", 14, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
107 case LogLevel::Debug: return LogLevelInfo{ "Debug", 15, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
108 default: return LogLevelInfo{ "Unknown", 16, ImVec4(1.0f, 1.0f, 1.0f, 1.0f) };
109 }
110 }

◆ Log() [1/2]

void Logger::Log ( const std::string & message,
const std::string & fileName,
int lineNumber,
LogLevel level = LogLevel::Info )
inline

Write a log message to the log file and console.

Parameters
message
fileName
lineNumber
level

Definition at line 158 of file Logger.h.

159 {
160
161 auto now = std::chrono::system_clock::now();
162 auto in_time_t = std::chrono::system_clock::to_time_t(now);
163
164 std::tm buf;
165 localtime_s(&buf, &in_time_t);
166
167 // Obtenez les millisecondes à partir de maintenant
168 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
169
170 // Utilisez LogLevelToString pour obtenir la chaîne de caractères du niveau de log
171 std::string levelStr = GetLogLevelInfo(level).name;
172
173 std::stringstream ss;
174 ss << "[" << std::put_time(&buf, "%Y-%m-%d") << "] "
175 << "[" << std::put_time(&buf, "%X") << "." << std::setfill('0') << std::setw(3) << ms.count() << "] "
176 << "[" << levelStr << "] "
177 << "[" << fileName << ":" << lineNumber << "] "
178 << message;
179
180 Log(ss.str(), level);
181
182 std::ofstream file(m_logFilePath, std::ios::app);
183 if (file.is_open())
184 {
185 file << ss.str() << std::endl;
186 file.close();
187 }
188 }
static const LogLevelInfo GetLogLevelInfo(LogLevel level)
Definition Logger.h:88
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
Definition Logger.h:158

◆ Log() [2/2]

void Logger::Log ( const std::string & message,
LogLevel level )
inline

Write a log message to the log buffer. This is the fonction wich is used to send log messages to the gui. It's using a buffer to store the log messages with a maximum size. The buffer default size is 100 messages. This limit can be changed by changing the logBufferSize variable. But it is not recommended to change it because it can cause performance issues.

Parameters
messageThe log message to write.
levelThe log level for the message.

Definition at line 200 of file Logger.h.

201 {
202
203 // Si le niveau de log est désactivé, ne faites rien
204 if (m_disabledLogLevels[GetLogLevelInfo(level).value])
205 {
206 return;
207 }
208
209 if (logBuffer.size() >= logBufferSize)
210 {
211 logBuffer.pop_front();
212 }
213 logBuffer.push_back({ message, level });
214 }

◆ ManageLogFiles()

void Logger::ManageLogFiles ( const std::string & directoryPath)
inline

This function manages log files in the specified directory. It checks for log files with the ".log" extension, Then it keeps only the three most recent log files, deleting the oldest ones if there are more than three. It also creates a new log file for the current execution with a timestamp in its name.

Parameters
directoryPath

Definition at line 230 of file Logger.h.

231 {
232 std::vector<std::filesystem::path> logFiles;
233
234 // Parcourez tous les fichiers dans le dossier
235 for (const auto& entry : std::filesystem::directory_iterator(directoryPath))
236 {
237 // Si le fichier est un fichier de log, ajoutez-le à la liste
238 if (entry.path().extension() == ".log")
239 {
240 logFiles.push_back(entry.path());
241 }
242 }
243
244 // Si nous avons plus de trois fichiers de log, supprimez le plus ancien
245 while (logFiles.size() >= 3)
246 {
247 // Triez les fichiers par date de modification, le plus ancien en premier
248 std::sort(logFiles.begin(), logFiles.end(), [](const std::filesystem::path& a, const std::filesystem::path& b)
249 {
250 return std::filesystem::last_write_time(a) < std::filesystem::last_write_time(b);
251 });
252
253 // Supprimez le fichier le plus ancien
254 std::filesystem::remove(logFiles[0]);
255
256 // Supprimez-le de la liste
257 logFiles.erase(logFiles.begin());
258 }
259
260 // Créez un nouveau fichier de log pour cette exécution
261 auto now = std::chrono::system_clock::now();
262 auto in_time_t = std::chrono::system_clock::to_time_t(now);
263 std::tm buf;
264 localtime_s(&buf, &in_time_t);
265
266 std::stringstream ss;
267 ss << "Khaotic_log_" << std::put_time(&buf, "%Y_%m_%d_%Hh%Mm%Ss") << ".log";
268 m_logFileName = ss.str();
269 }

Member Data Documentation

◆ LogLevelCount

int Logger::LogLevelCount = static_cast<int>(LogLevel::Count)
staticconstexpr

Definition at line 59 of file Logger.h.

◆ m_disabledLogLevels

bool Logger::m_disabledLogLevels[LogLevelCount]

Definition at line 271 of file Logger.h.

◆ m_logFilePath

std::string Logger::m_logFilePath

Definition at line 272 of file Logger.h.


The documentation for this class was generated from the following file: