forked from mirror/openmw-tes3mp
[General] Rewrite Log class
This commit is contained in:
parent
d15c674584
commit
7ab01b66e4
2 changed files with 87 additions and 111 deletions
|
@ -1,107 +0,0 @@
|
|||
//
|
||||
// Created by koncord on 15.08.16.
|
||||
//
|
||||
|
||||
#include <cstdarg>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <cstdio>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include "Log.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
Log::Log(int logLevel) : logLevel(logLevel)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Log &Log::Get()
|
||||
{
|
||||
static Log instance(1000);
|
||||
return instance;
|
||||
}
|
||||
|
||||
void Log::SetLevel(int level)
|
||||
{
|
||||
logLevel = level;
|
||||
}
|
||||
|
||||
const char* getTime()
|
||||
{
|
||||
time_t t = time(nullptr);
|
||||
struct tm *tm = localtime(&t);
|
||||
static char result[20];
|
||||
sprintf(result, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
|
||||
1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
|
||||
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
|
||||
{
|
||||
if (level > logLevel || logLevel == LOG_OFF) return;
|
||||
std::stringstream sstr;
|
||||
|
||||
if (hasPrefix)
|
||||
{
|
||||
|
||||
sstr << "[" << getTime() << "] ";
|
||||
|
||||
if (file != nullptr && line != 0)
|
||||
{
|
||||
sstr << "[" << file << ":";
|
||||
sstr << line << "] ";
|
||||
}
|
||||
|
||||
sstr << "[";
|
||||
switch (level)
|
||||
{
|
||||
case LOG_WARN:
|
||||
sstr << "WARN";
|
||||
break;
|
||||
case LOG_ERROR:
|
||||
sstr << "ERR";
|
||||
break;
|
||||
case LOG_FATAL:
|
||||
sstr << "FATAL";
|
||||
break;
|
||||
case LOG_TRACE:
|
||||
sstr << "TRACE";
|
||||
break;
|
||||
case LOG_VERBOSE:
|
||||
case LOG_INFO:
|
||||
sstr << "INFO";
|
||||
break;
|
||||
default:
|
||||
sstr << "INFO";
|
||||
}
|
||||
sstr << "]: ";
|
||||
|
||||
}
|
||||
|
||||
sstr << message;
|
||||
char back = *sstr.str().rbegin();
|
||||
if (back != '\n')
|
||||
sstr << '\n';
|
||||
va_list args;
|
||||
va_start(args, message);
|
||||
vector<char> buf((unsigned long) (vsnprintf(nullptr, 0, sstr.str().c_str(), args) + 1));
|
||||
va_end(args);
|
||||
va_start(args, message);
|
||||
vsnprintf(buf.data(), buf.size(), sstr.str().c_str(), args);
|
||||
va_end(args);
|
||||
cout << buf.data() << flush;
|
||||
}
|
||||
|
||||
string Log::getFilenameTimestamp()
|
||||
{
|
||||
time_t rawtime = time(nullptr);
|
||||
struct tm *timeinfo = localtime(&rawtime);
|
||||
char buffer[25];
|
||||
strftime(buffer, 25, "%Y-%m-%d-%H_%M_%S", timeinfo);
|
||||
return string(buffer);
|
||||
}
|
|
@ -6,6 +6,8 @@
|
|||
#define OPENMW_LOG_HPP
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC system_header
|
||||
|
@ -32,6 +34,26 @@
|
|||
|
||||
class Log
|
||||
{
|
||||
private:
|
||||
std::string Message(boost::format &message) const
|
||||
{
|
||||
return message.str();
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
std::string Message(boost::format &msg, T arg, Args&&... args) const
|
||||
{
|
||||
msg % arg;
|
||||
return Message(msg, args...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
std::string Message(const std::string &fmt, Args&&... args) const
|
||||
{
|
||||
boost::format msg(fmt);
|
||||
return Message(msg, args...);
|
||||
}
|
||||
|
||||
public:
|
||||
enum
|
||||
{
|
||||
|
@ -44,15 +66,76 @@ public:
|
|||
LOG_TRACE,
|
||||
};
|
||||
|
||||
static Log &Get();
|
||||
void SetLevel(int level);
|
||||
void print(int level, bool hasPrefix, const char *file, int line, const char *message, ...) const;
|
||||
static Log &Get()
|
||||
{
|
||||
static Log instance(1000);
|
||||
return instance;
|
||||
}
|
||||
|
||||
void SetLevel(int level)
|
||||
{
|
||||
logLevel = level;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void print(int level, bool hasPrefix, const char *file, int line, const std::string &message, Args&&... args) const
|
||||
{
|
||||
if (level > logLevel || logLevel == LOG_OFF) return;
|
||||
|
||||
if (hasPrefix)
|
||||
{
|
||||
auto getTime = [this] () {
|
||||
time_t t = time(nullptr);
|
||||
struct tm *tm = localtime(&t);
|
||||
return Message("%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
|
||||
1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||
};
|
||||
|
||||
std::cout << "[" << getTime() << "] ";
|
||||
|
||||
if (file != nullptr && line != 0)
|
||||
{
|
||||
std::cout << "[" << file << ":";
|
||||
std::cout << line << "] ";
|
||||
}
|
||||
|
||||
std::cout << "[";
|
||||
switch (level)
|
||||
{
|
||||
case LOG_WARN:
|
||||
std::cout << "WARN";
|
||||
break;
|
||||
case LOG_ERROR:
|
||||
std::cout << "ERR";
|
||||
break;
|
||||
case LOG_FATAL:
|
||||
std::cout << "FATAL";
|
||||
break;
|
||||
case LOG_TRACE:
|
||||
std::cout << "TRACE";
|
||||
break;
|
||||
case LOG_VERBOSE:
|
||||
case LOG_INFO:
|
||||
std::cout << "INFO";
|
||||
break;
|
||||
default:
|
||||
std::cout << "INFO";
|
||||
}
|
||||
std::cout << "]: ";
|
||||
|
||||
}
|
||||
std::string str = Message(message, std::forward<Args&&>(args)...);
|
||||
if(str.back() != '\n')
|
||||
std::cout << str << std::endl;
|
||||
else
|
||||
std::cout << str << std::flush;
|
||||
}
|
||||
|
||||
Log(const Log &) = delete;
|
||||
Log &operator=(Log &) = delete;
|
||||
private:
|
||||
explicit Log(int logLevel);
|
||||
explicit Log(int logLevel) : logLevel(logLevel) {}
|
||||
int logLevel;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue