ES-DE/es-core/src/Log.cpp

124 lines
3.3 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: MIT
//
// EmulationStation Desktop Edition
// Log.cpp
//
2021-11-15 19:47:00 +00:00
// Log output.
//
#include "Log.h"
2021-11-15 22:45:17 +00:00
#include "utils/StringUtil.h"
2017-11-01 22:21:10 +00:00
2022-06-22 05:06:20 +00:00
LogLevel Log::getReportingLevel()
{
// Static Log functions need to grab the lock.
std::unique_lock<std::recursive_mutex> lock {sLogMutex};
return sReportingLevel;
}
void Log::setReportingLevel(LogLevel level)
{
// Static Log functions need to grab the lock.
std::unique_lock<std::recursive_mutex> lock {sLogMutex};
sReportingLevel = level;
}
std::string Log::getLogPath()
{
// No attempt is made to make this thread-safe.
// Currently getLogPath is public, and called in contexts with
// and without sLogMutex locked.
// getHomePath() currently does not generate any Log messages.
return Utils::FileSystem::getHomePath() + "/.emulationstation/es_log.txt";
}
void Log::init()
{
2022-06-22 05:06:20 +00:00
// No attempt is made to make this thread-safe.
// It is unlikely to be called across multiple threads.
// Both removeFile and renameFile might generate log messages,
// so they might try to grab the lock.
Utils::FileSystem::removeFile(getLogPath() + ".bak");
// Rename the previous log file.
Utils::FileSystem::renameFile(getLogPath(), getLogPath() + ".bak", true);
return;
}
void Log::open()
{
2022-06-22 05:06:20 +00:00
// Static Log functions need to grab the lock.
std::unique_lock<std::recursive_mutex> lock {sLogMutex};
#if defined(_WIN64)
2021-11-15 22:45:17 +00:00
sFile.open(Utils::String::stringToWideString(getLogPath()).c_str());
#else
2021-11-15 19:47:00 +00:00
sFile.open(getLogPath().c_str());
#endif
}
std::ostringstream& Log::get(LogLevel level)
{
2022-06-22 05:06:20 +00:00
// This function is not-static, lock is guarded by the Log() instance.
2022-01-16 17:18:28 +00:00
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
2021-11-15 19:47:00 +00:00
mOutStringStream << std::put_time(&tm, "%b %d %T ") << mLogLevelMap[level] << ":\t";
mMessageLevel = level;
2021-11-15 19:47:00 +00:00
return mOutStringStream;
}
void Log::flush()
{
2022-06-22 05:06:20 +00:00
// Flush file. Static Log functions need to grab the lock.
std::unique_lock<std::recursive_mutex> lock {sLogMutex};
2021-11-15 19:47:00 +00:00
sFile.flush();
}
void Log::close()
{
2022-06-22 05:06:20 +00:00
// Static Log functions need to grab the lock.
std::unique_lock<std::recursive_mutex> lock {sLogMutex};
2021-11-15 19:47:00 +00:00
if (sFile.is_open())
sFile.close();
}
2022-06-22 05:06:20 +00:00
Log::Log()
{
// Log instance created. We grab the lock until destruction.
// This permits `Log().get(...) << msg << msg << msg;` to
// function as expected.
sLogMutex.lock();
}
Log::~Log()
{
2022-06-22 05:06:20 +00:00
// sLogMutex was (and still is) locked from the constructor Log().
2021-11-15 19:47:00 +00:00
mOutStringStream << std::endl;
2021-11-15 19:47:00 +00:00
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";
2021-11-15 19:47:00 +00:00
std::cerr << mOutStringStream.str();
return;
}
2021-11-15 19:47:00 +00:00
sFile << mOutStringStream.str();
2021-11-15 19:47:00 +00:00
// 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();
2022-06-22 05:06:20 +00:00
// Release the lock, after any and all operations have been performed
// on mOutStringStream or sFile.
sLogMutex.unlock();
}