Some code cleanup in the logger.

This commit is contained in:
Leon Styhre 2021-11-15 20:47:00 +01:00
parent 634a58fbc3
commit 57d172bb14
2 changed files with 31 additions and 39 deletions

View file

@ -3,21 +3,11 @@
// EmulationStation Desktop Edition // EmulationStation Desktop Edition
// Log.cpp // Log.cpp
// //
// Log handling. // Log output.
// //
#include "Log.h" #include "Log.h"
#include "Platform.h"
#include "utils/StringUtil.h"
#include <fstream>
#include <iomanip>
#include <iostream>
LogLevel Log::reportingLevel = LogInfo;
std::ofstream file;
void Log::init() void Log::init()
{ {
Utils::FileSystem::removeFile(getLogPath() + ".bak"); Utils::FileSystem::removeFile(getLogPath() + ".bak");
@ -31,7 +21,7 @@ void Log::open()
#if defined(_WIN64) #if defined(_WIN64)
file.open(Utils::String::stringToWideString(getLogPath()).c_str()); file.open(Utils::String::stringToWideString(getLogPath()).c_str());
#else #else
file.open(getLogPath().c_str()); sFile.open(getLogPath().c_str());
#endif #endif
} }
@ -46,40 +36,39 @@ std::ostringstream& Log::get(LogLevel level)
#else #else
localtime_r(&t, &tm); localtime_r(&t, &tm);
#endif #endif
os << std::put_time(&tm, "%b %d %T ") << logLevelMap[level] << ":\t"; mOutStringStream << std::put_time(&tm, "%b %d %T ") << mLogLevelMap[level] << ":\t";
messageLevel = level; mMessageLevel = level;
return os; return mOutStringStream;
} }
void Log::flush() void Log::flush()
{ {
// This runs on application exit. // Flush file.
file.flush(); sFile.flush();
} }
void Log::close() void Log::close()
{ {
if (file.is_open()) if (sFile.is_open())
file.close(); sFile.close();
} }
Log::~Log() Log::~Log()
{ {
os << std::endl; mOutStringStream << std::endl;
if (!file.is_open()) { if (!sFile.is_open()) {
// Not open yet, print to stdout. // Not open yet, print to stdout.
std::cerr << "Error: Tried to write to log file before it was open, " std::cerr << "Error: Tried to write to log file before it was open, "
"the following won't be logged:\n"; "the following won't be logged:\n";
std::cerr << os.str(); std::cerr << mOutStringStream.str();
return; return;
} }
file << os.str(); sFile << mOutStringStream.str();
// If it's an error, also print to console. // If it's an error or the --debug flag has been set, then print to the console as well.
// Print all messages if using --debug. if (mMessageLevel == LogError || sReportingLevel >= LogDebug)
if (messageLevel == LogError || reportingLevel >= LogDebug) std::cerr << mOutStringStream.str();
std::cerr << os.str();
} }

View file

@ -3,7 +3,7 @@
// EmulationStation Desktop Edition // EmulationStation Desktop Edition
// Log.h // Log.h
// //
// Log handling. // Log output.
// //
#ifndef ES_CORE_LOG_H #ifndef ES_CORE_LOG_H
@ -11,6 +11,9 @@
#include "utils/FileSystemUtil.h" #include "utils/FileSystemUtil.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map> #include <map>
#include <sstream> #include <sstream>
@ -33,8 +36,8 @@ public:
~Log(); ~Log();
std::ostringstream& get(LogLevel level = LogInfo); std::ostringstream& get(LogLevel level = LogInfo);
static LogLevel getReportingLevel() { return reportingLevel; } static LogLevel getReportingLevel() { return sReportingLevel; }
static void setReportingLevel(LogLevel level) { reportingLevel = level; } static void setReportingLevel(LogLevel level) { sReportingLevel = level; }
static std::string getLogPath() static std::string getLogPath()
{ {
return Utils::FileSystem::getHomePath() + "/.emulationstation/es_log.txt"; return Utils::FileSystem::getHomePath() + "/.emulationstation/es_log.txt";
@ -46,17 +49,17 @@ public:
static void close(); static void close();
protected: protected:
std::ostringstream os; std::ostringstream mOutStringStream;
private: private:
std::map<LogLevel, std::string> logLevelMap{// Log level indicators. std::map<LogLevel, std::string> mLogLevelMap{// Log level indicators.
{LogError, "Error"}, {LogError, "Error"},
{LogWarning, "Warn"}, {LogWarning, "Warn"},
{LogInfo, "Info"}, {LogInfo, "Info"},
{LogDebug, "Debug"}}; {LogDebug, "Debug"}};
inline static std::ofstream sFile;
static LogLevel reportingLevel; inline static LogLevel sReportingLevel = LogInfo;
LogLevel messageLevel; LogLevel mMessageLevel;
}; };
#endif // ES_CORE_LOG_H #endif // ES_CORE_LOG_H