Added logger

coverity_scan^2
Koncord 8 years ago
parent 2bdacc950b
commit 02a447c589

@ -47,6 +47,7 @@ set(SERVER
Player.cpp
Networking.cpp
Utils.cpp
Log.cpp
Script/Script.cpp Script/ScriptFunction.cpp
Script/ScriptFunctions.cpp
Script/Functions/Translocations.cpp Script/Functions/Stats.cpp Script/Functions/Items.cpp

@ -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

@ -2,6 +2,7 @@
#include <BitStream.h>
#include "Player.hpp"
#include "Networking.hpp"
#include "Log.hpp"
#include <RakPeer.h>
#include <MessageIdentifiers.h>
#include <components/openmw-mp/NetworkMessages.hpp>
@ -69,6 +70,11 @@ int main(int argc, char *argv[])
loadSettings(mgr);
int logLevel = mgr.getInt("loglevel", "General");
if(logLevel < Log::INFO || logLevel > Log::FATAL)
logLevel = Log::INFO;
LOG_INIT(logLevel);
int players = mgr.getInt("players", "General");
int port = mgr.getInt("port", "General");
@ -106,6 +112,8 @@ int main(int argc, char *argv[])
RakNet::RakPeerInterface::DestroyInstance(peer);
if (code == 0)
printf("Quitting peacefully.\n");
LOG_MESSAGE_SIMPLE(Log::INFO, "%s", "Quitting peacefully.");
LOG_QUIT();
return code;
}
Loading…
Cancel
Save