Merge pull request #56 from mc-server/LinuxColorConsole
Linux color console
This commit is contained in:
commit
e508e09753
@ -117,13 +117,13 @@ void cLog::Log(const char * a_Format, va_list argList)
|
|||||||
|
|
||||||
AString Line;
|
AString Line;
|
||||||
#ifdef _DEBUG
|
#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
|
#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
|
#endif
|
||||||
if (m_File)
|
if (m_File)
|
||||||
{
|
{
|
||||||
fputs(Line.c_str(), m_File);
|
fprintf(m_File, "%s\n", Line.c_str(), m_File);
|
||||||
fflush(m_File);
|
fflush(m_File);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,15 +9,22 @@
|
|||||||
|
|
||||||
|
|
||||||
cMCLogger * cMCLogger::s_MCLogger = NULL;
|
cMCLogger * cMCLogger::s_MCLogger = NULL;
|
||||||
|
bool g_ShouldColorOutput = false;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
#include <io.h> // Needed for _isatty(), not available on Linux
|
||||||
|
|
||||||
HANDLE g_Console = GetStdHandle(STD_OUTPUT_HANDLE);
|
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
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cMCLogger* cMCLogger::GetInstance()
|
|
||||||
|
cMCLogger * cMCLogger::GetInstance(void)
|
||||||
{
|
{
|
||||||
return s_MCLogger;
|
return s_MCLogger;
|
||||||
}
|
}
|
||||||
@ -31,9 +38,23 @@ cMCLogger::cMCLogger(void)
|
|||||||
AString FileName;
|
AString FileName;
|
||||||
Printf(FileName, "LOG_%d.txt", (int)time(NULL));
|
Printf(FileName, "LOG_%d.txt", (int)time(NULL));
|
||||||
m_Log = new cLog(FileName);
|
m_Log = new cLog(FileName);
|
||||||
m_Log->Log("--- Started Log ---");
|
m_Log->Log("--- Started Log ---\n");
|
||||||
|
|
||||||
s_MCLogger = this;
|
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()
|
cMCLogger::~cMCLogger()
|
||||||
{
|
{
|
||||||
m_Log->Log("--- Stopped Log ---");
|
m_Log->Log("--- Stopped Log ---\n");
|
||||||
delete m_Log;
|
delete m_Log;
|
||||||
if (this == s_MCLogger)
|
if (this == s_MCLogger)
|
||||||
s_MCLogger = NULL;
|
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);
|
cCSLock Lock(m_CriticalSection);
|
||||||
SetColor( 0x7 ); // 0x7 is default grey color
|
SetColor(csRegular);
|
||||||
m_Log->Log( a_Format, a_ArgList );
|
m_Log->Log(a_Format, a_ArgList);
|
||||||
SetColor(0x07); // revert color back
|
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);
|
cCSLock Lock(m_CriticalSection);
|
||||||
// for( int i = 0; i < 16; i++)
|
SetColor(csInfo);
|
||||||
// {
|
m_Log->Log(a_Format, a_ArgList);
|
||||||
// for( int j = 0; j < 16; j++ )
|
ResetColor();
|
||||||
// {
|
puts("");
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
cCSLock Lock(m_CriticalSection);
|
||||||
SetColor( 0xc ); // 0xc is red
|
SetColor(csWarning);
|
||||||
m_Log->Log( a_Format, a_ArgList );
|
m_Log->Log(a_Format, a_ArgList);
|
||||||
SetColor(0x07); // revert color back
|
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);
|
cCSLock Lock(m_CriticalSection);
|
||||||
SetColor( 0xc0 ); // 0xc0 is red bg and black text
|
SetColor(csError);
|
||||||
m_Log->Log( a_Format, a_ArgList );
|
m_Log->Log(a_Format, a_ArgList);
|
||||||
SetColor(0x07); // revert color back
|
ResetColor();
|
||||||
|
puts("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cMCLogger::SetColor( unsigned char a_Color )
|
void cMCLogger::SetColor(eColorScheme a_Scheme)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
if (!g_ShouldColorOutput)
|
||||||
SetConsoleTextAttribute(g_Console, a_Color);
|
{
|
||||||
#else
|
return;
|
||||||
(void)a_Color;
|
}
|
||||||
#endif
|
#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
|
// Global functions
|
||||||
|
|
||||||
void LOG(const char* a_Format, ...)
|
void LOG(const char* a_Format, ...)
|
||||||
{
|
{
|
||||||
va_list argList;
|
va_list argList;
|
||||||
|
@ -26,11 +26,23 @@ public: // tolua_export
|
|||||||
|
|
||||||
static cMCLogger* GetInstance();
|
static cMCLogger* GetInstance();
|
||||||
private:
|
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;
|
cCriticalSection m_CriticalSection;
|
||||||
cLog* m_Log;
|
cLog * m_Log;
|
||||||
static cMCLogger* s_MCLogger;
|
static cMCLogger * s_MCLogger;
|
||||||
}; // tolua_export
|
}; // tolua_export
|
||||||
|
|
||||||
extern void LOG(const char* a_Format, ...);
|
extern void LOG(const char* a_Format, ...);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user