2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include <time.h>
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Log.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-03 15:37:13 -05:00
|
|
|
cMCLogger * cMCLogger::s_MCLogger = NULL;
|
2013-08-10 06:44:39 -04:00
|
|
|
bool g_ShouldColorOutput = false;
|
2013-02-03 15:37:13 -05:00
|
|
|
|
2013-08-04 02:56:20 -04:00
|
|
|
#ifdef _WIN32
|
2013-08-10 06:50:23 -04:00
|
|
|
#include <io.h> // Needed for _isatty(), not available on Linux
|
|
|
|
|
2013-08-04 02:56:20 -04:00
|
|
|
HANDLE g_Console = GetStdHandle(STD_OUTPUT_HANDLE);
|
2013-08-10 06:44:39 -04:00
|
|
|
WORD g_DefaultConsoleAttrib = 0x07;
|
|
|
|
#elif defined (__linux) && !defined(ANDROID_NDK)
|
2013-08-10 06:51:57 -04:00
|
|
|
#include <unistd.h> // Needed for isatty() on Linux
|
2013-08-04 02:56:20 -04:00
|
|
|
#endif
|
2013-02-03 15:37:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-10 06:44:39 -04:00
|
|
|
|
|
|
|
cMCLogger * cMCLogger::GetInstance(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return s_MCLogger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-03 15:37:13 -05:00
|
|
|
cMCLogger::cMCLogger(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
AString FileName;
|
2013-02-03 15:37:13 -05:00
|
|
|
Printf(FileName, "LOG_%d.txt", (int)time(NULL));
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Log = new cLog(FileName);
|
2013-08-10 07:03:05 -04:00
|
|
|
m_Log->Log("--- Started Log ---\n");
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
s_MCLogger = this;
|
2013-08-10 06:44:39 -04:00
|
|
|
|
|
|
|
#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)
|
2013-08-10 07:07:49 -04:00
|
|
|
g_ShouldColorOutput = isatty(fileno(stdout));
|
2013-08-10 06:44:39 -04:00
|
|
|
// TODO: Check if the terminal supports colors, somehow?
|
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-03 15:37:13 -05:00
|
|
|
cMCLogger::cMCLogger(const AString & a_FileName)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-02-03 15:37:13 -05:00
|
|
|
m_Log = new cLog(a_FileName);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cMCLogger::~cMCLogger()
|
|
|
|
{
|
2013-08-10 07:03:05 -04:00
|
|
|
m_Log->Log("--- Stopped Log ---\n");
|
2012-06-14 09:06:06 -04:00
|
|
|
delete m_Log;
|
|
|
|
if (this == s_MCLogger)
|
|
|
|
s_MCLogger = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ )
|
|
|
|
{
|
|
|
|
switch( a_LogType )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
LOG("%s", a_Text);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
LOGINFO("%s", a_Text);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
LOGWARN("%s", a_Text);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
LOGERROR("%s", a_Text);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG("(#%d#: %s", a_LogType, a_Text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-09 16:40:12 -04:00
|
|
|
void cMCLogger::Log(const char * a_Format, va_list a_ArgList)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cCSLock Lock(m_CriticalSection);
|
2013-08-10 06:44:39 -04:00
|
|
|
SetColor(csRegular);
|
2013-08-09 16:40:12 -04:00
|
|
|
m_Log->Log(a_Format, a_ArgList);
|
2013-08-10 06:44:39 -04:00
|
|
|
ResetColor();
|
2013-08-10 07:03:05 -04:00
|
|
|
puts("");
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-09 16:40:12 -04:00
|
|
|
void cMCLogger::Info(const char * a_Format, va_list a_ArgList)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cCSLock Lock(m_CriticalSection);
|
2013-08-10 06:44:39 -04:00
|
|
|
SetColor(csInfo);
|
2013-08-09 16:40:12 -04:00
|
|
|
m_Log->Log(a_Format, a_ArgList);
|
2013-08-10 06:44:39 -04:00
|
|
|
ResetColor();
|
2013-08-10 07:03:05 -04:00
|
|
|
puts("");
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-09 16:40:12 -04:00
|
|
|
void cMCLogger::Warn(const char * a_Format, va_list a_ArgList)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cCSLock Lock(m_CriticalSection);
|
2013-08-10 06:44:39 -04:00
|
|
|
SetColor(csWarning);
|
2013-08-09 16:40:12 -04:00
|
|
|
m_Log->Log(a_Format, a_ArgList);
|
2013-08-10 06:44:39 -04:00
|
|
|
ResetColor();
|
2013-08-10 07:03:05 -04:00
|
|
|
puts("");
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-09 16:40:12 -04:00
|
|
|
void cMCLogger::Error(const char * a_Format, va_list a_ArgList)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cCSLock Lock(m_CriticalSection);
|
2013-08-10 06:44:39 -04:00
|
|
|
SetColor(csError);
|
2013-08-09 16:40:12 -04:00
|
|
|
m_Log->Log(a_Format, a_ArgList);
|
2013-08-10 06:44:39 -04:00
|
|
|
ResetColor();
|
2013-08-10 07:03:05 -04:00
|
|
|
puts("");
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-09 16:40:12 -04:00
|
|
|
void cMCLogger::SetColor(eColorScheme a_Scheme)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-10 06:44:39 -04:00
|
|
|
if (!g_ShouldColorOutput)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-08-09 16:40:12 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
WORD Attrib = 0x07; // by default, gray on black
|
|
|
|
switch (a_Scheme)
|
|
|
|
{
|
2013-08-10 06:44:39 -04:00
|
|
|
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
|
2013-08-09 16:40:12 -04:00
|
|
|
default: ASSERT(!"Unhandled color scheme");
|
|
|
|
}
|
|
|
|
SetConsoleTextAttribute(g_Console, Attrib);
|
2013-08-10 03:42:20 -04:00
|
|
|
#elif defined(__linux) && !defined(ANDROID_NDK)
|
2013-08-09 16:40:12 -04:00
|
|
|
switch (a_Scheme)
|
|
|
|
{
|
2013-08-10 06:44:39 -04:00
|
|
|
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
|
2013-08-09 16:40:12 -04:00
|
|
|
default: ASSERT(!"Unhandled color scheme");
|
|
|
|
}
|
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-10 06:44:39 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Global functions
|
2013-08-10 06:44:39 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|