Support ANSI color codes in the console logger for Linux/Mac

This commit is contained in:
Pierre Bourdon 2012-06-19 12:11:15 +02:00
parent 34606f34a9
commit 988bd53b5f
2 changed files with 26 additions and 1 deletions

View File

@ -35,6 +35,9 @@ ConsoleListener::ConsoleListener()
{
#ifdef _WIN32
hConsole = NULL;
bUseColor = true;
#else
bUseColor = isatty(fileno(stdout));
#endif
}
@ -299,7 +302,28 @@ void ConsoleListener::Log(LogTypes::LOG_LEVELS Level, const char *Text)
SetConsoleTextAttribute(hConsole, Color);
WriteConsole(hConsole, Text, (DWORD)strlen(Text), &cCharsWritten, NULL);
#else
fprintf(stderr, "%s", Text);
char ColorAttr[16] = "";
char ResetAttr[16] = "";
if (bUseColor)
{
strcpy(ResetAttr, "\033[0m");
switch (Level)
{
case NOTICE_LEVEL: // light green
strcpy(ColorAttr, "\033[92m");
break;
case ERROR_LEVEL: // light red
strcpy(ColorAttr, "\033[91m");
break;
case WARNING_LEVEL: // light yellow
strcpy(ColorAttr, "\033[93m");
break;
default:
break;
}
}
fprintf(stderr, "%s%s%s", ColorAttr, Text, ResetAttr);
#endif
}
// Clear console screen

View File

@ -48,6 +48,7 @@ private:
HWND GetHwnd(void);
HANDLE hConsole;
#endif
bool bUseColor;
};
#endif // _CONSOLELISTENER_H