diff options
-rw-r--r-- | src/Logger.cpp | 56 | ||||
-rw-r--r-- | src/LoggerListeners.cpp | 122 | ||||
-rw-r--r-- | src/LoggerListeners.h | 28 | ||||
-rw-r--r-- | src/OSSupport/File.h | 8 |
4 files changed, 177 insertions, 37 deletions
diff --git a/src/Logger.cpp b/src/Logger.cpp index e0ea973f8..572a0e160 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -2,6 +2,12 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "OSSupport/IsThread.h" +#ifdef _WIN32 + #include <time.h> +#endif + + + cLogger & cLogger::GetInstance(void) @@ -10,35 +16,43 @@ cLogger & cLogger::GetInstance(void) return Instance; } + + + + void cLogger::InitiateMultithreading() { GetInstance(); } + + + + void cLogger::LogSimple(AString a_Message, eLogLevel a_LogLevel) { time_t rawtime; - time ( &rawtime); + time(&rawtime); - struct tm* timeinfo; + struct tm * timeinfo; #ifdef _MSC_VER struct tm timeinforeal; timeinfo = &timeinforeal; localtime_s(timeinfo, &rawtime); #else - timeinfo = localtime( &rawtime); + timeinfo = localtime(&rawtime); #endif AString Line; #ifdef _DEBUG - Printf(Line, "[%04lx|%02d:%02d:%02d] %s\n", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str()); + Printf(Line, "[%04lx|%02d:%02d:%02d] %s\n", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str()); #else - Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str()); + Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str()); #endif cCSLock Lock(m_CriticalSection); - for(size_t i = 0; i < m_LogListeners.size(); i++) + for (size_t i = 0; i < m_LogListeners.size(); i++) { m_LogListeners[i]->Log(a_Message, a_LogLevel); } @@ -55,12 +69,20 @@ void cLogger::Log(const char * a_Format, eLogLevel a_LogLevel, va_list a_ArgList LogSimple(Message, a_LogLevel); } + + + + void cLogger::AttachListener(cListener * a_Listener) { cCSLock Lock(m_CriticalSection); m_LogListeners.push_back(a_Listener); } + + + + void cLogger::DetachListener(cListener * a_Listener) { cCSLock Lock(m_CriticalSection); @@ -69,10 +91,12 @@ void cLogger::DetachListener(cListener * a_Listener) + + //////////////////////////////////////////////////////////////////////////////// // Global functions -void LOG(const char* a_Format, ...) +void LOG(const char * a_Format, ...) { va_list argList; va_start(argList, a_Format); @@ -80,7 +104,11 @@ void LOG(const char* a_Format, ...) va_end(argList); } -void LOGINFO(const char* a_Format, ...) + + + + +void LOGINFO(const char * a_Format, ...) { va_list argList; va_start(argList, a_Format); @@ -88,7 +116,11 @@ void LOGINFO(const char* a_Format, ...) va_end(argList); } -void LOGWARN(const char* a_Format, ...) + + + + +void LOGWARN(const char * a_Format, ...) { va_list argList; va_start(argList, a_Format); @@ -96,7 +128,11 @@ void LOGWARN(const char* a_Format, ...) va_end(argList); } -void LOGERROR(const char* a_Format, ...) + + + + +void LOGERROR(const char * a_Format, ...) { va_list argList; va_start(argList, a_Format); diff --git a/src/LoggerListeners.cpp b/src/LoggerListeners.cpp index d6567abe0..912342a65 100644 --- a/src/LoggerListeners.cpp +++ b/src/LoggerListeners.cpp @@ -5,6 +5,7 @@ #if defined(_WIN32) #include <io.h> // Needed for _isatty(), not available on Linux + #include <time.h> #elif defined(__linux) && !defined(ANDROID_NDK) #include <unistd.h> // Needed for isatty() on Linux #elif defined(ANDROID_NDK) @@ -29,10 +30,15 @@ }; #endif + + + + #ifdef _WIN32 class cWindowsConsoleListener : public cColouredConsoleListener { + typedef cColouredConsoleListener super; public: cWindowsConsoleListener(HANDLE a_Console, WORD a_DefaultConsoleAttrib) : m_Console(a_Console), @@ -43,47 +49,65 @@ #ifdef DEBUG virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override { - cColouredConsoleListener::Log(a_Message, a_LogLevel); + super::Log(a_Message, a_LogLevel); // In a Windows Debug build, output the log to debug console as well: OutputDebugStringA(a_Message.c_str()); } #endif - - + + virtual void SetLogColour(cLogger::eLogLevel a_LogLevel) override { // by default, gray on black - WORD Attrib = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; + WORD Attrib = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; switch (a_LogLevel) { case cLogger::llRegular: + { // Gray on black - Attrib = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; - break; + Attrib = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; + break; + } case cLogger::llInfo: + { // Yellow on black - Attrib = FOREGROUND_GREEN | ; - break; + Attrib = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; + break; + } case cLogger::llWarning: + { // Red on black Attrib = FOREGROUND_RED | FOREGROUND_INTENSITY; - break; + break; + } case cLogger::llError: - // Black on red + { + // Black on red Attrib = BACKGROUND_RED | BACKGROUND_INTENSITY; break; + } } SetConsoleTextAttribute(m_Console, Attrib); } + + virtual void SetDefaultLogColour() override { SetConsoleTextAttribute(m_Console, m_DefaultConsoleAttrib); } + private: + HANDLE m_Console; WORD m_DefaultConsoleAttrib; }; + + + #elif defined (__linux) && !defined(ANDROID_NDK) + + + class cLinuxConsoleListener : public cColouredConsoleListener { @@ -93,30 +117,46 @@ switch (a_LogLevel) { case cLogger::llRegular: + { // Whatever the console default is printf("\x1b[0m"); break; + } case cLogger::llInfo: + { // Yellow on black printf("\x1b[33;1m"); break; + } case cLogger::llWarning: + { // Red on black printf("\x1b[31;1m"); break; + } case cLogger::llError: + { // Yellow on red printf("\x1b[1;33;41;1m"); break; + } } } + + virtual void SetDefaultLogColour() override { // Whatever the console default is printf("\x1b[0m"); } }; + + + #elif defined(ANDROID_NDK) + + + class cAndroidConsoleListener : public cLogger::cListener { @@ -127,23 +167,36 @@ switch (a_LogLevel) { case cLogger::llRegular: + { AndroidLogLevel = ANDROID_LOG_VERBOSE; break; + } case cLogger::llInfo: + { AndroidLogLevel = ANDROID_LOG_INFO; break; + } case cLogger::llWarning: + { AndroidLogLevel = ANDROID_LOG_WARNING; break; + } case cLogger::llError: + { AndroidLogLevel = ANDROID_LOG_ERROR; break; + } } __android_log_print(AndroidLogLevel, "MCServer", "%s", a_Message.c_str()); } }; + #endif + + + + class cVanillaCPPConsoleListener : public cLogger::cListener { @@ -154,17 +207,25 @@ public: switch (a_LogLevel) { case cLogger::llRegular: + { LogLevelString = "Log"; break; + } case cLogger::llInfo: + { LogLevelString = "Info"; break; + } case cLogger::llWarning: + { LogLevelString = "Warning"; break; + } case cLogger::llError: + { LogLevelString = "Error"; break; + } } printf("%s: %s", LogLevelString.c_str(), a_Message.c_str()); } @@ -172,7 +233,9 @@ public: -cLogger::cListener * MakeConsoleListener() + + +cLogger::cListener * MakeConsoleListener(void) { #ifdef _WIN32 // See whether we are writing to a console the default console attrib: @@ -180,12 +243,14 @@ cLogger::cListener * MakeConsoleListener() if (ShouldColorOutput) { CONSOLE_SCREEN_BUFFER_INFO sbi; - HANDLE Console = getStdHandle(STD_OUTPUT_HANDLE); + HANDLE Console = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(Console, &sbi); WORD DefaultConsoleAttrib = sbi.wAttributes; return new cWindowsConsoleListener(Console, DefaultConsoleAttrib); - } else { - return new cVanillaCPPConsoleListener(); + } + else + { + return new cVanillaCPPConsoleListener; } #elif defined (__linux) && !defined(ANDROID_NDK) @@ -193,7 +258,9 @@ cLogger::cListener * MakeConsoleListener() if (isatty(fileno(stdout))) { return new cLinuxConsoleListener(); - } else { + } + else + { return new cVanillaCPPConsoleListener(); } #else @@ -201,7 +268,14 @@ cLogger::cListener * MakeConsoleListener() #endif } -cFileListener::cFileListener() + + + + +//////////////////////////////////////////////////////////////////////////////// +// cFileListener: + +cFileListener::cFileListener(void) { cFile::CreateFolder(FILE_IO_PREFIX + AString("logs")); AString FileName; @@ -209,23 +283,39 @@ cFileListener::cFileListener() m_File.Open(FileName, cFile::fmAppend); } + + + + void cFileListener::Log(AString a_Message, cLogger::eLogLevel a_LogLevel) { AString LogLevelString; switch (a_LogLevel) { case cLogger::llRegular: + { LogLevelString = "Log"; break; + } case cLogger::llInfo: + { LogLevelString = "Info"; break; + } case cLogger::llWarning: + { LogLevelString = "Warning"; break; + } case cLogger::llError: + { LogLevelString = "Error"; break; + } } m_File.Printf("%s: %s", LogLevelString.c_str(), a_Message.c_str()); } + + + + diff --git a/src/LoggerListeners.h b/src/LoggerListeners.h index 4d2889a93..d300184b1 100644 --- a/src/LoggerListeners.h +++ b/src/LoggerListeners.h @@ -1,17 +1,31 @@ #include "Logger.h" + + + + class cFileListener : public cLogger::cListener { - public: - - cFileListener(); - cFileListener(AString a_Filename); +public: + + cFileListener(); + cFileListener(AString a_Filename); + + virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override; - virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override; - private: - cFile m_File; +private: + + cFile m_File; }; + + + + cLogger::cListener * MakeConsoleListener(); + + + + diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h index 8891511c4..dfb38e839 100644 --- a/src/OSSupport/File.h +++ b/src/OSSupport/File.h @@ -60,10 +60,10 @@ public: /** The mode in which to open the file */ enum eMode { - 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 - fmAppend // Write-only. If the file already exists cursor will be moved to the end of the file + 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 + 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 */ |