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

122 lines
3.5 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: MIT
//
// ES-DE
// Log.cpp
//
2021-11-15 19:47:00 +00:00
// Log output.
// This class is thread safe.
//
#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()
{
std::unique_lock<std::mutex> lock {sLogMutex};
2022-06-22 05:06:20 +00:00
return sReportingLevel;
}
void Log::setReportingLevel(LogLevel level)
{
std::unique_lock<std::mutex> lock {sLogMutex};
2022-06-22 05:06:20 +00:00
sReportingLevel = level;
}
void Log::init()
{
sLogPath = Utils::FileSystem::getAppDataDirectory().append("es_log.txt");
Utils::FileSystem::removeFile(sLogPath.string() + ".bak");
// Rename the previous log file.
Utils::FileSystem::renameFile(sLogPath.string(), sLogPath.string() + ".bak", true);
return;
}
void Log::open()
{
std::unique_lock<std::mutex> lock {sLogMutex};
#if defined(_WIN64)
sFile.open(Utils::String::stringToWideString(sLogPath.string()).c_str());
#else
sFile.open(sLogPath.string().c_str());
#endif
}
void Log::flush()
{
std::unique_lock<std::mutex> lock {sLogMutex};
sFile.flush();
}
void Log::close()
{
std::unique_lock<std::mutex> lock {sLogMutex};
if (sFile.is_open())
sFile.close();
}
std::ostringstream& Log::get(LogLevel level)
{
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
std::unique_lock<std::mutex> lock {sLogMutex};
mOutStringStream << std::put_time(&tm, "%b %d %H:%M:%S ") << mLogLevelMap[level]
<< (level == LogLevel::LogInfo || level == LogLevel::LogWarning ? ": " :
": ");
2021-11-15 19:47:00 +00:00
mMessageLevel = level;
2021-11-15 19:47:00 +00:00
return mOutStringStream;
}
Log::~Log()
{
std::unique_lock<std::mutex> lock {sLogMutex};
2023-01-28 12:36:22 +00:00
mOutStringStream << std::endl;
2021-11-15 19:47:00 +00:00
if (!sFile.is_open()) {
// Not open yet, print to stdout.
#if defined(__ANDROID__)
__android_log_print(
ANDROID_LOG_ERROR, ANDROID_APPLICATION_ID,
"Error: Tried to write to log file before it was open, the following won't be logged:");
__android_log_print(ANDROID_LOG_ERROR, ANDROID_APPLICATION_ID, "%s",
mOutStringStream.str().c_str());
#else
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();
#endif
return;
}
2021-11-15 19:47:00 +00:00
sFile << mOutStringStream.str();
2023-11-20 21:52:33 +00:00
#if defined(__ANDROID__)
if (mMessageLevel == LogError) {
__android_log_print(ANDROID_LOG_ERROR, ANDROID_APPLICATION_ID, "%s",
mOutStringStream.str().c_str());
2023-11-20 21:52:33 +00:00
}
else if (sReportingLevel >= LogDebug) {
if (mMessageLevel == LogInfo)
__android_log_print(ANDROID_LOG_INFO, ANDROID_APPLICATION_ID, "%s",
mOutStringStream.str().c_str());
2023-11-20 21:52:33 +00:00
else if (mMessageLevel == LogWarning)
__android_log_print(ANDROID_LOG_WARN, ANDROID_APPLICATION_ID, "%s",
mOutStringStream.str().c_str());
2023-11-20 21:52:33 +00:00
else
__android_log_print(ANDROID_LOG_DEBUG, ANDROID_APPLICATION_ID, "%s",
mOutStringStream.str().c_str());
2023-11-20 21:52:33 +00:00
}
#else
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();
2023-11-20 21:52:33 +00:00
#endif
}