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
 
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

Definition at line 26 of file Logger.h.

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 };

Constructor & Destructor Documentation

◆ Logger()

Logger::Logger ( )
inline

Definition at line 87 of file Logger.h.

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 }

Member Function Documentation

◆ Get()

static Logger & Logger::Get ( )
inlinestatic

Definition at line 17 of file Logger.h.

18 {
19 static Logger instance;
20 return instance;
21 }

◆ GetLogBuffer()

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

Definition at line 172 of file Logger.h.

172{ return logBuffer; }

◆ GetLogLevelInfo()

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

Definition at line 63 of file Logger.h.

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 }

◆ Log() [1/2]

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

Definition at line 123 of file Logger.h.

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 }

◆ Log() [2/2]

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

Definition at line 156 of file Logger.h.

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 }

◆ ManageLogFiles()

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

Definition at line 174 of file Logger.h.

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 }

Member Data Documentation

◆ LogLevelCount

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

Definition at line 48 of file Logger.h.

◆ m_disabledLogLevels

bool Logger::m_disabledLogLevels[LogLevelCount]

Definition at line 215 of file Logger.h.

◆ m_logFilePath

std::string Logger::m_logFilePath

Definition at line 216 of file Logger.h.


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