From 806d0936dc94f235858ffe1772a6215f86c5d000 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 10 Aug 2014 19:34:11 +0100 Subject: [PATCH] First Implementatation of new Loggin framework --- SetFlags.cmake | 2 + src/Bindings/LuaFunctions.h | 2 +- src/Bindings/ManualBindings.cpp | 10 +- src/CMakeLists.txt | 6 +- src/CompositeChat.cpp | 24 +-- src/CompositeChat.h | 2 +- src/Globals.h | 2 +- src/MCLogger.cpp | 267 -------------------------------- src/MCLogger.h | 95 ------------ src/OSSupport/File.cpp | 5 +- src/OSSupport/File.h | 3 +- src/Root.cpp | 26 +++- src/Root.h | 2 - src/StringUtils.h | 1 - src/main.cpp | 2 + 15 files changed, 51 insertions(+), 398 deletions(-) delete mode 100644 src/MCLogger.cpp delete mode 100644 src/MCLogger.h diff --git a/SetFlags.cmake b/SetFlags.cmake index a5a61eaa4..0e2e0c277 100644 --- a/SetFlags.cmake +++ b/SetFlags.cmake @@ -1,3 +1,5 @@ + + macro (add_flags_lnk FLAGS) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAGS}") set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${FLAGS}") diff --git a/src/Bindings/LuaFunctions.h b/src/Bindings/LuaFunctions.h index 2ea37d7a4..6a645ed53 100644 --- a/src/Bindings/LuaFunctions.h +++ b/src/Bindings/LuaFunctions.h @@ -1,6 +1,6 @@ #pragma once -#include "../MCLogger.h" +#include "LogDispacher.h" #include // tolua_begin diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 9ba1501c5..d792cd0ee 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -168,7 +168,7 @@ static AString GetLogMessage(lua_State * tolua_S) static int tolua_LOG(lua_State * tolua_S) { // If the param is a cCompositeChat, read the log level from it: - cMCLogger::eLogLevel LogLevel = cMCLogger::llRegular; + Logger::eLogLevel LogLevel = Logger::llRegular; tolua_Error err; if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err)) { @@ -176,7 +176,7 @@ static int tolua_LOG(lua_State * tolua_S) } // Log the message: - cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), LogLevel); + Logger::GetInstance().LogSimple(GetLogMessage(tolua_S).c_str(), LogLevel); return 0; } @@ -186,7 +186,7 @@ static int tolua_LOG(lua_State * tolua_S) static int tolua_LOGINFO(lua_State * tolua_S) { - cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), cMCLogger::llInfo); + Logger::GetInstance().LogSimple(GetLogMessage(tolua_S).c_str(), Logger::llInfo); return 0; } @@ -196,7 +196,7 @@ static int tolua_LOGINFO(lua_State * tolua_S) static int tolua_LOGWARN(lua_State * tolua_S) { - cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), cMCLogger::llWarning); + Logger::GetInstance().LogSimple(GetLogMessage(tolua_S).c_str(), Logger::llWarning); return 0; } @@ -206,7 +206,7 @@ static int tolua_LOGWARN(lua_State * tolua_S) static int tolua_LOGERROR(lua_State * tolua_S) { - cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), cMCLogger::llError); + Logger::GetInstance().LogSimple(GetLogMessage(tolua_S).c_str(), Logger::llError); return 0; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index db9c61082..0feee4fcb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required (VERSION 2.8.2) project (MCServer) + include_directories (SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/../lib/") include_directories (SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/../lib/jsoncpp/include") include_directories (SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/../lib/polarssl/include") @@ -41,8 +42,9 @@ SET (SRCS LightingThread.cpp LineBlockTracer.cpp LinearInterpolation.cpp + Listeners.cpp Log.cpp - MCLogger.cpp + LogDispacher.cpp Map.cpp MapManager.cpp MobCensus.cpp @@ -108,7 +110,7 @@ SET (HDRS LinearInterpolation.h LinearUpscale.h Log.h - MCLogger.h + LogDispacher.h Map.h MapManager.h Matrix4.h diff --git a/src/CompositeChat.cpp b/src/CompositeChat.cpp index f1a797897..b702447be 100644 --- a/src/CompositeChat.cpp +++ b/src/CompositeChat.cpp @@ -353,23 +353,23 @@ AString cCompositeChat::ExtractText(void) const -cMCLogger::eLogLevel cCompositeChat::MessageTypeToLogLevel(eMessageType a_MessageType) +Logger::eLogLevel cCompositeChat::MessageTypeToLogLevel(eMessageType a_MessageType) { switch (a_MessageType) { - case mtCustom: return cMCLogger::llRegular; - case mtFailure: return cMCLogger::llWarning; - case mtInformation: return cMCLogger::llInfo; - case mtSuccess: return cMCLogger::llRegular; - case mtWarning: return cMCLogger::llWarning; - case mtFatal: return cMCLogger::llError; - case mtDeath: return cMCLogger::llRegular; - case mtPrivateMessage: return cMCLogger::llRegular; - case mtJoin: return cMCLogger::llRegular; - case mtLeave: return cMCLogger::llRegular; + case mtCustom: return Logger::llRegular; + case mtFailure: return Logger::llWarning; + case mtInformation: return Logger::llInfo; + case mtSuccess: return Logger::llRegular; + case mtWarning: return Logger::llWarning; + case mtFatal: return Logger::llError; + case mtDeath: return Logger::llRegular; + case mtPrivateMessage: return Logger::llRegular; + case mtJoin: return Logger::llRegular; + case mtLeave: return Logger::llRegular; } ASSERT(!"Unhandled MessageType"); - return cMCLogger::llError; + return Logger::llError; } diff --git a/src/CompositeChat.h b/src/CompositeChat.h index 1ad196f1d..cc7c446c3 100644 --- a/src/CompositeChat.h +++ b/src/CompositeChat.h @@ -196,7 +196,7 @@ public: /** Converts the MessageType to a LogLevel value. Used by the logging bindings when logging a cCompositeChat object. */ - static cMCLogger::eLogLevel MessageTypeToLogLevel(eMessageType a_MessageType); + static Logger::eLogLevel MessageTypeToLogLevel(eMessageType a_MessageType); protected: /** All the parts that */ diff --git a/src/Globals.h b/src/Globals.h index 60ee456c9..ae7a68e7f 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -249,7 +249,7 @@ template class SizeChecker; #include "OSSupport/Event.h" #include "OSSupport/Thread.h" #include "OSSupport/File.h" - #include "MCLogger.h" + #include "LogDispacher.h" #else // Logging functions void inline LOGERROR(const char* a_Format, ...) FORMATSTRING(1, 2); diff --git a/src/MCLogger.cpp b/src/MCLogger.cpp deleted file mode 100644 index 044e83937..000000000 --- a/src/MCLogger.cpp +++ /dev/null @@ -1,267 +0,0 @@ - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - -#include -#include "Log.h" - - - - - -cMCLogger * cMCLogger::s_MCLogger = NULL; - -#ifdef _WIN32 - #include // Needed for _isatty(), not available on Linux - - HANDLE g_Console = GetStdHandle(STD_OUTPUT_HANDLE); - WORD g_DefaultConsoleAttrib = 0x07; -#elif defined (__linux) && !defined(ANDROID_NDK) - #include // Needed for isatty() on Linux -#endif - - - - - -cMCLogger * cMCLogger::GetInstance(void) -{ - return s_MCLogger; -} - - - - - -cMCLogger::cMCLogger(void): - m_ShouldColorOutput(false) -{ - AString FileName; - Printf(FileName, "LOG_%d.txt", (int)time(NULL)); - InitLog(FileName); -} - - - - - -cMCLogger::cMCLogger(const AString & a_FileName) -{ - InitLog(a_FileName); -} - - - - - -cMCLogger::~cMCLogger() -{ - m_Log->Log("--- Stopped Log ---\n"); - delete m_Log; - m_Log = NULL; - if (this == s_MCLogger) - { - s_MCLogger = NULL; - } -} - - - - - -void cMCLogger::InitLog(const AString & a_FileName) -{ - m_Log = new cLog(a_FileName); - m_Log->Log("--- Started Log ---\n"); - - s_MCLogger = this; - - #ifdef _WIN32 - // See whether we are writing to a console the default console attrib: - m_ShouldColorOutput = (_isatty(_fileno(stdin)) != 0); - if (m_ShouldColorOutput) - { - CONSOLE_SCREEN_BUFFER_INFO sbi; - GetConsoleScreenBufferInfo(g_Console, &sbi); - g_DefaultConsoleAttrib = sbi.wAttributes; - } - #elif defined (__linux) && !defined(ANDROID_NDK) - m_ShouldColorOutput = isatty(fileno(stdout)); - // TODO: Check if the terminal supports colors, somehow? - #endif -} - - - - - -void cMCLogger::LogSimple(const char * a_Text, eLogLevel a_LogLevel) -{ - switch (a_LogLevel) - { - case llRegular: - { - LOG("%s", a_Text); - break; - } - case llInfo: - { - LOGINFO("%s", a_Text); - break; - } - case llWarning: - { - LOGWARN("%s", a_Text); - break; - } - case llError: - { - LOGERROR("%s", a_Text); - break; - } - } -} - - - - - -void cMCLogger::Log(const char * a_Format, va_list a_ArgList) -{ - cCSLock Lock(m_CriticalSection); - SetColor(csRegular); - m_Log->Log(a_Format, a_ArgList); - ResetColor(); - puts(""); -} - - - - - -void cMCLogger::Info(const char * a_Format, va_list a_ArgList) -{ - cCSLock Lock(m_CriticalSection); - SetColor(csInfo); - m_Log->Log(a_Format, a_ArgList); - ResetColor(); - puts(""); -} - - - - - -void cMCLogger::Warn(const char * a_Format, va_list a_ArgList) -{ - cCSLock Lock(m_CriticalSection); - SetColor(csWarning); - m_Log->Log(a_Format, a_ArgList); - ResetColor(); - puts(""); -} - - - - - -void cMCLogger::Error(const char * a_Format, va_list a_ArgList) -{ - cCSLock Lock(m_CriticalSection); - SetColor(csError); - m_Log->Log(a_Format, a_ArgList); - ResetColor(); - puts(""); -} - - - - - -void cMCLogger::SetColor(eColorScheme a_Scheme) -{ - if (!m_ShouldColorOutput) - { - return; - } - #ifdef _WIN32 - WORD Attrib = 0x07; // by default, gray on black - switch (a_Scheme) - { - case csRegular: Attrib = 0x07; break; // Gray on black - case csInfo: Attrib = 0x0e; break; // Yellow on black - case csWarning: Attrib = 0x0c; break; // Read on black - case csError: Attrib = 0xc0; break; // Black on red - default: ASSERT(!"Unhandled color scheme"); - } - SetConsoleTextAttribute(g_Console, Attrib); - #elif defined(__linux) && !defined(ANDROID_NDK) - switch (a_Scheme) - { - case csRegular: printf("\x1b[0m"); break; // Whatever the console default is - case csInfo: printf("\x1b[33;1m"); break; // Yellow on black - case csWarning: printf("\x1b[31;1m"); break; // Red on black - case csError: printf("\x1b[1;33;41;1m"); break; // Yellow on red - default: ASSERT(!"Unhandled color scheme"); - } - #endif -} - - - - - -void cMCLogger::ResetColor(void) -{ - if (!m_ShouldColorOutput) - { - return; - } - #ifdef _WIN32 - SetConsoleTextAttribute(g_Console, g_DefaultConsoleAttrib); - #elif defined(__linux) && !defined(ANDROID_NDK) - printf("\x1b[0m"); - #endif -} - - - - - -//////////////////////////////////////////////////////////////////////////////// -// Global functions - -void LOG(const char* a_Format, ...) -{ - va_list argList; - va_start(argList, a_Format); - cMCLogger::GetInstance()->Log( a_Format, argList); - va_end(argList); -} - -void LOGINFO(const char* a_Format, ...) -{ - va_list argList; - va_start(argList, a_Format); - cMCLogger::GetInstance()->Info( a_Format, argList); - va_end(argList); -} - -void LOGWARN(const char* a_Format, ...) -{ - va_list argList; - va_start(argList, a_Format); - cMCLogger::GetInstance()->Warn( a_Format, argList); - va_end(argList); -} - -void LOGERROR(const char* a_Format, ...) -{ - va_list argList; - va_start(argList, a_Format); - cMCLogger::GetInstance()->Error( a_Format, argList); - va_end(argList); -} - - - - diff --git a/src/MCLogger.h b/src/MCLogger.h deleted file mode 100644 index aa3a52d02..000000000 --- a/src/MCLogger.h +++ /dev/null @@ -1,95 +0,0 @@ - -#pragma once - - - - -class cLog; - - - - - -class cMCLogger -{ -public: - enum eLogLevel - { - llRegular, - llInfo, - llWarning, - llError, - }; - // tolua_end - - /** Creates a logger with the default filename, "logs/LOG_.log" */ - cMCLogger(void); - - /** Creates a logger with the specified filename inside "logs" folder */ - cMCLogger(const AString & a_FileName); - - ~cMCLogger(); - - void Log (const char * a_Format, va_list a_ArgList) FORMATSTRING(2, 0); - void Info (const char * a_Format, va_list a_ArgList) FORMATSTRING(2, 0); - void Warn (const char * a_Format, va_list a_ArgList) FORMATSTRING(2, 0); - void Error(const char * a_Format, va_list a_ArgList) FORMATSTRING(2, 0); - - /** Logs the simple text message at the specified log level. */ - void LogSimple(const char * a_Text, eLogLevel a_LogLevel = llRegular); - - static cMCLogger * GetInstance(); -private: - enum eColorScheme - { - csRegular, - csInfo, - csWarning, - csError, - } ; - - cCriticalSection m_CriticalSection; - cLog * m_Log; - static cMCLogger * s_MCLogger; - bool m_ShouldColorOutput; - - - /// Sets the specified color scheme in the terminal (TODO: if coloring available) - void SetColor(eColorScheme a_Scheme); - - /// Resets the color back to whatever is the default in the terminal - void ResetColor(void); - - /// Common initialization for all constructors, creates a logfile with the specified name and assigns s_MCLogger to this - void InitLog(const AString & a_FileName); -}; - - - - - -extern void LOG(const char* a_Format, ...) FORMATSTRING(1, 2); -extern void LOGINFO(const char* a_Format, ...) FORMATSTRING(1, 2); -extern void LOGWARN(const char* a_Format, ...) FORMATSTRING(1, 2); -extern void LOGERROR(const char* a_Format, ...) FORMATSTRING(1, 2); - - - - - -// In debug builds, translate LOGD to LOG, otherwise leave it out altogether: -#ifdef _DEBUG - #define LOGD LOG -#else - #define LOGD(...) -#endif // _DEBUG - - - - - -#define LOGWARNING LOGWARN - - - - diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index ff6fb5898..af8a832f6 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -70,6 +70,7 @@ bool cFile::Open(const AString & iFileName, eMode iMode) case fmRead: Mode = "rb"; break; case fmWrite: Mode = "wb"; break; case fmReadWrite: Mode = "rb+"; break; + case fmAppend: Mode = "a+"; break; } if (Mode == NULL) { @@ -255,7 +256,7 @@ int cFile::ReadRestOfFile(AString & a_Contents) return -1; } - int DataSize = GetSize() - Tell(); + size_t DataSize = GetSize() - Tell(); // HACK: This depends on the internal knowledge that AString's data() function returns the internal buffer directly a_Contents.assign((size_t)DataSize, '\0'); @@ -459,7 +460,7 @@ int cFile::Printf(const char * a_Fmt, ...) va_start(args, a_Fmt); AppendVPrintf(buf, a_Fmt, args); va_end(args); - return Write(buf.c_str(), (int)buf.length()); + return Write(buf.c_str(), buf.length()); } diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h index 2a7ecf0ed..8891511c4 100644 --- a/src/OSSupport/File.h +++ b/src/OSSupport/File.h @@ -62,7 +62,8 @@ public: { fmRead, // Read-only. If the file doesn't exist, object will not be valid fmWrite, // Write-only. If the file already exists, it will be overwritten - fmReadWrite // Read/write. If the file already exists, it will be left intact; writing will overwrite the data from the beginning + fmReadWrite, // Read/write. If the file already exists, it will be left intact; writing will overwrite the data from the beginning + fmAppend // Write-only. If the file already exists cursor will be moved to the end of the file } ; /** Simple constructor - creates an unopened file object, use Open() to open / create a real file */ diff --git a/src/Root.cpp b/src/Root.cpp index c20cf0d21..72048b631 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -18,6 +18,7 @@ #include "CommandOutput.h" #include "DeadlockDetect.h" #include "OSSupport/Timer.h" +#include "Listeners.h" #include "inifile/iniFile.h" @@ -51,7 +52,6 @@ cRoot::cRoot(void) : m_FurnaceRecipe(NULL), m_WebAdmin(NULL), m_PluginManager(NULL), - m_Log(NULL), m_bStop(false), m_bRestart(false) { @@ -105,10 +105,15 @@ void cRoot::Start(void) HMENU hmenu = GetSystemMenu(hwnd, FALSE); EnableMenuItem(hmenu, SC_CLOSE, MF_GRAYED); // Disable close button when starting up; it causes problems with our CTRL-CLOSE handling #endif + + Logger::cLoggerListener * consoleLogListener = Logger::MakeConsoleListener(); + Logger::cLoggerListener * fileLogListener = new Logger::cFileListener(); + Logger::GetInstance().AttachListener(consoleLogListener); + Logger::GetInstance().AttachListener(fileLogListener); + + LOG("--- Started Log ---\n"); cDeadlockDetect dd; - delete m_Log; - m_Log = new cMCLogger(); m_bStop = false; while (!m_bStop) @@ -249,8 +254,13 @@ void cRoot::Start(void) delete m_Server; m_Server = NULL; LOG("Shutdown successful!"); } - - delete m_Log; m_Log = NULL; + + LOG("--- Stopped Log ---"); + + Logger::GetInstance().DetachListener(consoleLogListener); + delete consoleLogListener; + Logger::GetInstance().DetachListener(fileLogListener); + delete fileLogListener; } @@ -274,15 +284,15 @@ void cRoot::LoadWorlds(cIniFile & IniFile) m_WorldsByName[ DefaultWorldName ] = m_pDefaultWorld; // Then load the other worlds - unsigned int KeyNum = IniFile.FindKey("Worlds"); - unsigned int NumWorlds = IniFile.GetNumValues(KeyNum); + int KeyNum = IniFile.FindKey("Worlds"); + int NumWorlds = IniFile.GetNumValues(KeyNum); if (NumWorlds <= 0) { return; } bool FoundAdditionalWorlds = false; - for (unsigned int i = 0; i < NumWorlds; i++) + for (int i = 0; i < NumWorlds; i++) { AString ValueName = IniFile.GetValueName(KeyNum, i); if (ValueName.compare("World") != 0) diff --git a/src/Root.h b/src/Root.h index 1cd175ab4..6840efcbe 100644 --- a/src/Root.h +++ b/src/Root.h @@ -196,8 +196,6 @@ private: cMojangAPI m_MojangAPI; cHTTPServer m_HTTPServer; - cMCLogger * m_Log; - bool m_bStop; bool m_bRestart; diff --git a/src/StringUtils.h b/src/StringUtils.h index 142aaf59b..3d4379352 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -12,7 +12,6 @@ - typedef std::string AString; typedef std::vector AStringVector; typedef std::list AStringList; diff --git a/src/main.cpp b/src/main.cpp index 106233342..e40035538 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -273,6 +273,8 @@ int main( int argc, char **argv) } } // for i - argv[] + Logger::InitiateMultithreading(); + #if !defined(ANDROID_NDK) try #endif