Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
logger.cpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin
3// Distributed under the terms of the zlib License. See LICENSE.md for details.
4
5#include "logger.hpp"
6
7#include "utility.hpp"
8
9#include <filesystem>
10#include <chrono>
11#include <iomanip>
12#include <sstream>
13#include <thread>
14
15namespace rendering_engine
16{
17
18namespace
19{
20std::string LogLevelToString(LogLevel level)
21{
22 switch (level)
23 {
24 case LogLevel::Debug: return "DEBUG";
25 case LogLevel::Info: return "INFO";
26 case LogLevel::Warning: return "WARNING";
27 case LogLevel::Error: return "ERROR";
28 default: return "UNKNOWN";
29 }
30}
31}
32
34{
35 static Logger instance;
36 return instance;
37}
38
39void Logger::Initialize(const std::string& appName)
40{
41 std::lock_guard<std::mutex> lock(mMutex);
42
43 if (mFile.is_open())
44 return;
45
46 auto logFolderPath = Utility::GetLogsFolderPath();
47 if (!std::filesystem::exists(logFolderPath))
48 {
49 std::filesystem::create_directory(logFolderPath);
50 }
51
52 // System clock for human-readable timestamp
53 auto now = std::chrono::system_clock::now();
54 auto nowTimeT = std::chrono::system_clock::to_time_t(now);
55
56 std::tm localTime{};
57#ifdef _WIN32
58 localtime_s(&localTime, &nowTimeT);
59#else
60 localtime_r(&nowTimeT, &localTime);
61#endif
62
63 std::ostringstream filename;
64 filename << logFolderPath.string() << "/"
65 << appName << "_"
66 << std::put_time(&localTime, "%Y-%m-%d_%H-%M-%S")
67 << ".log";
68
69 mFile.open(filename.str(), std::ios::out | std::ios::trunc);
70
71 mCurrentLevel = ReadConfiguredLogLevel();
72
73 if (mFile.is_open())
74 {
75 mFile << "===== Engine Log Started =====" << std::endl;
76 mFile.flush();
77 }
78}
79
81{
82 std::lock_guard<std::mutex> lock(mMutex);
83
84 if (mFile.is_open())
85 {
86 mFile << "===== Engine Log Shutdown =====" << std::endl;
87 mFile.flush();
88 mFile.close();
89 }
90}
91
92LogLevel Logger::ReadConfiguredLogLevel()
93{
94 LogLevel configuredLevel = LogLevel::Info;
95
96 const std::string confLogLevelStr = Utility::ReadConfigFile().logLevel;
97 if (confLogLevelStr == std::string{ "Debug" })
98 {
99 configuredLevel = LogLevel::Debug;
100 }
101 if (confLogLevelStr == std::string{ "Warning" })
102 {
103 configuredLevel = LogLevel::Warning;
104 }
105 if (confLogLevelStr == std::string{ "Error" })
106 {
107 configuredLevel = LogLevel::Error;
108 }
109
110 return configuredLevel;
111}
112
113void Logger::SetLogLevel(LogLevel level)
114{
115 std::lock_guard<std::mutex> lock(mMutex);
116 mCurrentLevel = level;
117}
118
120 const std::string& message,
121 const char* file,
122 int line)
123{
124 if (level < mCurrentLevel)
125 return;
126
127 if (!mFile.is_open())
128 return;
129
130 // Capture time outside lock for minimal contention
131 auto nowSystem = std::chrono::system_clock::now();
132 auto nowTimeT = std::chrono::system_clock::to_time_t(nowSystem);
133 auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
134 nowSystem.time_since_epoch()) % 1000;
135
136 std::tm localTime{};
137#ifdef _WIN32
138 localtime_s(&localTime, &nowTimeT);
139#else
140 localtime_r(&nowTimeT, &localTime);
141#endif
142
143 std::ostringstream timestampStream;
144 timestampStream << std::put_time(&localTime, "%Y-%m-%d %H:%M:%S")
145 << "." << std::setfill('0') << std::setw(3) << nowMs.count();
146
147 const auto threadId = std::this_thread::get_id();
148
149 std::lock_guard<std::mutex> lock(mMutex);
150
151 mFile << "[" << timestampStream.str() << "] "
152 << "[" << LogLevelToString(level) << "] "
153 << "[Thread " << threadId << "] "
154 << message
155 << " (" << file << ":" << line << ")"
156 << std::endl;
157
158 mFile.flush();
159}
160
161} // namespace rendering_engine
Singleton engine-wide logging system.
Definition logger.hpp:76
void Shutdown()
Finalizes logging system and closes the log file.
Definition logger.cpp:80
void Log(LogLevel level, const std::string &message, const char *file, int line)
Writes a formatted log message.
Definition logger.cpp:119
void Initialize(const std::string &appName)
Initializes logging system and creates a new log file.
Definition logger.cpp:39
static Logger & Get()
Returns singleton logger instance.
Definition logger.cpp:33
static std::filesystem::path GetLogsFolderPath()
Returns absolute path to Logs folder.
Definition utility.cpp:278
static AppConfig ReadConfigFile()
Reads application settings from the JSON config file.
Definition utility.cpp:37
Engine-wide logging system for runtime diagnostics and performance tracking.
LogLevel
Defines severity levels for log messages.
Definition logger.hpp:56
@ Warning
Non-critical issues or unexpected states.
@ Info
General informational messages.
@ Error
Critical errors affecting functionality.
@ Debug
Detailed diagnostic information.
std::string logLevel
Logging verbosity level ("Error", "Warning", "Info", "Debug").
Definition utility.hpp:42