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
|
|
|
|
2018-01-27 20:04:08 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
2020-07-10 16:32:23 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2020-06-21 10:26:21 +00:00
|
|
|
#include "Platform.h"
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2020-09-21 17:17:34 +00:00
|
|
|
#include <fstream>
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <iostream>
|
2019-12-31 05:06:13 +00:00
|
|
|
#include <iomanip>
|
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
|
|
|
|
|
|
|
LogLevel Log::getReportingLevel()
|
|
|
|
{
|
2020-06-26 15:17:35 +00:00
|
|
|
return reportingLevel;
|
2013-01-04 23:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Log::getLogPath()
|
|
|
|
{
|
2020-06-26 15:17:35 +00:00
|
|
|
return Utils::FileSystem::getHomePath() + "/.emulationstation/es_log.txt";
|
2013-01-04 23:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Log::setReportingLevel(LogLevel level)
|
|
|
|
{
|
2020-06-26 15:17:35 +00:00
|
|
|
reportingLevel = level;
|
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()
|
|
|
|
{
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
file.open(Utils::String::stringToWideString(getLogPath()).c_str());
|
|
|
|
#else
|
|
|
|
file.open(getLogPath().c_str());
|
|
|
|
#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;
|
|
|
|
#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
|
|
|
|
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()
|
|
|
|
{
|
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.
|
2020-06-26 15:17:35 +00:00
|
|
|
std::cerr << "ERROR - tried to write to log file before it was open! "
|
|
|
|
"The following won't be logged:\n";
|
|
|
|
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
|
|
|
}
|