2013-01-04 23:31:51 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <iostream>
|
2017-08-01 10:34:49 +00:00
|
|
|
#include <stdio.h>
|
2013-05-13 19:53:28 +00:00
|
|
|
#include "platform.h"
|
2013-01-04 23:31:51 +00:00
|
|
|
|
|
|
|
LogLevel Log::reportingLevel = LogInfo;
|
2013-01-08 02:24:59 +00:00
|
|
|
FILE* Log::file = NULL; //fopen(getLogPath().c_str(), "w");
|
2013-01-04 23:31:51 +00:00
|
|
|
|
|
|
|
LogLevel Log::getReportingLevel()
|
|
|
|
{
|
|
|
|
return reportingLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Log::getLogPath()
|
|
|
|
{
|
2013-05-13 19:53:28 +00:00
|
|
|
std::string home = getHomePath();
|
2013-01-04 23:31:51 +00:00
|
|
|
return home + "/.emulationstation/es_log.txt";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::setReportingLevel(LogLevel level)
|
|
|
|
{
|
|
|
|
reportingLevel = level;
|
|
|
|
}
|
|
|
|
|
2017-08-01 10:34:49 +00:00
|
|
|
void Log::init()
|
|
|
|
{
|
|
|
|
remove((getLogPath() + ".bak").c_str());
|
|
|
|
// rename previous log file
|
|
|
|
rename(getLogPath().c_str(), (getLogPath() + ".bak").c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-08 02:24:59 +00:00
|
|
|
void Log::open()
|
|
|
|
{
|
|
|
|
file = fopen(getLogPath().c_str(), "w");
|
|
|
|
}
|
|
|
|
|
2013-01-04 23:31:51 +00:00
|
|
|
std::ostringstream& Log::get(LogLevel level)
|
|
|
|
{
|
|
|
|
os << "lvl" << level << ": \t";
|
|
|
|
messageLevel = level;
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::flush()
|
|
|
|
{
|
|
|
|
fflush(getOutput());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::close()
|
|
|
|
{
|
|
|
|
fclose(file);
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* Log::getOutput()
|
|
|
|
{
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::~Log()
|
|
|
|
{
|
|
|
|
os << std::endl;
|
2013-01-08 02:24:59 +00:00
|
|
|
|
|
|
|
if(getOutput() == NULL)
|
|
|
|
{
|
2014-03-22 16:44:57 +00:00
|
|
|
// 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";
|
|
|
|
std::cerr << os.str();
|
2013-01-08 02:24:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-04 23:31:51 +00:00
|
|
|
fprintf(getOutput(), "%s", os.str().c_str());
|
|
|
|
|
|
|
|
//if it's an error, also print to console
|
2013-06-30 01:37:18 +00:00
|
|
|
//print all messages if using --debug
|
|
|
|
if(messageLevel == LogError || reportingLevel >= LogDebug)
|
2013-01-04 23:31:51 +00:00
|
|
|
fprintf(stderr, "%s", os.str().c_str());
|
|
|
|
}
|