Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
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. More...
 
void Shutdown ()
 Finalizes logging system and closes the log file. More...
 
void Log (LogLevel level, const std::string &message, const char *file, int line)
 Writes a formatted log message. More...
 

Static Public Member Functions

static LoggerGet ()
 Returns singleton logger instance. More...
 

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 (!boost::filesystem::exists(logFolderPath))
48 {
49 boost::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 auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
56 now.time_since_epoch()) % 1000;
57
58 std::tm localTime{};
59#ifdef _WIN32
60 localtime_s(&localTime, &nowTimeT);
61#else
62 localtime_r(&nowTimeT, &localTime);
63#endif
64
65 std::ostringstream filename;
66 filename << logFolderPath.string() << "/"
67 << appName << "_"
68 << std::put_time(&localTime, "%Y-%m-%d_%H-%M-%S")
69 << ".log";
70
71 mFile.open(filename.str(), std::ios::out | std::ios::trunc);
72
73 mCurrentLevel = ReadConfiguredLogLevel();
74
75 if (mFile.is_open())
76 {
77 mFile << "===== Engine Log Started =====" << std::endl;
78 mFile.flush();
79 }
80}
static boost::filesystem::path GetLogsFolderPath()
Returns absolute path to Logs folder.
Definition: utility.cpp:269

◆ 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 121 of file logger.cpp.

125{
126 if (level < mCurrentLevel)
127 return;
128
129 if (!mFile.is_open())
130 return;
131
132 // Capture time outside lock for minimal contention
133 auto nowSystem = std::chrono::system_clock::now();
134 auto nowTimeT = std::chrono::system_clock::to_time_t(nowSystem);
135 auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
136 nowSystem.time_since_epoch()) % 1000;
137
138 std::tm localTime{};
139#ifdef _WIN32
140 localtime_s(&localTime, &nowTimeT);
141#else
142 localtime_r(&nowTimeT, &localTime);
143#endif
144
145 std::ostringstream timestampStream;
146 timestampStream << std::put_time(&localTime, "%Y-%m-%d %H:%M:%S")
147 << "." << std::setfill('0') << std::setw(3) << nowMs.count();
148
149 const auto threadId = std::this_thread::get_id();
150
151 std::lock_guard<std::mutex> lock(mMutex);
152
153 mFile << "[" << timestampStream.str() << "] "
154 << "[" << LogLevelToString(level) << "] "
155 << "[Thread " << threadId << "] "
156 << message
157 << " (" << file << ":" << line << ")"
158 << std::endl;
159
160 mFile.flush();
161}

◆ Shutdown()

void rendering_engine::Logger::Shutdown ( )

Finalizes logging system and closes the log file.

Definition at line 82 of file logger.cpp.

83{
84 std::lock_guard<std::mutex> lock(mMutex);
85
86 if (mFile.is_open())
87 {
88 mFile << "===== Engine Log Shutdown =====" << std::endl;
89 mFile.flush();
90 mFile.close();
91 }
92}

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