#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include "sys_util.h"
#include "daily_and_size_sink.h"
#include <spdlog/pattern_formatter.h>
#include "color_format.h"

#define CONSOLE_PATTERN ANSI_MAGENTA"%Y-%m-%d %H:%M:%S.%e" ANSI_NONE" %l" ANSI_BLUE" %=6t %s:%#" ANSI_BLUE" [%!]" ANSI_NONE" ===> %v"
#define FILE_PATTERN "%Y-%m-%d %H:%M:%S.%e %l %=6t %s:%# [%!] ===> %v"

namespace zsy
{
    std::shared_ptr<spdlog::logger> getLogger()
    {
        static std::shared_ptr<spdlog::logger> logger = []
        {
            // const auto file_sink = std::make_shared<spdlog::sinks::daily_file_format_sink_mt>(SysUtil::WORK_DIR + "\\logs\\deviceAccessLayer.log", 0, 0);
            auto file_sink = std::make_shared<DailyAndSizeSink>(
                SysUtil::WORK_DIR + "\\logs\\deviceAccessLayer.log",
                50 * 1024 * 1024,
                15
            );
            constexpr auto level = static_cast<spdlog::level::level_enum>(SPDLOG_ACTIVE_LEVEL);

            auto file_formatter = std::make_unique<spdlog::pattern_formatter>();
            file_formatter->add_flag<LogLevelFormat>('l', false)
                    .set_pattern(FILE_PATTERN);
            file_sink->set_formatter(std::move(file_formatter));

            auto console_formatter = std::make_unique<spdlog::pattern_formatter>();
            console_formatter->add_flag<LogLevelFormat>('l')
                    .add_flag<LogMsgFormat>('v')
                    .set_pattern(CONSOLE_PATTERN);
            const auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
            console_sink->set_formatter(std::move(console_formatter));

            spdlog::sinks_init_list sinks = {console_sink, file_sink};
            auto logger_ = std::make_shared<spdlog::logger>("zsy", sinks);
            logger_->set_level(level);
            logger_->flush_on(level);
            spdlog::set_default_logger(logger_);
            return logger_;
        }();
        return logger;
    }
}
