forked from teamnwah/openmw-tes3coop
Added logger
parent
2bdacc950b
commit
02a447c589
@ -0,0 +1,81 @@
|
||||
//
|
||||
// Created by koncord on 15.08.16.
|
||||
//
|
||||
|
||||
#include <cstdarg>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include "Log.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
Log *Log::sLog = nullptr;
|
||||
|
||||
Log::Log(int logLevel) : logLevel(logLevel)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Log::Create(int logLevel)
|
||||
{
|
||||
if(sLog != nullptr)
|
||||
return;
|
||||
sLog = new Log(logLevel);
|
||||
}
|
||||
|
||||
void Log::Delete()
|
||||
{
|
||||
if(sLog == nullptr)
|
||||
return
|
||||
delete sLog;
|
||||
sLog = nullptr;
|
||||
}
|
||||
|
||||
const Log &Log::Get()
|
||||
{
|
||||
return *sLog;
|
||||
}
|
||||
|
||||
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",
|
||||
1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Log::print(int level, const char *file, int line, const char *message, ...) const
|
||||
{
|
||||
if(level > logLevel) return;
|
||||
std::string str = "[" + string(getTime()) + "] ";
|
||||
|
||||
if(file != 0 && line != 0)
|
||||
str += "["+ string(file) + ":" + to_string(line) + "] ";
|
||||
|
||||
str += "[";
|
||||
switch(level)
|
||||
{
|
||||
case WARNING:
|
||||
str += "WARN";
|
||||
break;
|
||||
case ERROR:
|
||||
str += "ERR";
|
||||
break;
|
||||
case FATAL:
|
||||
str += "FATAL";
|
||||
break;
|
||||
default:
|
||||
str += "INFO";
|
||||
}
|
||||
str += "]: ";
|
||||
str += message;
|
||||
if(str.back() != '\n')
|
||||
str += '\n';
|
||||
va_list args;
|
||||
va_start(args, message);
|
||||
vprintf(str.c_str(), args);
|
||||
va_end(args);
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by koncord on 15.08.16.
|
||||
//
|
||||
|
||||
#ifndef OPENMW_LOG_HPP
|
||||
#define OPENMW_LOG_HPP
|
||||
|
||||
#if defined(NOLOGS)
|
||||
#define LOG_INIT(logLevel)
|
||||
#define LOG_QUIT()
|
||||
#define LOG_MESSAGE(level, msg, ...)
|
||||
#define LOG_MESSAGE_SIMPLE(level, msg, ...)
|
||||
#else
|
||||
#define LOG_INIT(logLevel) Log::Create(logLevel)
|
||||
#define LOG_QUIT() Log::Delete()
|
||||
#define LOG_MESSAGE(level, msg, ...) Log::Get().print((level), (__FILE__), (__LINE__), (msg), __VA_ARGS__)
|
||||
#define LOG_MESSAGE_SIMPLE(level, msg, ...) Log::Get().print((level), (0), (0), (msg), __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
class Log
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
INFO = 0,
|
||||
WARNING,
|
||||
ERROR,
|
||||
FATAL,
|
||||
};
|
||||
static void Create(int logLevel);
|
||||
static void Delete();
|
||||
static const Log &Get();
|
||||
void print(int level, const char *file, int line, const char *message, ...) const;
|
||||
private:
|
||||
Log(int logLevel);
|
||||
Log( const Log& ) = delete;
|
||||
Log& operator=( const Log& ) = delete;
|
||||
static Log * sLog;
|
||||
int logLevel;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //OPENMW_LOG_HPP
|
Loading…
Reference in New Issue