2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-26 15:17:35 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-26 15:17:35 +00:00
|
|
|
// Log.cpp
|
|
|
|
//
|
|
|
|
// Log handling.
|
|
|
|
//
|
|
|
|
|
2013-01-04 23:31:51 +00:00
|
|
|
#include "Log.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2020-06-21 10:26:21 +00:00
|
|
|
#include "Platform.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2020-09-21 17:17:34 +00:00
|
|
|
#include <fstream>
|
2019-12-31 05:06:13 +00:00
|
|
|
#include <iomanip>
|
2021-07-07 18:31:46 +00:00
|
|
|
#include <iostream>
|
2013-01-04 23:31:51 +00:00
|
|
|
|
|
|
|
LogLevel Log::reportingLevel = LogInfo;
|
2020-07-10 16:32:23 +00:00
|
|
|
std::ofstream file;
|
2013-01-04 23:31:51 +00:00
|
|
|
|
2017-08-01 10:34:49 +00:00
|
|
|
void Log::init()
|
|
|
|
{
|
2020-07-10 16:32:23 +00:00
|
|
|
Utils::FileSystem::removeFile(getLogPath() + ".bak");
|
|
|
|
// Rename the previous log file.
|
|
|
|
Utils::FileSystem::renameFile(getLogPath(), getLogPath() + ".bak", true);
|
2020-06-26 15:17:35 +00:00
|
|
|
return;
|
2017-08-01 10:34:49 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 02:24:59 +00:00
|
|
|
void Log::open()
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
file.open(Utils::String::stringToWideString(getLogPath()).c_str());
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2020-07-10 16:32:23 +00:00
|
|
|
file.open(getLogPath().c_str());
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2013-01-08 02:24:59 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 23:31:51 +00:00
|
|
|
std::ostringstream& Log::get(LogLevel level)
|
|
|
|
{
|
2020-06-26 15:17:35 +00:00
|
|
|
time_t t = time(nullptr);
|
2020-12-29 12:44:13 +00:00
|
|
|
struct tm tm;
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(_WIN64)
|
2020-12-29 12:44:13 +00:00
|
|
|
// Of course Windows does not follow standards and puts the parameters the other way
|
|
|
|
// around compared to POSIX.
|
|
|
|
localtime_s(&tm, &t);
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2020-12-29 12:44:13 +00:00
|
|
|
localtime_r(&t, &tm);
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-12-29 12:44:13 +00:00
|
|
|
os << std::put_time(&tm, "%b %d %T ") << logLevelMap[level] << ":\t";
|
2020-06-26 15:17:35 +00:00
|
|
|
messageLevel = level;
|
2013-01-04 23:31:51 +00:00
|
|
|
|
2020-06-26 15:17:35 +00:00
|
|
|
return os;
|
2013-01-04 23:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Log::flush()
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// This runs on application exit.
|
2020-07-10 16:32:23 +00:00
|
|
|
file.flush();
|
2013-01-04 23:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Log::close()
|
|
|
|
{
|
2021-01-01 09:28:42 +00:00
|
|
|
if (file.is_open())
|
|
|
|
file.close();
|
2013-01-04 23:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Log::~Log()
|
|
|
|
{
|
2020-06-26 15:17:35 +00:00
|
|
|
os << std::endl;
|
|
|
|
|
2020-07-10 16:32:23 +00:00
|
|
|
if (!file.is_open()) {
|
|
|
|
// Not open yet, print to stdout.
|
2021-07-07 18:31:46 +00:00
|
|
|
std::cerr << "Error: Tried to write to log file before it was open, "
|
|
|
|
"the following won't be logged:\n";
|
2020-06-26 15:17:35 +00:00
|
|
|
std::cerr << os.str();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-10 16:32:23 +00:00
|
|
|
file << os.str();
|
2020-06-26 15:17:35 +00:00
|
|
|
|
|
|
|
// If it's an error, also print to console.
|
|
|
|
// Print all messages if using --debug.
|
|
|
|
if (messageLevel == LogError || reportingLevel >= LogDebug)
|
2020-07-10 16:32:23 +00:00
|
|
|
std::cerr << os.str();
|
2013-01-04 23:31:51 +00:00
|
|
|
}
|