#include "logger.h"
#include <spdlog/async.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include "spdlog/sinks/stdout_color_sinks.h"

#define MAX_ROTATING_FILE_SIZE  1048576 * 5
#define MAX_ROTATING_FILES      3
    
Logger::Logger()
{

}

Logger::~Logger()
{

}

void Logger::setup(LOG_LEVEL level)
{
#if 0
    auto rotating_logger = spdlog::rotating_logger_mt<spdlog::async_factory>("file", "logs/log.txt", MAX_ROTATING_FILE_SIZE, MAX_ROTATING_FILES);
    auto console_logger = spdlog::stdout_color_mt<spdlog::async_factory>("out");    
    auto err_logger = spdlog::stderr_color_mt<spdlog::async_factory>("err");
#endif

    spdlog::init_thread_pool(8192, 1);

    auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("logs/log.txt", MAX_ROTATING_FILE_SIZE, MAX_ROTATING_FILES);
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    // auto err_sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();

    rotating_sink->set_level(spdlog::level::trace);
    console_sink->set_level(spdlog::level::debug);
    // console_sink配置错误接收器只接收错误级别以上的日志
    // err_sink->set_level(spdlog::level::err);

    // auto logger = std::make_shared<spdlog::logger>("multi_sink", spdlog::sinks_init_list{rotating_sink, console_sink, err_sink});

    // asynchronous logger with multi sinks
    std::vector<spdlog::sink_ptr> sinks {rotating_sink, console_sink};
    auto logger = std::make_shared<spdlog::async_logger>("multi_sink", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
    
#ifdef NDEBUG
    logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
#else
    logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] Thread ID: %t (%s:%#) [%^%l%$] %v");
#endif
    logger->set_level(static_cast<spdlog::level::level_enum>(level)); // 允许所有日志通过 logger 层
    spdlog::register_logger(logger);
    spdlog::set_default_logger(logger);
}