spdlog wiki:https://github.com/gabime/spdlog/wiki
别人的学习笔记:https://www.cnblogs.com/oucsheep/p/8426548.html
别人的学习笔记:https://github.com/gabime/spdlog/wiki
百度搜 spdlog 封装可以看到很多写的差不多的单例类,我看公司遗留的代码也是借鉴这些写的。
最常见的是一开头就写上:
#ifdef _WIN32
#define __FILENAME__ (strrchr(__FILE__, '\\') ? (strrchr(__FILE__, '\\') + 1):__FILE__)
#else
#define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1):__FILE__)
#endif
//定义一个在日志后添加 文件名 函数名 行号 的宏定义
#ifndef suffix
#define suffix(msg) std::string(msg).append(" <")\
.append(__FILENAME__).append("> <").append(__func__)\
.append("> <").append(std::to_string(__LINE__))\
.append(">").c_str()
//#define suffix(msg) std::string().append(" File:")\
// .append(__FILENAME__).append("\", Func:\"").append(__func__)\
// .append("()\", Line:\"").append(std::to_string(__LINE__)).append("\"").append(msg).c_str()
#endif
这种文件行号之类的 spdlog 本身也自带,可以参见 SPDLOG_LOGGER_CALL 宏定义,用这个还得加上宏:
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
不过它自带的函数用的 __FUNCTION__ 在 VS 下会显示类名,我自定义改成了 __func__ 只保留了函数名。
代码如下:
#pragma once
//定义宏使输出文件名和行号
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
#define SPDLOG_TRACE_ON
//#define SPDLOG_DEBUG_ON
#include "spdlog/spdlog.h"
#include "spdlog/sinks/easy_file_sink.h"
//#include "spdlog/sinks/daily_file_sink.h"
//spdlog 简易封装
//龚建波 2021-1-27 修改
//''' 示例
// Qt使用时 CONFIG += utf8_source
// INITLOG(QString("日志.log").toLocal8Bit().toStdString());
// ERRORLOG("测试下 {0} {1} {2} {3}",123,true,"测试下",QString("测试下").toStdString());
// WARNLOG("测试下 {0} {1} {2} {3}",123,true,"测试下",QString("测试下").toStdString());
// INFOLOG("测试下 {0} {1} {2} {3}",123,true,"测试下",QString("测试下").toStdString());
// DEBUGLOG("测试下 {0} {1} {2} {3}",123,true,"测试下",QString("测试下").toStdString());
// TRACELOG("测试下 {0} {1} {2} {3}",123,true,"测试下",QString("测试下").toStdString());
//'''
//spdlog讲解参见博客 https://www.cnblogs.com/oucsheep/p/8426548.html
class BaseLog
{
private:
BaseLog()=default;
public:
static BaseLog* getInstance() {
static BaseLog instance;
return &instance;
}
//初始化日志,路径使用locale编码
//如: QString("日志.log").toLocal8Bit().toStdString()
void init(const std::string& path){
//自定义的sink
logPtr = spdlog::easy_logger_mt("easy",path,1024*1024*25);
//可以配置多个sink
//std::make_shared<spdlog::logger>
//spdlog::register_logger(logPtr); 配合 spdlog::drop_all();
//设置日志记录级别
logPtr->set_level(spdlog::level::trace);
//设置格式
//参见文档 https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
//[%Y-%m-%d %H:%M:%S.%e] 时间
//[%l] 日志级别
//[%t] 线程
//[%s] 文件
//[%#] 行号
//[%!] 函数
//[%v] 实际文本
logPtr->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [%t] [%s %!:%#] %v");
//设置当出发 err 或更严重的错误时立刻刷新日志到 disk
logPtr->flush_on(spdlog::level::trace);
//spdlog::flush_every(std::chrono::seconds(3));
}
auto logger() {
return logPtr;
}
private:
std::shared_ptr<spdlog::logger> logPtr;
};
#define INITLOG(path) BaseLog::getInstance()->init(path)
//参见SPDLOG_LOGGER_CALL
#define SPDLOG_BASE(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, __func__}, level, __VA_ARGS__)
#define TRACELOG(...) SPDLOG_BASE(BaseLog::getInstance()->logger(), spdlog::level::trace, __VA_ARGS__)
#define DEBUGLOG(...) SPDLOG_BASE(BaseLog::getInstance()->logger(), spdlog::level::debug, __VA_ARGS__)
#define INFOLOG(...) SPDLOG_BASE(BaseLog::getInstance()->logger(), spdlog::level::info, __VA_ARGS__)
#define WARNLOG(...) SPDLOG_BASE(BaseLog::getInstance()->logger(), spdlog::level::warn, __VA_ARGS__)
#define ERRORLOG(...) SPDLOG_BASE(BaseLog::getInstance()->logger(), spdlog::level::err, __VA_ARGS__)
#define CRITICALLOG(...) SPDLOG_BASE(BaseLog::getInstance()->logger(), spdlog::level::critical, __VA_ARGS__)
再贴一个百度出来的:https://blog.csdn.net/et_endeavoring/article/details/86109121
#pragma once
#include "Util.h"
#ifdef _WIN32
#define __FILENAME__ (strrchr(__FILE__, '\\') ? (strrchr(__FILE__, '\\') + 1):__FILE__)
#else
#define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1):__FILE__)
#endif
//定义一个在日志后添加 文件名 函数名 行号 的宏定义
#ifndef suffix
#define suffix(msg) std::string(msg).append(" <")\
.append(__FILENAME__).append("> <").append(__func__)\
.append("> <").append(std::to_string(__LINE__))\
.append(">").c_str()
//#define suffix(msg) std::string().append(" File:")\
// .append(__FILENAME__).append("\", Func:\"").append(__func__)\
// .append("()\", Line:\"").append(std::to_string(__LINE__)).append("\"").append(msg).c_str()
#endif
//在 spdlog.h 之前定义,才有效
#ifndef SPDLOG_TRACE_ON
#define SPDLOG_TRACE_ON
#endif
#ifndef SPDLOG_DEBUG_ON
#define SPDLOG_DEBUG_ON
#endif
#include <spdlog\spdlog.h>
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/basic_file_sink.h"
#include <spdlog\sinks\daily_file_sink.h>
class Logger
{
public:
static Logger& GetInstance() {
static Logger m_instance;
return m_instance;
}
auto GetLogger() { return nml_logger; }
private:
Logger() {
util::MakeSureDirExist("logs");
//设置为异步日志
//spdlog::set_async_mode(32768); // 必须为 2 的幂
std::vector<spdlog::sink_ptr> sinkList;
//#ifdef _CONSOLE
#ifdef _DEBUG
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
consoleSink->set_level(spdlog::level::debug);
//consoleSink->set_pattern("[multi_sink_example] [%^%l%$] %v");
consoleSink->set_pattern("[%m-%d %H:%M:%S.%e][%^%L%$] %v");
sinkList.push_back(consoleSink);
#endif
auto basicSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/basicSink.txt");
basicSink->set_level(spdlog::level::debug);
basicSink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%5l%$] %v");
sinkList.push_back(basicSink);
nml_logger = std::make_shared<spdlog::logger>("both", begin(sinkList), end(sinkList));
//register it if you need to access it globally
spdlog::register_logger(nml_logger);
// 设置日志记录级别
#ifdef _DEBUG
nml_logger->set_level(spdlog::level::trace);
#else
nml_logger->set_level(spdlog::level::err);
#endif
//设置 logger 格式[%^%L%$]
//nml_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%5l] %v");
//nml_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%5l%$] %v");
//设置当出发 err 或更严重的错误时立刻刷新日志到 disk
nml_logger->flush_on(spdlog::level::err);
spdlog::flush_every(std::chrono::seconds(3));
}
~Logger() {
spdlog::drop_all();
}
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
private:
std::shared_ptr<spdlog::logger> nml_logger;
};
//Logger& operator<<(Logger& log, const char* s);
//Logger& operator<<(Logger& log, const std::string& s);
//#define DEBUG Logger::GetInstance()
#define LTrace(msg,...) Logger::GetInstance().GetLogger()->trace(suffix(msg),__VA_ARGS__)
//#define LDebug(msg,...) Logger::GetInstance().GetLogger()->debug(suffix(msg),__VA_ARGS__)
#define LDebug(...) Logger::GetInstance().GetLogger()->debug(__VA_ARGS__)
#define LInfo(...) Logger::GetInstance().GetLogger()->info(__VA_ARGS__)
#define LWarn(...) Logger::GetInstance().GetLogger()->warn(__VA_ARGS__)
#define LError(...) Logger::GetInstance().GetLogger()->error(__VA_ARGS__)
#define LCritical(...) Logger::GetInstance().GetLogger()->critical(__VA_ARGS__)
#define criticalif(b, ...) \
do { \
if ((b)) { \
Logger::GetInstance().GetLogger()->critical(__VA_ARGS__); \
} \
} while (0)
#ifdef WIN32
#define errcode WSAGetLastError()
#endif
最新评论