#include "net_pub.h"

std::shared_ptr<CLogger> CLogger::logger_ = nullptr;
Log_t CLogger::log_ = nullptr;

std::mutex CLogger::mutex_;

Log_t CLogger::getLogger()
{
    if (logger_ == nullptr) {
        mutex_.lock();
        if (logger_ == nullptr) {
            logger_ = std::make_shared<CLogger>();
            logger_->init();
        }
        mutex_.unlock();
    }
    return logger_->log_;
}

void CLogger::init()
{
    // 创建控制台接收器
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(spdlog::level::debug);
    console_sink->set_pattern("[console] [%^%l%$] %v");

    // 创建文件接收器
    auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
        "logs/nettrans.log", 1024 * 100, 5);
    file_sink->set_level(spdlog::level::debug);
    file_sink->set_pattern("[file] [%^%l%$] %v");

    // 创建多接收器日志器
    std::vector<spdlog::sink_ptr> sinks{console_sink, file_sink};
    log_ = std::make_shared<spdlog::logger>("multi_sink", sinks.begin(),
                                            sinks.end());
    spdlog::register_logger(log_);
    log_->set_level(spdlog::level::debug);
    log_->set_pattern("[%H:%M:%S %z] [%n] [%^%l%$] %v");
}
