spdlog的簡單封裝和使用

頭文件:app

#ifndef _SIMLOG_H_
#define _SIMLOG_H_

#include <QtWidgets/QWidget>
#include <spdlog/spdlog.h>
#include "simlog_export.h"

#ifdef _WIN32
//strrchr:查找字符在指定字符串從右面開始的第一次出現的位置,若是成功,返回該字符以及後面的字符,若是失敗,返回NULL
//strcgr:查找字符在指定字符串首次出現的位置
#define __FILENAME__ (strrchr(__FILE__,'\\')?(strrchr(__FILE__,'\\')+1):__FILE__)
#else
#define __FILENAME__ (strrchr(__FILE__,'/')?(strrchr(__FILE__,'/')+1):__FILE__)
#endif //_WIN32

#ifndef SUFFIX
//在錯誤級別的日誌後面追加文件名,函數名,行號
#define SUFFIX(msg) std::string(msg).append("  <")\
                    .append(__FILENAME__).append("> <").append(__FUNCTION__)\
                    .append("> <").append(std::to_string(__LINE__))\
                    .append(">").c_str()
#endif //suffix

/*
日誌等級:trace,debug,info,warn,err ,critical
使用方法:包含simlog.h頭文件,調用初始化函數,使用LDebug等打印日誌信息
例:
SimLog::Instance().InitSimLog("scenario_edit", "scenario_edit_log.txt");
int i = 10;
double d_number = 10.01;
LDebug("SimLog::Async message");
LDebug("SimLog::Async message #{0},d_number:{1}", i,d_number);
注:使用{}格式化字符串,裏面的數字爲佔位符
*/

#define LTrace(msg,...)  SimLog::Instance().GetLogger()->trace(SUFFIX(msg),__VA_ARGS__)
#define LDebug(...)  SimLog::Instance().GetLogger()->debug(__VA_ARGS__)
#define LInfo(...)  SimLog::Instance().GetLogger()->info(__VA_ARGS__)
#define LWarn(...) SimLog::Instance().GetLogger()->warn(__VA_ARGS__)
#define LError(msg,...)  SimLog::Instance().GetLogger()->error(SUFFIX(msg),__VA_ARGS__)
#define LCritical(...)  SimLog::Instance().GetLogger()->critical(__VA_ARGS__)

class SIMLOG_EXPORT SimLog
{
public:
    static SimLog&Instance();

    void InitSimLog(std::string logger_name,std::string file_name, int log_level= spdlog::level::trace);

    void EndLog();

    void SetLevel(int level = spdlog::level::trace);

    auto GetLogger() 
    {
        return my_logger_;
    }

private:
    //私有構造函數,拷貝構造函數和拷貝賦值函數,禁止在類外聲明實例
    SimLog();
    ~SimLog();
    SimLog(const SimLog &other) = delete;
    SimLog& operator=(const SimLog &other) = delete;

private:
    std::shared_ptr<spdlog::logger> my_logger_;
};

#endif //_SIMLOG_H_

源文件:異步

#include "simlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/async.h"

#include <QDateTime>
#include <QDebug>
#include <QString>


SimLog& SimLog::Instance()
{
    static SimLog log;
    return log;
}

void SimLog::InitSimLog(std::string logger_name, std::string file_name,int log_level)
{
    //設置日誌等級
    spdlog::set_level(static_cast<spdlog::level::level_enum>(log_level));
    //設置日誌爲異步日誌,不帶滾動,日誌文件會一直寫入
    my_logger_ = spdlog::basic_logger_mt<spdlog::async_factory >(logger_name, file_name);
    //當遇到錯誤級別以上的馬上刷新到日誌
    my_logger_->flush_on(spdlog::level::err);
    //每三秒刷新一次
    spdlog::flush_every(std::chrono::seconds(3));

    //測試
    for (int i = 0; i < 101; i++)
    {
        my_logger_->info("SimLog::Async message #{}", i);
    }
}

void SimLog::EndLog()
{
    spdlog::shutdown();
}

SimLog::SimLog()
{

}

SimLog::~SimLog()
{
    EndLog();
}


void SimLog::SetLevel(int level)
{
    spdlog::set_level(static_cast<spdlog::level::level_enum>(level));
}

使用方法:async

(1)包含頭文件函數

(2)調用初始化函數測試

(3)使用使用LDebug等打印日誌信息spa

例:
SimLog::Instance().InitSimLog("test", "log.txt");
int i = 10;
double d_number = 10.01;
LDebug("SimLog::Async message");
LDebug("SimLog::Async message #{0},d_number:{1}", i,d_number);
注:使用{}格式化字符串,裏面的數字爲佔位符debug

相關文章
相關標籤/搜索