FeatherMC/src/logging/Logger.cpp

69 lines
1.5 KiB
C++
Raw Permalink Normal View History

2020-07-29 02:43:59 +01:00
#include "Logger.h"
#include <cstdio>
#include <cstdarg>
// Instead of defining FMT_HEADER_ONLY or building fmtlib
// we can just include all implementation code in Logger.cpp
#include "fmt/src/format.cc"
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
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
namespace
{
class LoggingManager
2020-07-29 05:32:35 +01:00
{
public:
LoggingManager()
2020-07-29 05:32:35 +01:00
{
2020-08-07 06:23:12 +01:00
#ifdef _WIN32
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle != INVALID_HANDLE_VALUE)
{
DWORD mode = 0;
if (GetConsoleMode(handle, &mode))
2020-08-07 06:23:12 +01:00
{
// To enable ANSI escape sequences on Windows 10 we need to set this flag
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(handle, mode);
2020-08-07 06:23:12 +01:00
}
}
#endif
}
};
2020-07-29 05:32:35 +01:00
static LoggingManager s_loggingManager;
2020-08-07 06:23:12 +01:00
}