//
// Created by m on 7/18/23.
//
#include "logger.hpp"
#include <fmt/format.h>
#include <fmt/chrono.h>
#include <iostream>

LogLevel Logger::minimum_loglevel = LogLevel::DEBUG;

void Logger::set_minimum_loglevel(LogLevel logLevel) {
    Logger::minimum_loglevel = logLevel;
}

void Logger::log(LogLevel level, const std::string &msg) {
    if(level < Logger::minimum_loglevel) {
        return;
    }

    auto loglevel_name = "UNKNOWN";
    switch (level) {
        case LogLevel::TRACE:
            loglevel_name = "TRA";
            break;
        case DEBUG:
            loglevel_name = "DEB";
            break;
        case INFO:
            loglevel_name = "INF";
            break;
        case WARNING:
            loglevel_name = "WAR";
            break;
        case ERROR:
            loglevel_name = "ERR";
            break;
        case NONE:
            loglevel_name = "NON";
            break;
    }
    auto output = fmt::format("[{}][{}] {}", std::chrono::system_clock::now(), loglevel_name, msg);

    if(level == LogLevel::ERROR) {
        std::cerr << output << "\n";
    }
    std::cout << output << "\n";
}
