mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2024-11-25 15:45:38 +00:00
Some code cleanup in the logger.
This commit is contained in:
parent
634a58fbc3
commit
57d172bb14
|
@ -3,21 +3,11 @@
|
|||
// EmulationStation Desktop Edition
|
||||
// Log.cpp
|
||||
//
|
||||
// Log handling.
|
||||
// Log output.
|
||||
//
|
||||
|
||||
#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()
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -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 <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
|
@ -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<LogLevel, std::string> logLevelMap{// Log level indicators.
|
||||
{LogError, "Error"},
|
||||
{LogWarning, "Warn"},
|
||||
{LogInfo, "Info"},
|
||||
{LogDebug, "Debug"}};
|
||||
|
||||
static LogLevel reportingLevel;
|
||||
LogLevel messageLevel;
|
||||
std::map<LogLevel, std::string> 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
|
||||
|
|
Loading…
Reference in a new issue