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

118 lines
2.2 KiB
C++
Raw Normal View History

2016-08-14 23:45:42 +00:00
//
// Created by koncord on 15.08.16.
//
#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>
2016-08-14 23:45:42 +00:00
#include "Log.hpp"
using namespace std;
2016-08-16 21:52:40 +00:00
Log *Log::sLog = NULL;
2016-08-14 23:45:42 +00:00
Log::Log(int logLevel) : logLevel(logLevel)
{
}
void Log::Create(int logLevel)
{
2016-08-17 15:20:36 +00:00
if (sLog != NULL)
2016-08-14 23:45:42 +00:00
return;
sLog = new Log(logLevel);
}
void Log::Delete()
{
2016-08-17 15:20:36 +00:00
if (sLog == NULL)
2016-08-14 23:45:42 +00:00
return
delete sLog;
2016-08-16 21:52:40 +00:00
sLog = NULL;
2016-08-14 23:45:42 +00:00
}
const Log &Log::Get()
{
return *sLog;
}
void Log::SetLevel(int level)
{
sLog->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 Log::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);
2016-11-17 04:43:29 +00:00
vector<char> buf((unsigned long) (vsnprintf(NULL, 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 Log::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;
}