2014-08-12 11:05:04 -04:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
|
|
|
#include "LoggerListeners.h"
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <io.h> // Needed for _isatty(), not available on Linux
|
2014-08-12 16:43:04 -04:00
|
|
|
#include <time.h>
|
2014-08-12 11:05:04 -04:00
|
|
|
#elif defined(__linux) && !defined(ANDROID_NDK)
|
|
|
|
#include <unistd.h> // Needed for isatty() on Linux
|
|
|
|
#elif defined(ANDROID_NDK)
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || (defined (__linux) && !defined(ANDROID_NDK))
|
|
|
|
class cColouredConsoleListener
|
|
|
|
: public cLogger::cListener
|
|
|
|
{
|
2014-08-13 07:28:45 -04:00
|
|
|
protected:
|
2014-08-12 11:05:04 -04:00
|
|
|
|
|
|
|
virtual void SetLogColour(cLogger::eLogLevel a_LogLevel) = 0;
|
|
|
|
virtual void SetDefaultLogColour() = 0;
|
|
|
|
|
|
|
|
virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override
|
|
|
|
{
|
|
|
|
SetLogColour(a_LogLevel);
|
2014-08-13 07:28:45 -04:00
|
|
|
fputs(a_Message.c_str(), stdout);
|
2014-08-12 11:05:04 -04:00
|
|
|
SetDefaultLogColour();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
class cWindowsConsoleListener
|
|
|
|
: public cColouredConsoleListener
|
|
|
|
{
|
2014-08-12 16:43:04 -04:00
|
|
|
typedef cColouredConsoleListener super;
|
2014-08-12 11:05:04 -04:00
|
|
|
public:
|
|
|
|
cWindowsConsoleListener(HANDLE a_Console, WORD a_DefaultConsoleAttrib) :
|
|
|
|
m_Console(a_Console),
|
|
|
|
m_DefaultConsoleAttrib(a_DefaultConsoleAttrib)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-13 07:28:45 -04:00
|
|
|
#ifdef _DEBUG
|
2014-08-12 11:05:04 -04:00
|
|
|
virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override
|
|
|
|
{
|
2014-08-12 16:43:04 -04:00
|
|
|
super::Log(a_Message, a_LogLevel);
|
2014-08-12 11:05:04 -04:00
|
|
|
// In a Windows Debug build, output the log to debug console as well:
|
|
|
|
OutputDebugStringA(a_Message.c_str());
|
|
|
|
}
|
|
|
|
#endif
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
virtual void SetLogColour(cLogger::eLogLevel a_LogLevel) override
|
|
|
|
{
|
|
|
|
// by default, gray on black
|
2014-08-12 16:43:04 -04:00
|
|
|
WORD Attrib = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
|
2014-08-12 11:05:04 -04:00
|
|
|
switch (a_LogLevel)
|
|
|
|
{
|
|
|
|
case cLogger::llRegular:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-27 08:13:13 -04:00
|
|
|
// Gray on black
|
2014-08-12 16:43:04 -04:00
|
|
|
Attrib = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llInfo:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
// Yellow on black
|
2014-08-12 16:43:04 -04:00
|
|
|
Attrib = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llWarning:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
// Red on black
|
|
|
|
Attrib = FOREGROUND_RED | FOREGROUND_INTENSITY;
|
2014-08-12 16:43:04 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llError:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
|
|
|
// Black on red
|
2014-08-12 11:05:04 -04:00
|
|
|
Attrib = BACKGROUND_RED | BACKGROUND_INTENSITY;
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
|
|
|
SetConsoleTextAttribute(m_Console, Attrib);
|
|
|
|
}
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
virtual void SetDefaultLogColour() override
|
2014-08-27 08:13:13 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
SetConsoleTextAttribute(m_Console, m_DefaultConsoleAttrib);
|
|
|
|
}
|
2014-08-12 16:43:04 -04:00
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
private:
|
2014-08-12 16:43:04 -04:00
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
HANDLE m_Console;
|
|
|
|
WORD m_DefaultConsoleAttrib;
|
|
|
|
};
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
#elif defined (__linux) && !defined(ANDROID_NDK)
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
class cLinuxConsoleListener
|
|
|
|
: public cColouredConsoleListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void SetLogColour(cLogger::eLogLevel a_LogLevel) override
|
|
|
|
{
|
|
|
|
switch (a_LogLevel)
|
|
|
|
{
|
|
|
|
case cLogger::llRegular:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-27 08:13:13 -04:00
|
|
|
// Whatever the console default is
|
2014-08-12 11:05:04 -04:00
|
|
|
printf("\x1b[0m");
|
2014-08-27 08:13:13 -04:00
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llInfo:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-27 08:13:13 -04:00
|
|
|
// Yellow on black
|
2014-08-12 11:05:04 -04:00
|
|
|
printf("\x1b[33;1m");
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llWarning:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
// Red on black
|
|
|
|
printf("\x1b[31;1m");
|
2014-08-27 08:13:13 -04:00
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llError:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
// Yellow on red
|
|
|
|
printf("\x1b[1;33;41;1m");
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
virtual void SetDefaultLogColour() override
|
|
|
|
{
|
2014-08-27 08:13:13 -04:00
|
|
|
// Whatever the console default is
|
2014-08-12 11:05:04 -04:00
|
|
|
printf("\x1b[0m");
|
|
|
|
}
|
|
|
|
};
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
#elif defined(ANDROID_NDK)
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
class cAndroidConsoleListener
|
|
|
|
: public cLogger::cListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override
|
|
|
|
{
|
|
|
|
android_LogPriority AndroidLogLevel;
|
|
|
|
switch (a_LogLevel)
|
|
|
|
{
|
|
|
|
case cLogger::llRegular:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
AndroidLogLevel = ANDROID_LOG_VERBOSE;
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llInfo:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
AndroidLogLevel = ANDROID_LOG_INFO;
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llWarning:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
AndroidLogLevel = ANDROID_LOG_WARNING;
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llError:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
AndroidLogLevel = ANDROID_LOG_ERROR;
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
|
|
|
__android_log_print(AndroidLogLevel, "MCServer", "%s", a_Message.c_str());
|
|
|
|
}
|
|
|
|
};
|
2014-08-12 16:43:04 -04:00
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
#endif
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
class cVanillaCPPConsoleListener
|
|
|
|
: public cLogger::cListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override
|
|
|
|
{
|
|
|
|
AString LogLevelString;
|
|
|
|
switch (a_LogLevel)
|
|
|
|
{
|
|
|
|
case cLogger::llRegular:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
LogLevelString = "Log";
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llInfo:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
LogLevelString = "Info";
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llWarning:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
LogLevelString = "Warning";
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llError:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
LogLevelString = "Error";
|
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
|
|
|
printf("%s: %s", LogLevelString.c_str(), a_Message.c_str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
cLogger::cListener * MakeConsoleListener(void)
|
2014-08-12 11:05:04 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
// See whether we are writing to a console the default console attrib:
|
|
|
|
bool ShouldColorOutput = (_isatty(_fileno(stdin)) != 0);
|
|
|
|
if (ShouldColorOutput)
|
|
|
|
{
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO sbi;
|
2014-08-12 16:43:04 -04:00
|
|
|
HANDLE Console = GetStdHandle(STD_OUTPUT_HANDLE);
|
2014-08-12 11:05:04 -04:00
|
|
|
GetConsoleScreenBufferInfo(Console, &sbi);
|
|
|
|
WORD DefaultConsoleAttrib = sbi.wAttributes;
|
|
|
|
return new cWindowsConsoleListener(Console, DefaultConsoleAttrib);
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new cVanillaCPPConsoleListener;
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined (__linux) && !defined(ANDROID_NDK)
|
|
|
|
// TODO: lookup terminal in terminfo
|
|
|
|
if (isatty(fileno(stdout)))
|
|
|
|
{
|
|
|
|
return new cLinuxConsoleListener();
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-12 11:05:04 -04:00
|
|
|
return new cVanillaCPPConsoleListener();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return new cVanillaCPPConsoleListener();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cFileListener:
|
|
|
|
|
|
|
|
cFileListener::cFileListener(void)
|
2014-08-12 11:05:04 -04:00
|
|
|
{
|
|
|
|
cFile::CreateFolder(FILE_IO_PREFIX + AString("logs"));
|
|
|
|
AString FileName;
|
|
|
|
FileName = Printf("%s%sLOG_%d.txt", FILE_IO_PREFIX, "logs/", (int)time(NULL));
|
|
|
|
m_File.Open(FileName, cFile::fmAppend);
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
void cFileListener::Log(AString a_Message, cLogger::eLogLevel a_LogLevel)
|
|
|
|
{
|
2014-08-13 07:31:19 -04:00
|
|
|
const char * LogLevelPrefix = "Unkn ";
|
2014-08-12 11:05:04 -04:00
|
|
|
switch (a_LogLevel)
|
|
|
|
{
|
|
|
|
case cLogger::llRegular:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-13 07:31:19 -04:00
|
|
|
LogLevelPrefix = " ";
|
2014-08-12 11:05:04 -04:00
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llInfo:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-13 07:31:19 -04:00
|
|
|
LogLevelPrefix = "info ";
|
2014-08-12 11:05:04 -04:00
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llWarning:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-13 07:31:19 -04:00
|
|
|
LogLevelPrefix = "Warn ";
|
2014-08-12 11:05:04 -04:00
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
case cLogger::llError:
|
2014-08-12 16:43:04 -04:00
|
|
|
{
|
2014-08-13 07:31:19 -04:00
|
|
|
LogLevelPrefix = "Err ";
|
2014-08-12 11:05:04 -04:00
|
|
|
break;
|
2014-08-12 16:43:04 -04:00
|
|
|
}
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
2014-08-13 07:31:19 -04:00
|
|
|
m_File.Printf("%s%s", LogLevelPrefix, a_Message.c_str());
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|