From 02a447c589e98b80476707710a5955a3e829a518 Mon Sep 17 00:00:00 2001 From: Koncord Date: Mon, 15 Aug 2016 07:45:42 +0800 Subject: [PATCH] Added logger --- apps/openmw-mp/CMakeLists.txt | 1 + apps/openmw-mp/Log.cpp | 81 +++++++++++++++++++++++++++++++++++ apps/openmw-mp/Log.hpp | 44 +++++++++++++++++++ apps/openmw-mp/main.cpp | 8 ++++ 4 files changed, 134 insertions(+) create mode 100644 apps/openmw-mp/Log.cpp create mode 100644 apps/openmw-mp/Log.hpp diff --git a/apps/openmw-mp/CMakeLists.txt b/apps/openmw-mp/CMakeLists.txt index 3f204e611..f5ed74de3 100644 --- a/apps/openmw-mp/CMakeLists.txt +++ b/apps/openmw-mp/CMakeLists.txt @@ -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 diff --git a/apps/openmw-mp/Log.cpp b/apps/openmw-mp/Log.cpp new file mode 100644 index 000000000..72c534826 --- /dev/null +++ b/apps/openmw-mp/Log.cpp @@ -0,0 +1,81 @@ +// +// Created by koncord on 15.08.16. +// + +#include +#include +#include +#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); +} diff --git a/apps/openmw-mp/Log.hpp b/apps/openmw-mp/Log.hpp new file mode 100644 index 000000000..d85bc2afd --- /dev/null +++ b/apps/openmw-mp/Log.hpp @@ -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 diff --git a/apps/openmw-mp/main.cpp b/apps/openmw-mp/main.cpp index ccf314f2b..4e8cd378a 100644 --- a/apps/openmw-mp/main.cpp +++ b/apps/openmw-mp/main.cpp @@ -2,6 +2,7 @@ #include #include "Player.hpp" #include "Networking.hpp" +#include "Log.hpp" #include #include #include @@ -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; } \ No newline at end of file