Skip to content

封装spdlog库

spdlog库

  1. 网址:https://github.com/gabime/spdlog
  2. 特性:
    • 支持控制台或异步文件输出
    • 支持控制台彩色输出

封装

  • Logger.h

    cpp
    #pragma once
    #include "Framework/Utility/Singleton.h"
    #include <spdlog/spdlog.h>
    #include <filesystem>
    
    namespace fs = std::filesystem;
    
    class Logger : public Singleton<Logger>
    {
    public:
        Logger(token);
        ~Logger();
    
        std::shared_ptr<spdlog::logger> getLogger()
        {
            return mLogger;
        }
    
        void setLogLevel(std::string level);
    
    private:
        std::shared_ptr<spdlog::logger> mLogger;
        fs::path mLogDir = fs::current_path();
        std::string mLogNamePrefix = "log_";
        bool mConsole = true;
        std::string mLevel = "debug";
    };
    
    // 有代码行号
    // #define LOG_TRACE(...)      SPDLOG_LOGGER_CALL(Logger::instance().getLogger().get(), spdlog::level::trace, __VA_ARGS__)
    // #define LOG_DEBUG(...)      SPDLOG_LOGGER_CALL(Logger::instance().getLogger().get(), spdlog::level::debug, __VA_ARGS__)
    // #define LOG_INFO(...)       SPDLOG_LOGGER_CALL(Logger::instance().getLogger().get(), spdlog::level::info, __VA_ARGS__)
    // #define LOG_WARN(...)       SPDLOG_LOGGER_CALL(Logger::instance().getLogger().get(), spdlog::level::warn, __VA_ARGS__)
    // #define LOG_ERROR(...)      SPDLOG_LOGGER_CALL(Logger::instance().getLogger().get(), spdlog::level::err, __VA_ARGS__)
    // #define LOG_CRITICAL(...)   SPDLOG_LOGGER_CALL(Logger::instance().getLogger().get(), spdlog::level::critical, __VA_ARGS__)
    
    // 没有代码行号
    #define LOG_TRACE(...)       Logger::instance().getLogger().get()->trace(__VA_ARGS__)
    #define LOG_DEBUG(...)       Logger::instance().getLogger().get()->debug(__VA_ARGS__)
    #define LOG_INFO(...)        Logger::instance().getLogger().get()->info(__VA_ARGS__)
    #define LOG_WARN(...)        Logger::instance().getLogger().get()->warn(__VA_ARGS__)
    #define LOG_ERROR(...)       Logger::instance().getLogger().get()->error(__VA_ARGS__)
    #define LOG_CRITICAL(...)    Logger::instance().getLogger().get()->critical(__VA_ARGS__)
  • Logger.cpp

    cpp
    #include "Logger.h"
    #include <spdlog/async.h>
    #include <spdlog/sinks/rotating_file_sink.h>
    #include <spdlog/sinks/stdout_color_sinks.h>
    #include <iostream>
    
    Logger::Logger(token)
    {
        try {
            const std::string loggerName = mLogNamePrefix + std::string("test");
            if (mConsole) {
                mLogger = spdlog::stdout_color_mt(loggerName);
            } else {
                // multi part log files, with every part 50M, max 100 files
                mLogger = spdlog::create_async<spdlog::sinks::rotating_file_sink_mt>(
                    loggerName, mLogDir.append(loggerName + ".log").u8string(), 50 * 1024 * 1024, 100);
            }
    
            setLogLevel(mLevel);
    
            mLogger->set_pattern("[T:%-6t] %+");
        } catch (const spdlog::spdlog_ex& ex) {
            std::cout << "[Logger] Initialization failed: " << ex.what() << std::endl;
        }
    }
    
    Logger::~Logger()
    {
        spdlog::shutdown();
    }
    
    void Logger::setLogLevel(std::string level)
    {
        mLevel = level;
    
        if (mLevel == "trace") {
            mLogger->set_level(spdlog::level::trace);
            mLogger->flush_on(spdlog::level::trace);
        } else if (mLevel == "debug") {
            mLogger->set_level(spdlog::level::debug);
            mLogger->flush_on(spdlog::level::debug);
        } else if (mLevel == "info") {
            mLogger->set_level(spdlog::level::info);
            mLogger->flush_on(spdlog::level::info);
        } else if (mLevel == "warn") {
            mLogger->set_level(spdlog::level::warn);
            mLogger->flush_on(spdlog::level::warn);
        } else if (mLevel == "error") {
            mLogger->set_level(spdlog::level::err);
            mLogger->flush_on(spdlog::level::err);
        } else if (mLevel == "critical") {
            mLogger->set_level(spdlog::level::critical);
            mLogger->flush_on(spdlog::level::critical);
        }
    }

使用

cpp
#include "Logger.h"

LOG_INFO("Wlecome to use {0} and {1}", "Logger", "Singleton");