diff --git a/es-core/src/Log.cpp b/es-core/src/Log.cpp index 2530e5067..c9659109e 100644 --- a/es-core/src/Log.cpp +++ b/es-core/src/Log.cpp @@ -3,21 +3,11 @@ // EmulationStation Desktop Edition // Log.cpp // -// Log handling. +// Log output. // #include "Log.h" -#include "Platform.h" -#include "utils/StringUtil.h" - -#include -#include -#include - -LogLevel Log::reportingLevel = LogInfo; -std::ofstream file; - void Log::init() { Utils::FileSystem::removeFile(getLogPath() + ".bak"); @@ -31,7 +21,7 @@ void Log::open() #if defined(_WIN64) file.open(Utils::String::stringToWideString(getLogPath()).c_str()); #else - file.open(getLogPath().c_str()); + sFile.open(getLogPath().c_str()); #endif } @@ -46,40 +36,39 @@ std::ostringstream& Log::get(LogLevel level) #else localtime_r(&t, &tm); #endif - os << std::put_time(&tm, "%b %d %T ") << logLevelMap[level] << ":\t"; - messageLevel = level; + mOutStringStream << std::put_time(&tm, "%b %d %T ") << mLogLevelMap[level] << ":\t"; + mMessageLevel = level; - return os; + return mOutStringStream; } void Log::flush() { - // This runs on application exit. - file.flush(); + // Flush file. + sFile.flush(); } void Log::close() { - if (file.is_open()) - file.close(); + if (sFile.is_open()) + sFile.close(); } Log::~Log() { - os << std::endl; + mOutStringStream << std::endl; - if (!file.is_open()) { + if (!sFile.is_open()) { // Not open yet, print to stdout. std::cerr << "Error: Tried to write to log file before it was open, " "the following won't be logged:\n"; - std::cerr << os.str(); + std::cerr << mOutStringStream.str(); return; } - file << os.str(); + sFile << mOutStringStream.str(); - // If it's an error, also print to console. - // Print all messages if using --debug. - if (messageLevel == LogError || reportingLevel >= LogDebug) - std::cerr << os.str(); + // If it's an error or the --debug flag has been set, then print to the console as well. + if (mMessageLevel == LogError || sReportingLevel >= LogDebug) + std::cerr << mOutStringStream.str(); } diff --git a/es-core/src/Log.h b/es-core/src/Log.h index 9d98951a0..b6180333a 100644 --- a/es-core/src/Log.h +++ b/es-core/src/Log.h @@ -3,7 +3,7 @@ // EmulationStation Desktop Edition // Log.h // -// Log handling. +// Log output. // #ifndef ES_CORE_LOG_H @@ -11,6 +11,9 @@ #include "utils/FileSystemUtil.h" +#include +#include +#include #include #include @@ -33,8 +36,8 @@ public: ~Log(); std::ostringstream& get(LogLevel level = LogInfo); - static LogLevel getReportingLevel() { return reportingLevel; } - static void setReportingLevel(LogLevel level) { reportingLevel = level; } + static LogLevel getReportingLevel() { return sReportingLevel; } + static void setReportingLevel(LogLevel level) { sReportingLevel = level; } static std::string getLogPath() { return Utils::FileSystem::getHomePath() + "/.emulationstation/es_log.txt"; @@ -46,17 +49,17 @@ public: static void close(); protected: - std::ostringstream os; + std::ostringstream mOutStringStream; private: - std::map logLevelMap{// Log level indicators. - {LogError, "Error"}, - {LogWarning, "Warn"}, - {LogInfo, "Info"}, - {LogDebug, "Debug"}}; - - static LogLevel reportingLevel; - LogLevel messageLevel; + std::map mLogLevelMap{// Log level indicators. + {LogError, "Error"}, + {LogWarning, "Warn"}, + {LogInfo, "Info"}, + {LogDebug, "Debug"}}; + inline static std::ofstream sFile; + inline static LogLevel sReportingLevel = LogInfo; + LogLevel mMessageLevel; }; #endif // ES_CORE_LOG_H