Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
rendering_engine::Logger Class Reference

Singleton engine-wide logging system. More...

#include <logger.hpp>

Public Member Functions

void Initialize (const std::string &appName)
 Initializes logging system and creates a new log file.
 
void Shutdown ()
 Finalizes logging system and closes the log file.
 
void Log (LogLevel level, const std::string &message, const char *file, int line)
 Writes a formatted log message.
 

Static Public Member Functions

static Logger & Get ()
 Returns singleton logger instance.
 

Detailed Description

Singleton engine-wide logging system.

Responsibilities:

  • Create log file per application run
  • Write formatted log entries
  • Apply runtime log-level filtering
  • Ensure thread-safe file access

The logger must be initialized during application startup and shutdown during application termination.

Definition at line 75 of file logger.hpp.

Member Function Documentation

◆ Get()

Logger & rendering_engine::Logger::Get ( )
static

Returns singleton logger instance.

Definition at line 33 of file logger.cpp.

34{
35 static Logger instance;
36 return instance;
37}

◆ Initialize()

void rendering_engine::Logger::Initialize ( const std::string &  appName)

Initializes logging system and creates a new log file.

Parameters
appNameName of the application (used in log filename).

Definition at line 39 of file logger.cpp.

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}
static std::filesystem::path GetLogsFolderPath()
Returns absolute path to Logs folder.
Definition utility.cpp:278

◆ Log()

void rendering_engine::Logger::Log ( LogLevel  level,
const std::string &  message,
const char *  file,
int  line 
)

Writes a formatted log message.

Parameters
levelLog severity level.
messageLog message string.
fileSource file name.
lineSource line number.

Definition at line 119 of file logger.cpp.

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}

◆ Shutdown()

void rendering_engine::Logger::Shutdown ( )

Finalizes logging system and closes the log file.

Definition at line 80 of file logger.cpp.

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}

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