1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 17:19:56 +00:00
openmw-tes3mp/components/openmw-mp/TimedLog.cpp

119 lines
2.4 KiB
C++
Raw Normal View History

2016-08-14 23:45:42 +00:00
#include <cstdarg>
#include <iostream>
#include <cstring>
2016-08-15 13:35:29 +00:00
#include <ctime>
2016-08-16 21:52:40 +00:00
#include <cstdio>
#include <sstream>
2016-08-18 00:44:47 +00:00
#include <vector>
2016-12-18 04:43:30 +00:00
#include <boost/lexical_cast.hpp>
#include "TimedLog.hpp"
2016-08-14 23:45:42 +00:00
using namespace std;
TimedLog *TimedLog::sTimedLog = nullptr;
2016-08-14 23:45:42 +00:00
TimedLog::TimedLog(int logLevel) : logLevel(logLevel)
2016-08-14 23:45:42 +00:00
{
}
void TimedLog::Create(int logLevel)
2016-08-14 23:45:42 +00:00
{
if (sTimedLog != nullptr)
2016-08-14 23:45:42 +00:00
return;
sTimedLog = new TimedLog(logLevel);
2016-08-14 23:45:42 +00:00
}
void TimedLog::Delete()
2016-08-14 23:45:42 +00:00
{
if (sTimedLog == nullptr)
2017-02-24 05:41:11 +00:00
return;
delete sTimedLog;
sTimedLog = nullptr;
2016-08-14 23:45:42 +00:00
}
const TimedLog &TimedLog::Get()
2016-08-14 23:45:42 +00:00
{
return *sTimedLog;
2016-08-14 23:45:42 +00:00
}
int TimedLog::GetLevel()
{
return sTimedLog->logLevel;
}
void TimedLog::SetLevel(int level)
{
sTimedLog->logLevel = level;
}
2016-08-14 23:45:42 +00:00
const char* getTime()
{
time_t t = time(0);
struct tm *tm = localtime(&t);
static char result[20];
sprintf(result, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
2016-08-18 15:38:24 +00:00
1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
2016-08-14 23:45:42 +00:00
tm->tm_hour, tm->tm_min, tm->tm_sec);
return result;
}
void TimedLog::print(int level, bool hasPrefix, const char *file, int line, const char *message, ...) const
2016-08-14 23:45:42 +00:00
{
2016-08-17 15:20:36 +00:00
if (level < logLevel) return;
2016-08-16 21:52:40 +00:00
std::stringstream sstr;
2016-08-14 23:45:42 +00:00
if (hasPrefix)
2016-08-16 21:52:40 +00:00
{
2016-08-14 23:45:42 +00:00
sstr << "[" << getTime() << "] ";
if (file != 0 && line != 0)
{
sstr << "[" << file << ":";
sstr << line << "] ";
}
sstr << "[";
switch (level)
{
2016-08-15 13:33:18 +00:00
case LOG_WARN:
2016-08-16 21:52:40 +00:00
sstr << "WARN";
2016-08-14 23:45:42 +00:00
break;
2016-08-15 13:33:18 +00:00
case LOG_ERROR:
2016-08-16 21:52:40 +00:00
sstr << "ERR";
2016-08-14 23:45:42 +00:00
break;
2016-08-15 13:33:18 +00:00
case LOG_FATAL:
2016-08-16 21:52:40 +00:00
sstr << "FATAL";
2016-08-14 23:45:42 +00:00
break;
default:
2016-08-16 21:52:40 +00:00
sstr << "INFO";
}
sstr << "]: ";
2016-08-14 23:45:42 +00:00
}
2016-08-16 21:52:40 +00:00
sstr << message;
char back = *sstr.str().rbegin();
2016-08-17 15:20:36 +00:00
if (back != '\n')
2016-08-16 21:52:40 +00:00
sstr << '\n';
2016-08-14 23:45:42 +00:00
va_list args;
va_start(args, message);
2017-06-27 14:49:28 +00:00
vector<char> buf((unsigned long) (vsnprintf(nullptr, 0, sstr.str().c_str(), args) + 1));
2016-08-14 23:45:42 +00:00
va_end(args);
2016-08-18 00:44:47 +00:00
va_start(args, message);
vsnprintf(buf.data(), buf.size(), sstr.str().c_str(), args);
va_end(args);
cout << buf.data() << flush;
2016-08-14 23:45:42 +00:00
}
string TimedLog::getFilenameTimestamp()
{
time_t rawtime = time(0);
struct tm *timeinfo = localtime(&rawtime);
char buffer[25];
strftime(buffer, 25, "%Y-%m-%d-%H_%M_%S", timeinfo);
std::string timestamp(buffer);
return timestamp;
}