diff options
author | Alexander Harkness <bearbin@gmail.com> | 2013-08-10 13:40:38 +0200 |
---|---|---|
committer | Alexander Harkness <bearbin@gmail.com> | 2013-08-10 13:40:38 +0200 |
commit | e508e0975314fa37b307456a69e869b8b2e9b43a (patch) | |
tree | 1546e9223bf208bb828ccf0a1708b0b3d26ed37b /source | |
parent | Piston head, when removed, removes the associated piston body, too. (diff) | |
parent | Linux color redirection fixed. (diff) | |
download | cuberite-e508e0975314fa37b307456a69e869b8b2e9b43a.tar cuberite-e508e0975314fa37b307456a69e869b8b2e9b43a.tar.gz cuberite-e508e0975314fa37b307456a69e869b8b2e9b43a.tar.bz2 cuberite-e508e0975314fa37b307456a69e869b8b2e9b43a.tar.lz cuberite-e508e0975314fa37b307456a69e869b8b2e9b43a.tar.xz cuberite-e508e0975314fa37b307456a69e869b8b2e9b43a.tar.zst cuberite-e508e0975314fa37b307456a69e869b8b2e9b43a.zip |
Diffstat (limited to 'source')
-rw-r--r-- | source/Log.cpp | 6 | ||||
-rw-r--r-- | source/MCLogger.cpp | 123 | ||||
-rw-r--r-- | source/MCLogger.h | 20 |
3 files changed, 107 insertions, 42 deletions
diff --git a/source/Log.cpp b/source/Log.cpp index 5f78bba1b..2b505de5d 100644 --- a/source/Log.cpp +++ b/source/Log.cpp @@ -117,13 +117,13 @@ void cLog::Log(const char * a_Format, va_list argList) AString Line; #ifdef _DEBUG - Printf(Line, "[%04x|%02d:%02d:%02d] %s\n", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str()); + Printf(Line, "[%04x|%02d:%02d:%02d] %s", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str()); #else - Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str()); + Printf(Line, "[%02d:%02d:%02d] %s", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str()); #endif if (m_File) { - fputs(Line.c_str(), m_File); + fprintf(m_File, "%s\n", Line.c_str(), m_File); fflush(m_File); } diff --git a/source/MCLogger.cpp b/source/MCLogger.cpp index b0ec4efd5..de0fcae2e 100644 --- a/source/MCLogger.cpp +++ b/source/MCLogger.cpp @@ -9,15 +9,22 @@ cMCLogger * cMCLogger::s_MCLogger = NULL; +bool g_ShouldColorOutput = false; #ifdef _WIN32 + #include <io.h> // 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 <unistd.h> // Needed for isatty() on Linux #endif -cMCLogger* cMCLogger::GetInstance() + +cMCLogger * cMCLogger::GetInstance(void) { return s_MCLogger; } @@ -31,9 +38,23 @@ cMCLogger::cMCLogger(void) AString FileName; Printf(FileName, "LOG_%d.txt", (int)time(NULL)); m_Log = new cLog(FileName); - m_Log->Log("--- Started Log ---"); + m_Log->Log("--- Started Log ---\n"); s_MCLogger = this; + + #ifdef _WIN32 + // See whether we are writing to a console the default console attrib: + g_ShouldColorOutput = (_isatty(_fileno(stdin)) != 0); + if (g_ShouldColorOutput) + { + CONSOLE_SCREEN_BUFFER_INFO sbi; + GetConsoleScreenBufferInfo(g_Console, &sbi); + g_DefaultConsoleAttrib = sbi.wAttributes; + } + #elif defined (__linux) && !defined(ANDROID_NDK) + g_ShouldColorOutput = isatty(fileno(stdout)); + // TODO: Check if the terminal supports colors, somehow? + #endif } @@ -51,7 +72,7 @@ cMCLogger::cMCLogger(const AString & a_FileName) cMCLogger::~cMCLogger() { - m_Log->Log("--- Stopped Log ---"); + m_Log->Log("--- Stopped Log ---\n"); delete m_Log; if (this == s_MCLogger) s_MCLogger = NULL; @@ -87,71 +108,102 @@ void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ ) -void cMCLogger::Log(const char* a_Format, va_list a_ArgList) +void cMCLogger::Log(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor( 0x7 ); // 0x7 is default grey color - m_Log->Log( a_Format, a_ArgList ); - SetColor(0x07); // revert color back + SetColor(csRegular); + m_Log->Log(a_Format, a_ArgList); + ResetColor(); + puts(""); } -void cMCLogger::Info(const char* a_Format, va_list a_ArgList) +void cMCLogger::Info(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); -// for( int i = 0; i < 16; i++) -// { -// for( int j = 0; j < 16; j++ ) -// { -// SetConsoleTextAttribute( hConsole, i | (j<<4) ); -// printf("0x%x", (i|j<<4)); -// } -// printf("\n"); -// } - - SetColor( 0xe ); // 0xe is yellow - m_Log->Log( a_Format, a_ArgList ); - SetColor(0x07); // revert color back + SetColor(csInfo); + m_Log->Log(a_Format, a_ArgList); + ResetColor(); + puts(""); } -void cMCLogger::Warn(const char* a_Format, va_list a_ArgList) +void cMCLogger::Warn(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor( 0xc ); // 0xc is red - m_Log->Log( a_Format, a_ArgList ); - SetColor(0x07); // revert color back + SetColor(csWarning); + m_Log->Log(a_Format, a_ArgList); + ResetColor(); + puts(""); } -void cMCLogger::Error(const char* a_Format, va_list a_ArgList) +void cMCLogger::Error(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor( 0xc0 ); // 0xc0 is red bg and black text - m_Log->Log( a_Format, a_ArgList ); - SetColor(0x07); // revert color back + SetColor(csError); + m_Log->Log(a_Format, a_ArgList); + ResetColor(); + puts(""); } -void cMCLogger::SetColor( unsigned char a_Color ) +void cMCLogger::SetColor(eColorScheme a_Scheme) { -#ifdef _WIN32 - SetConsoleTextAttribute(g_Console, a_Color); -#else - (void)a_Color; -#endif + if (!g_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 (!g_ShouldColorOutput) + { + return; + } + #ifdef _WIN32 + SetConsoleTextAttribute(g_Console, g_DefaultConsoleAttrib); + #elif defined(__linux) && !defined(ANDROID_NDK) + printf("\x1b[0m"); + #endif } @@ -160,6 +212,7 @@ void cMCLogger::SetColor( unsigned char a_Color ) ////////////////////////////////////////////////////////////////////////// // Global functions + void LOG(const char* a_Format, ...) { va_list argList; diff --git a/source/MCLogger.h b/source/MCLogger.h index 0c4566400..6f7d34e66 100644 --- a/source/MCLogger.h +++ b/source/MCLogger.h @@ -26,11 +26,23 @@ public: // tolua_export static cMCLogger* GetInstance(); private: - void SetColor( unsigned char a_Color ); - + enum eColorScheme + { + csRegular, + csInfo, + csWarning, + csError, + } ; + + /// 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); + cCriticalSection m_CriticalSection; - cLog* m_Log; - static cMCLogger* s_MCLogger; + cLog * m_Log; + static cMCLogger * s_MCLogger; }; // tolua_export extern void LOG(const char* a_Format, ...); |