mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2024-11-23 22:55:39 +00:00
76 lines
1.6 KiB
C++
76 lines
1.6 KiB
C++
// SPDX-License-Identifier: MIT
|
|
//
|
|
// EmulationStation Desktop Edition
|
|
// Log.cpp
|
|
//
|
|
// Log output.
|
|
//
|
|
|
|
#include "Log.h"
|
|
#include "utils/StringUtil.h"
|
|
|
|
void Log::init()
|
|
{
|
|
Utils::FileSystem::removeFile(getLogPath() + ".bak");
|
|
// Rename the previous log file.
|
|
Utils::FileSystem::renameFile(getLogPath(), getLogPath() + ".bak", true);
|
|
return;
|
|
}
|
|
|
|
void Log::open()
|
|
{
|
|
#if defined(_WIN64)
|
|
sFile.open(Utils::String::stringToWideString(getLogPath()).c_str());
|
|
#else
|
|
sFile.open(getLogPath().c_str());
|
|
#endif
|
|
}
|
|
|
|
std::ostringstream& Log::get(LogLevel level)
|
|
{
|
|
time_t t = time(nullptr);
|
|
struct tm tm;
|
|
#if defined(_WIN64)
|
|
// Of course Windows does not follow standards and puts the parameters the other way
|
|
// around compared to POSIX.
|
|
localtime_s(&tm, &t);
|
|
#else
|
|
localtime_r(&t, &tm);
|
|
#endif
|
|
mOutStringStream << std::put_time(&tm, "%b %d %T ") << mLogLevelMap[level] << ":\t";
|
|
mMessageLevel = level;
|
|
|
|
return mOutStringStream;
|
|
}
|
|
|
|
void Log::flush()
|
|
{
|
|
// Flush file.
|
|
sFile.flush();
|
|
}
|
|
|
|
void Log::close()
|
|
{
|
|
if (sFile.is_open())
|
|
sFile.close();
|
|
}
|
|
|
|
Log::~Log()
|
|
{
|
|
mOutStringStream << std::endl;
|
|
|
|
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 << mOutStringStream.str();
|
|
return;
|
|
}
|
|
|
|
sFile << mOutStringStream.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();
|
|
}
|