一、 下载spdlog
https://codeload.github.com/gabime/spdlog/zip/refs/heads/v1.x
spdlog链接
二、配置工程编译,和eigen库类似spdlog无需单独编译
拷贝到工程目录下
配置目录
稍微封装一下符合qDebug() 使用习惯
/*
** File name: LogSystem.h
** Author:
** Date: 2024-11-4
** Brief: 日志系统接口
** Copyright (C) 1392019713@qq.com All rights reserved.
*/#pragma once#include "LogStream.h"
#include "../../System/Include/Singleton.h"
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include "LogSystemExportLib.h"class LOG_SYSTEM_EXPORT CLogSystem
{DECLARE_UNMANGED_SINGLETON_X(CLogSystem)
public:virtual ~CLogSystem() = default;void SetFileInfo(const std::string& strfileName = "./LogSystem.txt", const std::string& strName = "LogSystem", int nDivideSize = 1024 * 1024 * 10);CLogSystem(const CLogSystem&) = delete;CLogSystem &operator=(const CLogSystem&) = delete;private:CLogSystem();private:std::shared_ptr<spdlog::sinks::rotating_file_sink_mt> m_pFile_sink;std::shared_ptr<spdlog::sinks::stdout_color_sink_mt> m_pConsole_sink;public:std::shared_ptr<spdlog::logger> m_pLogger;
};#define LOG(level) \CLogStream(level)#define LogInfo() LOG(spdlog::level::info)
#define LogWarn() LOG(spdlog::level::warn)
#define LogError() LOG(spdlog::level::err)
#define LogDebug() LOG(spdlog::level::debug)
#include "../Include/LogSystem.h"
#include <iostream>
#include <chrono>IMPLEMENT_UNMANED_SINGLETON_X(CLogSystem)CLogSystem::CLogSystem()
{m_pLogger = nullptr;m_pConsole_sink = nullptr;m_pFile_sink = nullptr;
}void CLogSystem::SetFileInfo(const std::string& strfileName, const std::string& strName, int nDivideSize)
{try{m_pConsole_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();m_pConsole_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%l%$] %v");m_pConsole_sink->set_level(spdlog::level::trace);m_pFile_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(strfileName, nDivideSize, 10);m_pFile_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v");m_pFile_sink->set_level(spdlog::level::trace);std::vector<spdlog::sink_ptr> vecSinks{ m_pConsole_sink, m_pFile_sink };m_pLogger = std::make_shared<spdlog::logger>(strName, begin(vecSinks), end(vecSinks));m_pLogger->set_pattern("%^[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v%$");m_pLogger->set_level(spdlog::level::trace);spdlog::flush_every(std::chrono::seconds(5));spdlog::set_default_logger(m_pLogger);}catch (const spdlog::spdlog_ex& ex){std::cout << "日志系统初始化失败:" << ex.what() << std::endl;}
}
/*
** File name: LogStream.h
** Author:
** Date: 2024-11-4
** Brief: 日志流类
** Copyright (C) 1392019713@qq.com All rights reserved.
*/#pragma once#include "LogSystemExportLib.h"
#include "LogSystem.h"
#include <stdint.h>
#include <string>class LOG_SYSTEM_EXPORT CLogStream
{
public:CLogStream(int nLevel);CLogStream& operator <<(bool bArg);CLogStream& operator <<(char cArg);CLogStream& operator <<(int16_t n16Arg);CLogStream& operator <<(uint16_t u16Arg);CLogStream& operator <<(int32_t n32Arg);CLogStream& operator <<(uint32_t u32Arg);CLogStream& operator <<(int64_t n64Arg);CLogStream& operator <<(uint64_t u64Arg);CLogStream& operator <<(double dArg);CLogStream& operator <<(const char* szArg);CLogStream& operator <<(const std::string& strArg);~CLogStream();private:std::string m_strMsg;int m_nLevel;
};#include "../Include/LogStream.h"CLogStream::CLogStream(int nLevel)
{m_nLevel = nLevel;
}CLogStream::~CLogStream()
{switch (m_nLevel) {case spdlog::level::info: {CLogSystem::Instance().m_pLogger->info(m_strMsg);}break;case spdlog::level::warn: {CLogSystem::Instance().m_pLogger->warn(m_strMsg);}break;case spdlog::level::err: {CLogSystem::Instance().m_pLogger->error(m_strMsg);}break;case spdlog::level::critical: {CLogSystem::Instance().m_pLogger->critical(m_strMsg);}break;case spdlog::level::debug: {CLogSystem::Instance().m_pLogger->debug(m_strMsg);}break;default:break;}
}CLogStream& CLogStream::operator<<(bool bArg)
{if (bArg){m_strMsg += " true";}else{m_strMsg += " false";}return *this;
}CLogStream& CLogStream::operator<<(char cArg)
{m_strMsg += cArg;return *this;
}CLogStream& CLogStream::operator<<(int16_t n16Arg)
{m_strMsg += std::to_string(n16Arg);return *this;
}CLogStream& CLogStream::operator<<(uint16_t u16Arg)
{m_strMsg += std::to_string(u16Arg);return *this;
}CLogStream& CLogStream::operator<<(int32_t n32Arg)
{m_strMsg += std::to_string(n32Arg);return *this;
}CLogStream& CLogStream::operator<<(uint32_t u32Arg)
{m_strMsg += std::to_string(u32Arg);return *this;
}CLogStream& CLogStream::operator<<(int64_t n64Arg)
{m_strMsg += std::to_string(n64Arg);return *this;
}CLogStream& CLogStream::operator<<(uint64_t u64Arg)
{m_strMsg += std::to_string(u64Arg);return *this;
}CLogStream& CLogStream::operator<<(double dArg)
{m_strMsg += std::to_string(dArg);return *this;
}CLogStream& CLogStream::operator<<(const char* szArg)
{m_strMsg += szArg;return *this;
}CLogStream& CLogStream::operator<<(const std::string& strArg)
{m_strMsg += strArg;return *this;
}
使用示例代码
std::string strPath = TransUnicode2String(QCoreApplication::applicationDirPath()) + "/HLACLog.txt";CLogSystem::Instance().SetFileInfo(strPath, "HLAC");LogDebug() << "HLAC启动";LogInfo() << "HLAC启动";LogWarn() << "HLAC启动";LogError() << "HLAC启动";
控制台打印
文件打印
如有问题可以留言