1
0
cuberite-2a/src/LoggerListeners.cpp

378 lines
7.1 KiB
C++
Raw Normal View History

2014-08-12 11:05:04 -04:00
#include "Globals.h"
#include "LoggerListeners.h"
2015-05-24 07:56:56 -04:00
#include <chrono>
2014-08-12 11:05:04 -04:00
#if defined(_WIN32)
#include <io.h> // Needed for _isatty(), not available on Linux
#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
{
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);
fputs(a_Message.c_str(), stdout);
2014-08-12 11:05:04 -04:00
SetDefaultLogColour();
}
};
#endif
2014-08-12 11:05:04 -04:00
#ifdef _WIN32
class cWindowsConsoleListener
: public cColouredConsoleListener
{
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)
{
}
#ifdef _DEBUG
2014-08-12 11:05:04 -04:00
virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override
{
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 11:05:04 -04:00
virtual void SetLogColour(cLogger::eLogLevel a_LogLevel) override
{
// by default, gray on black
WORD Attrib = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
2014-08-12 11:05:04 -04:00
switch (a_LogLevel)
{
case cLogger::llRegular:
{
2014-08-27 08:13:13 -04:00
// Gray on black
Attrib = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
break;
}
2014-08-12 11:05:04 -04:00
case cLogger::llInfo:
{
2014-08-12 11:05:04 -04:00
// Yellow on black
Attrib = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
}
2014-08-12 11:05:04 -04:00
case cLogger::llWarning:
{
2014-08-12 11:05:04 -04:00
// Red on black
Attrib = FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
}
2014-08-12 11:05:04 -04:00
case cLogger::llError:
{
// Black on red
2014-08-12 11:05:04 -04:00
Attrib = BACKGROUND_RED | BACKGROUND_INTENSITY;
break;
}
2014-08-12 11:05:04 -04:00
}
SetConsoleTextAttribute(m_Console, Attrib);
}
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 11:05:04 -04:00
private:
2014-08-12 11:05:04 -04:00
HANDLE m_Console;
WORD m_DefaultConsoleAttrib;
};
2014-08-12 11:05:04 -04:00
#elif defined (__linux) && !defined(ANDROID_NDK)
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-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 11:05:04 -04:00
case cLogger::llInfo:
{
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 11:05:04 -04:00
case cLogger::llWarning:
{
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 11:05:04 -04:00
case cLogger::llError:
{
2014-08-12 11:05:04 -04:00
// Yellow on red
printf("\x1b[1;33;41;1m");
break;
}
2014-08-12 11:05: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 11:05:04 -04:00
#elif defined(ANDROID_NDK)
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 11:05:04 -04:00
AndroidLogLevel = ANDROID_LOG_VERBOSE;
break;
}
2014-08-12 11:05:04 -04:00
case cLogger::llInfo:
{
2014-08-12 11:05:04 -04:00
AndroidLogLevel = ANDROID_LOG_INFO;
break;
}
2014-08-12 11:05:04 -04:00
case cLogger::llWarning:
{
2014-08-12 11:05:04 -04:00
AndroidLogLevel = ANDROID_LOG_WARNING;
break;
}
2014-08-12 11:05:04 -04:00
case cLogger::llError:
{
2014-08-12 11:05:04 -04:00
AndroidLogLevel = ANDROID_LOG_ERROR;
break;
}
2014-08-12 11:05:04 -04:00
}
2015-09-25 04:14:17 -04:00
__android_log_print(AndroidLogLevel, "Cuberite", "%s", a_Message.c_str());
2014-08-12 11:05:04 -04:00
}
};
2014-08-12 11:05:04 -04:00
#endif
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 11:05:04 -04:00
LogLevelString = "Log";
break;
}
2014-08-12 11:05:04 -04:00
case cLogger::llInfo:
{
2014-08-12 11:05:04 -04:00
LogLevelString = "Info";
break;
}
2014-08-12 11:05:04 -04:00
case cLogger::llWarning:
{
2014-08-12 11:05:04 -04:00
LogLevelString = "Warning";
break;
}
2014-08-12 11:05:04 -04:00
case cLogger::llError:
{
2014-08-12 11:05:04 -04:00
LogLevelString = "Error";
break;
}
2014-08-12 11:05:04 -04:00
}
printf("%s: %s", LogLevelString.c_str(), a_Message.c_str());
}
};
// Listener for when stdout is closed, i.e. When running as a daemon.
class cNullConsoleListener
: public cLogger::cListener
{
virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override
{
}
};
std::unique_ptr<cLogger::cListener> MakeConsoleListener(bool a_IsService)
2014-08-12 11:05:04 -04:00
{
if (a_IsService)
{
return cpp14::make_unique<cNullConsoleListener>();
}
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;
HANDLE Console = GetStdHandle(STD_OUTPUT_HANDLE);
2014-08-12 11:05:04 -04:00
GetConsoleScreenBufferInfo(Console, &sbi);
WORD DefaultConsoleAttrib = sbi.wAttributes;
return cpp14::make_unique<cWindowsConsoleListener>(Console, DefaultConsoleAttrib);
}
else
{
return cpp14::make_unique<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 cpp14::make_unique<cLinuxConsoleListener>();
}
else
{
return cpp14::make_unique<cVanillaCPPConsoleListener>();
2014-08-12 11:05:04 -04:00
}
#else
return cpp14::make_unique<cVanillaCPPConsoleListener>();
2014-08-12 11:05:04 -04:00
#endif
}
////////////////////////////////////////////////////////////////////////////////
// cFileListener:
class cFileListener
: public cLogger::cListener
2014-08-12 11:05:04 -04:00
{
public:
cFileListener(void) {}
bool Open()
{
// Assume creation succeeds, as the API does not provide a way to tell if the folder exists.
cFile::CreateFolder(FILE_IO_PREFIX + AString("logs"));
bool success = m_File.Open(
FILE_IO_PREFIX + Printf(
"logs/LOG_%d.txt",
std::chrono::duration_cast<std::chrono::duration<int, std::ratio<1>>>(
std::chrono::system_clock::now().time_since_epoch()
).count()
),
cFile::fmAppend
);
return success;
}
virtual void Log(AString a_Message, cLogger::eLogLevel a_LogLevel) override
2014-08-12 11:05:04 -04:00
{
const char * LogLevelPrefix = "Unkn ";
bool ShouldFlush = false;
switch (a_LogLevel)
{
case cLogger::llRegular:
{
LogLevelPrefix = " ";
break;
}
case cLogger::llInfo:
{
LogLevelPrefix = "Info ";
break;
}
case cLogger::llWarning:
{
LogLevelPrefix = "Warn ";
ShouldFlush = true;
break;
}
case cLogger::llError:
{
LogLevelPrefix = "Err ";
ShouldFlush = true;
break;
}
}
m_File.Printf("%s%s", LogLevelPrefix, a_Message.c_str());
if (ShouldFlush)
{
m_File.Flush();
}
2014-08-12 11:05:04 -04:00
}
private:
cFile m_File;
};
std::pair<bool, std::unique_ptr<cLogger::cListener>> MakeFileListener()
{
auto listener = cpp14::make_unique<cFileListener>();
if (!listener->Open())
{
return {false, nullptr};
}
return {true, std::move(listener)};
2014-08-12 11:05:04 -04:00
}