|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
// 定义宏使输出文件名和行号
|
|
|
|
|
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
|
|
|
|
|
#define SPDLOG_TRACE_ON
|
|
|
|
|
//#define SPDLOG_DEBUG_ON
|
|
|
|
|
|
|
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
|
#include "spdlog/sinks/stdout_color_sinks.h"
|
|
|
|
|
#include "spdlog/sinks/msvc_sink.h"
|
|
|
|
|
#include "spdlog/sinks/rotating_file_sink.h"
|
|
|
|
|
#include "spdlog/sinks/daily_file_sink.h"
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
#include "logFormat.h"
|
|
|
|
|
|
|
|
|
|
class EasySpdLog
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static EasySpdLog* getInstance()
|
|
|
|
|
{
|
|
|
|
|
static EasySpdLog instance;
|
|
|
|
|
return &instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetLogger(const std::string& path)
|
|
|
|
|
{
|
|
|
|
|
std::vector<spdlog::sink_ptr> sink_list;
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
|
|
|
|
sink_list.push_back(console_sink);
|
|
|
|
|
|
|
|
|
|
auto msvc_sink = std::make_shared<spdlog::sinks::msvc_sink_mt>();
|
|
|
|
|
sink_list.push_back(msvc_sink);
|
|
|
|
|
#endif
|
|
|
|
|
// 修改为按日期滚动的 daily_file_sink
|
|
|
|
|
auto daily_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>(path, 0, 0); // 每天00:00滚动日志
|
|
|
|
|
sink_list.push_back(daily_sink);
|
|
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lck(mtx_);
|
|
|
|
|
|
|
|
|
|
logger_ = std::make_shared<spdlog::logger>("multi_sink", begin(sink_list), end(sink_list));
|
|
|
|
|
//spdlog::register_logger(logger_);
|
|
|
|
|
spdlog::set_default_logger(logger_);
|
|
|
|
|
|
|
|
|
|
// 设置日志记录级别
|
|
|
|
|
logger_->set_level(spdlog::level::trace);
|
|
|
|
|
|
|
|
|
|
// 设置格式
|
|
|
|
|
// [%Y-%m-%d %H:%M:%S.%e] 时间 [%l] 日志级别 [%t] 线程 [%s] 文件 [%#] 行号 [%!] 函数 [%v] 实际文本
|
|
|
|
|
logger_->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [%t] [%s %!:%#] %v");
|
|
|
|
|
|
|
|
|
|
// 设置当出发err或更严重的错误时立刻刷新日志到disk
|
|
|
|
|
logger_->flush_on(spdlog::level::info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UnsetLogger()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lck(mtx_);
|
|
|
|
|
spdlog::shutdown();
|
|
|
|
|
logger_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto logger()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lck(mtx_);
|
|
|
|
|
return logger_;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
EasySpdLog() = default;
|
|
|
|
|
~EasySpdLog() = default;
|
|
|
|
|
EasySpdLog(const EasySpdLog&) = delete;
|
|
|
|
|
EasySpdLog& operator=(const EasySpdLog&) = delete;
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<spdlog::logger> logger_;
|
|
|
|
|
std::mutex mtx_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define SetLogger(path) EasySpdLog::getInstance()->SetLogger(path)
|
|
|
|
|
#define UnsetLogger() EasySpdLog::getInstance()->UnsetLogger();
|
|
|
|
|
//参见SPDLOG_LOGGER_CALL
|
|
|
|
|
#define SPDLOG_BASE(logger, level, ...) logger ? (logger)->log(spdlog::source_loc{__FILE__, __LINE__, __func__}, level, __VA_ARGS__) : void()
|
|
|
|
|
#define LTrace(...) SPDLOG_BASE(EasySpdLog::getInstance()->logger(), spdlog::level::trace, __VA_ARGS__)
|
|
|
|
|
#define LDebug(...) SPDLOG_BASE(EasySpdLog::getInstance()->logger(), spdlog::level::debug, __VA_ARGS__)
|
|
|
|
|
#define LInfo(...) SPDLOG_BASE(EasySpdLog::getInstance()->logger(), spdlog::level::info, __VA_ARGS__)
|
|
|
|
|
#define LWarn(...) SPDLOG_BASE(EasySpdLog::getInstance()->logger(), spdlog::level::warn, __VA_ARGS__)
|
|
|
|
|
#define LError(...) SPDLOG_BASE(EasySpdLog::getInstance()->logger(), spdlog::level::err, __VA_ARGS__)
|
|
|
|
|
#define LCritical(...) SPDLOG_BASE(EasySpdLog::getInstance()->logger(), spdlog::level::critical, __VA_ARGS__)
|
|
|
|
|
|