#include <cstdarg>
#include <iostream>
#include <vector>

#include "application/log.h"

void Log::log(LogLevel level, const std::string &fmt, ...) {
    if (level > this->level)
        return;

    va_list args;

    // First va_start: count how many characters are needed for formatting
    va_start(args, fmt);
    size_t len = std::vsnprintf(NULL, 0, fmt.c_str(), args);
    va_end(args);

    std::vector<char> buf(len + 1);

    // Second va_start: actually write formatted message to buffer
    va_start(args, fmt);
    std::vsnprintf(&buf[0], len + 1, fmt.c_str(), args);
    va_end(args);

    std::cout << this->_getLabel(level) << " " << &buf[0] << std::endl;
}