Save log to file, closes #696
parent
6dc7d892b7
commit
254891d8f1
|
@ -92,11 +92,19 @@ extern "C"
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
CLogger logger; // single instance of logger
|
CLogger logger; // single instance of logger
|
||||||
|
logger.AddOutput(stderr);
|
||||||
|
|
||||||
auto systemUtils = CSystemUtils::Create(); // platform-specific utils
|
auto systemUtils = CSystemUtils::Create(); // platform-specific utils
|
||||||
systemUtils->Init();
|
systemUtils->Init();
|
||||||
|
|
||||||
//logger.SetOutputFile(systemUtils->GetSaveDir() + "/log.txt");
|
// Add file output to the logger
|
||||||
|
std::string logfile;
|
||||||
|
#if DEV_BUILD
|
||||||
|
logfile = "log.txt";
|
||||||
|
#else
|
||||||
|
logfile = systemUtils->GetSaveDir() + "/log.txt";
|
||||||
|
#endif
|
||||||
|
logger.AddOutput(fopen(logfile.c_str(), "w"));
|
||||||
|
|
||||||
// Workaround for character encoding in argv on Windows
|
// Workaround for character encoding in argv on Windows
|
||||||
#if PLATFORM_WINDOWS
|
#if PLATFORM_WINDOWS
|
||||||
|
|
|
@ -28,7 +28,6 @@ template<> CLogger* CSingleton<CLogger>::m_instance = nullptr;
|
||||||
|
|
||||||
CLogger::CLogger()
|
CLogger::CLogger()
|
||||||
{
|
{
|
||||||
m_file = nullptr;
|
|
||||||
#if DEV_BUILD
|
#if DEV_BUILD
|
||||||
m_logLevel = LOG_DEBUG;
|
m_logLevel = LOG_DEBUG;
|
||||||
#else
|
#else
|
||||||
|
@ -38,7 +37,10 @@ CLogger::CLogger()
|
||||||
|
|
||||||
CLogger::~CLogger()
|
CLogger::~CLogger()
|
||||||
{
|
{
|
||||||
Close();
|
for (FILE* out : m_outputs)
|
||||||
|
{
|
||||||
|
fclose(out);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLogger::Log(LogLevel type, const char* str, va_list args)
|
void CLogger::Log(LogLevel type, const char* str, va_list args)
|
||||||
|
@ -46,28 +48,34 @@ void CLogger::Log(LogLevel type, const char* str, va_list args)
|
||||||
if (type < m_logLevel)
|
if (type < m_logLevel)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
for (FILE* out : m_outputs)
|
||||||
|
{
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case LOG_TRACE:
|
case LOG_TRACE:
|
||||||
fprintf(IsOpened() ? m_file : stderr, "[TRACE]: ");
|
fprintf(out, "[TRACE]: ");
|
||||||
break;
|
break;
|
||||||
case LOG_DEBUG:
|
case LOG_DEBUG:
|
||||||
fprintf(IsOpened() ? m_file : stderr, "[DEBUG]: ");
|
fprintf(out, "[DEBUG]: ");
|
||||||
break;
|
break;
|
||||||
case LOG_WARN:
|
case LOG_WARN:
|
||||||
fprintf(IsOpened() ? m_file : stderr, "[WARN]: ");
|
fprintf(out, "[WARN]: ");
|
||||||
break;
|
break;
|
||||||
case LOG_INFO:
|
case LOG_INFO:
|
||||||
fprintf(IsOpened() ? m_file : stderr, "[INFO]: ");
|
fprintf(out, "[INFO]: ");
|
||||||
break;
|
break;
|
||||||
case LOG_ERROR:
|
case LOG_ERROR:
|
||||||
fprintf(IsOpened() ? m_file : stderr, "[ERROR]: ");
|
fprintf(out, "[ERROR]: ");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
vfprintf(IsOpened() ? m_file : stderr, str, args);
|
va_list args2;
|
||||||
|
va_copy(args2, args);
|
||||||
|
vfprintf(out, str, args2);
|
||||||
|
va_end(args2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLogger::Trace(const char* str, ...)
|
void CLogger::Trace(const char* str, ...)
|
||||||
|
@ -126,29 +134,10 @@ void CLogger::Log(LogLevel logLevel, const char* str, ...)
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLogger::SetOutputFile(std::string filename)
|
void CLogger::AddOutput(FILE* file)
|
||||||
{
|
{
|
||||||
m_filename = filename;
|
assert(file != nullptr);
|
||||||
Open();
|
m_outputs.push_back(file);
|
||||||
}
|
|
||||||
|
|
||||||
void CLogger::Open()
|
|
||||||
{
|
|
||||||
m_file = fopen(m_filename.c_str(), "w");
|
|
||||||
|
|
||||||
if (m_file == nullptr)
|
|
||||||
fprintf(stderr, "Could not create file %s\n", m_filename.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void CLogger::Close()
|
|
||||||
{
|
|
||||||
if (IsOpened())
|
|
||||||
fclose(m_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CLogger::IsOpened()
|
|
||||||
{
|
|
||||||
return m_file != nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLogger::SetLogLevel(LogLevel level)
|
void CLogger::SetLogLevel(LogLevel level)
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -104,9 +105,10 @@ public:
|
||||||
void Log(LogLevel logLevel, const char *str, ...);
|
void Log(LogLevel logLevel, const char *str, ...);
|
||||||
|
|
||||||
/** Set output file to write logs to
|
/** Set output file to write logs to
|
||||||
* \param filename - output file to write to
|
* The given file will be automatically closed when the logger exits
|
||||||
|
* \param file - file pointer to write to
|
||||||
*/
|
*/
|
||||||
void SetOutputFile(std::string filename);
|
void AddOutput(FILE* file);
|
||||||
|
|
||||||
/** Set log level. Logs with level below will not be shown
|
/** Set log level. Logs with level below will not be shown
|
||||||
* \param level - minimum log level to write
|
* \param level - minimum log level to write
|
||||||
|
@ -123,13 +125,8 @@ public:
|
||||||
static bool ParseLogLevel(const std::string& str, LogLevel& logLevel);
|
static bool ParseLogLevel(const std::string& str, LogLevel& logLevel);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_filename;
|
std::vector<FILE*> m_outputs;
|
||||||
FILE *m_file;
|
|
||||||
LogLevel m_logLevel;
|
LogLevel m_logLevel;
|
||||||
|
|
||||||
void Open();
|
|
||||||
void Close();
|
|
||||||
bool IsOpened();
|
|
||||||
void Log(LogLevel type, const char* str, va_list args);
|
void Log(LogLevel type, const char* str, va_list args);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue