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
|
|
|
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <iostream>
|
2019-12-31 05:06:13 +00:00
|
|
|
#include <iomanip>
|
2020-07-10 16:32:23 +00:00
|
|
|
#include <fstream>
|
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-07-10 16:32:23 +00:00
|
|
|
#ifdef _WIN64
|
|
|
|
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-07-26 21:30:45 +00:00
|
|
|
os << std::put_time(localtime(&t), "%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()
|
|
|
|
{
|
2020-07-10 16:32:23 +00:00
|
|
|
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
|
|
|
}
|