#include "Logging.h"
#include "bx/string.h"

#define WIN32_LEAN_AND_MEAN
#include "Windows.h"

#define ESC "\x1B["
#define YELLOW ESC "33m"
#define RED ESC "31m"
#define END ESC "39m"

namespace
{
    char LineBuffer[1024]{0};
    char OutBuffer[1024]{0};
    constexpr char LogFormat[]{"Line %u: %s\n"};
    constexpr char WarnFormat[]{YELLOW "Line %u: %s" END "\n"};
    constexpr char ErrorFormat[]{RED "Line %u: %s" END "\n"};
} // namespace

void Logging::Log(ELogType logType, uint32_t line, const char* format, ...)
{
    const char* LineFormat = LogFormat;
    if (logType == ELogType::Warn) LineFormat = WarnFormat;
    if (logType == ELogType::Error) LineFormat = ErrorFormat;

    va_list args;
    va_start(args, format);
    bx::snprintf(LineBuffer, sizeof(LineBuffer), LineFormat, line, format);
    bx::vprintf(LineBuffer, args);
    bx::vsnprintf(OutBuffer, sizeof(OutBuffer), LineBuffer, args);
    va_end(args);
    OutputDebugStringA(OutBuffer);
}
