2024-03-04 16:22:40 +08:00
|
|
|
#include "Log.h"
|
|
|
|
#include "Constant.h"
|
|
|
|
#include "spdlog/sinks/daily_file_sink.h"
|
|
|
|
#include "spdlog/sinks/stdout_sinks.h"
|
|
|
|
#include "spdlog/spdlog.h"
|
2024-08-12 11:26:42 +08:00
|
|
|
#include <QDebug>
|
2024-03-04 16:22:40 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
|
|
|
|
|
|
|
|
std::shared_ptr<spdlog::logger> Log::logger = nullptr;
|
|
|
|
bool Log::isInit = false;
|
|
|
|
|
|
|
|
Log::Log()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief 初始化logger
|
|
|
|
*/
|
|
|
|
void Log::init()
|
|
|
|
{
|
|
|
|
std::vector<spdlog::sink_ptr> sinks;
|
|
|
|
// 用于将日志输出到控制台
|
|
|
|
auto console_sink = std::make_shared<spdlog::sinks::stdout_sink_mt>();
|
|
|
|
// 用于将日志按日期保存到文件中
|
|
|
|
auto daily_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>(Constant::LogPath, 0, 1, false, 50);
|
|
|
|
sinks.push_back(console_sink);
|
|
|
|
sinks.push_back(daily_sink);
|
|
|
|
|
|
|
|
logger = std::make_shared<spdlog::logger>("daily_logger", sinks.begin(), sinks.end());
|
|
|
|
logger->set_pattern("[%Y/%m/%d %H:%M:%S] [%l] [thread:%t] [%s:%#] %v");
|
|
|
|
logger->flush_on(spdlog::level::info);
|
|
|
|
|
|
|
|
spdlog::register_logger(logger);
|
|
|
|
isInit = true;
|
|
|
|
}
|