9#include "boost/filesystem.hpp"
20std::string LogLevelToString(
LogLevel level)
28 default:
return "UNKNOWN";
41 std::lock_guard<std::mutex> lock(mMutex);
47 if (!boost::filesystem::exists(logFolderPath))
49 boost::filesystem::create_directory(logFolderPath);
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;
60 localtime_s(&localTime, &nowTimeT);
62 localtime_r(&nowTimeT, &localTime);
65 std::ostringstream filename;
66 filename << logFolderPath.string() <<
"/"
68 << std::put_time(&localTime,
"%Y-%m-%d_%H-%M-%S")
71 mFile.open(filename.str(), std::ios::out | std::ios::trunc);
73 mCurrentLevel = ReadConfiguredLogLevel();
77 mFile <<
"===== Engine Log Started =====" << std::endl;
84 std::lock_guard<std::mutex> lock(mMutex);
88 mFile <<
"===== Engine Log Shutdown =====" << std::endl;
94LogLevel Logger::ReadConfiguredLogLevel()
99 if (confLogLevelStr == std::string{
"Debug" })
103 if (confLogLevelStr == std::string{
"Warning" })
107 if (confLogLevelStr == std::string{
"Error" })
112 return configuredLevel;
115void Logger::SetLogLevel(
LogLevel level)
117 std::lock_guard<std::mutex> lock(mMutex);
118 mCurrentLevel = level;
122 const std::string& message,
126 if (level < mCurrentLevel)
129 if (!mFile.is_open())
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;
140 localtime_s(&localTime, &nowTimeT);
142 localtime_r(&nowTimeT, &localTime);
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();
149 const auto threadId = std::this_thread::get_id();
151 std::lock_guard<std::mutex> lock(mMutex);
153 mFile <<
"[" << timestampStream.str() <<
"] "
154 <<
"[" << LogLevelToString(level) <<
"] "
155 <<
"[Thread " << threadId <<
"] "
157 <<
" (" << file <<
":" << line <<
")"
Singleton engine-wide logging system.
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.
void Initialize(const std::string &appName)
Initializes logging system and creates a new log file.
static Logger & Get()
Returns singleton logger instance.
static boost::filesystem::path GetLogsFolderPath()
Returns absolute path to Logs folder.
static AppConfig ReadConfigFile()
Reads application settings from the JSON config file.
Engine-wide logging system for runtime diagnostics and performance tracking.
LogLevel
Defines severity levels for log messages.
@ 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").