FeatherMC/src/logging/Logger.cpp

65 lines
1.4 KiB
C++
Raw Normal View History

2020-07-29 02:43:59 +01:00
#include "Logger.h"
#include <cstdio>
#include <cstdarg>
2020-08-07 06:23:12 +01:00
#ifdef _WIN32
2020-07-29 05:32:35 +01:00
2020-08-07 06:23:12 +01:00
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
2020-07-29 05:32:35 +01:00
2020-08-07 06:23:12 +01:00
#include <windows.h>
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
2020-07-29 05:32:35 +01:00
2020-08-07 06:23:12 +01:00
#endif
2020-07-29 05:32:35 +01:00
2020-08-07 06:23:12 +01:00
namespace Feather::Log
{
namespace Channels
2020-07-29 02:43:59 +01:00
{
2020-08-07 06:23:12 +01:00
ChannelID General = Logger::Instance().RegisterChannel("General");
2020-07-29 02:43:59 +01:00
}
2020-07-29 05:32:35 +01:00
ChannelID Logger::RegisterChannel(const char* name)
{
2020-08-07 06:23:12 +01:00
size_t id = m_channels.size();
m_channels.emplace_back(name);
2020-07-29 05:32:35 +01:00
return id;
}
2020-08-07 06:23:12 +01:00
Logger& Logger::Instance()
{
static std::unique_ptr<Logger> s_logger = nullptr;
if (s_logger == nullptr)
s_logger = std::make_unique<Logger>();
2020-07-29 05:32:35 +01:00
2020-08-07 06:23:12 +01:00
return *s_logger;
}
2020-07-29 05:32:35 +01:00
2020-08-07 06:23:12 +01:00
namespace
2020-07-29 05:32:35 +01:00
{
2020-08-07 06:23:12 +01:00
class LoggingManager
2020-07-29 05:32:35 +01:00
{
2020-08-07 06:23:12 +01:00
public:
LoggingManager()
{
#ifdef _WIN32
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle != INVALID_HANDLE_VALUE)
{
DWORD mode = 0;
if (GetConsoleMode(handle, &mode))
{
// To enable ANSI escape sequences on Windows 10 we need to set this flag
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(handle, mode);
}
}
2020-07-29 05:32:35 +01:00
#endif
2020-08-07 06:23:12 +01:00
}
};
2020-07-29 05:32:35 +01:00
2020-08-07 06:23:12 +01:00
static LoggingManager s_networkManager;
}
}