83 lines
2.5 KiB
C++
83 lines
2.5 KiB
C++
#include "SpdLogger.h"
|
|
|
|
#include <QApplication>
|
|
|
|
#include "spdlog/async.h"
|
|
#include "spdlog/sinks/rotating_file_sink.h"
|
|
#include "spdlog/sinks/stdout_sinks.h"
|
|
#include "spdlog/sinks/stdout_color_sinks.h"
|
|
|
|
const char* LoggerName = "spd_logger";
|
|
|
|
static SpdLogger* SpdLoggerIns_{ nullptr };
|
|
static void LogMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
|
|
if (nullptr == SpdLoggerIns_) {
|
|
return;
|
|
}
|
|
|
|
switch (type) {
|
|
case QtDebugMsg:
|
|
LOG_DEBUG(msg.toStdString());
|
|
break;
|
|
case QtInfoMsg:
|
|
LOG_INFO(msg.toStdString());
|
|
break;
|
|
case QtWarningMsg:
|
|
LOG_WARN(msg.toStdString());
|
|
break;
|
|
case QtCriticalMsg:
|
|
LOG_CRITI(msg.toStdString());
|
|
break;
|
|
case QtFatalMsg:
|
|
LOG_ERROR(msg.toStdString());
|
|
break;
|
|
}
|
|
}
|
|
|
|
SpdLogger::SpdLogger(const std::string& filename, int flush) {
|
|
constexpr std::size_t max_size = 1024 * 1024 * 10;
|
|
constexpr std::size_t max_files = 5;
|
|
//auto tp = std::make_shared<spdlog::details::thread_pool>(1, 1);
|
|
//std::shared_ptr<spdlog::logger> logHander = spdlog::rotating_logger_mt<spdlog::async_factory>(LoggerName, filename, max_size, max_files);
|
|
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(filename, max_size, max_files);
|
|
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
|
|
|
std::string pFormat("[%Y-%m-%d %H:%M:%S.%e][%t][%^%l%$][%@]%v");
|
|
file_sink->set_pattern(pFormat);
|
|
console_sink->set_pattern(pFormat);
|
|
std::vector<spdlog::sink_ptr> sinks;
|
|
sinks.push_back(console_sink);
|
|
sinks.push_back(file_sink);
|
|
std::shared_ptr<spdlog::logger> logHander = std::make_shared<spdlog::logger>(LoggerName, begin(sinks), end(sinks));
|
|
|
|
#if NDEBUG
|
|
logHander->set_level(spdlog::level::debug);
|
|
|
|
#else
|
|
logHander->set_level(spdlog::level::debug);
|
|
#endif
|
|
|
|
if (flush > 0) {
|
|
spdlog::flush_every(std::chrono::seconds(flush));
|
|
}
|
|
|
|
logHander->flush_on(spdlog::level::warn);
|
|
spdlog::initialize_logger(std::move(logHander));
|
|
std::shared_ptr< spdlog::logger>ll = spdlog::get(LoggerName);
|
|
|
|
assert(nullptr == SpdLoggerIns_);
|
|
SpdLoggerIns_ = this;
|
|
LOG_INFO("----------------------MAIN----------------------------------------");
|
|
qInstallMessageHandler(LogMessageOutput);
|
|
}
|
|
|
|
SpdLogger::~SpdLogger() {
|
|
assert(nullptr != SpdLoggerIns_);
|
|
SpdLoggerIns_ = nullptr;
|
|
|
|
LOG_INFO("----------------------FINISH----------------------------------------");
|
|
|
|
spdlog::drop_all();
|
|
spdlog::shutdown();
|
|
}
|