spdlog 日志库配置
#pragma once
#include <spdlog/spdlog.h>
#include <spdlog/fmt/ostr.h>
void init_log(const char * logPath, spdlog::level::level_enum logLevel);
#define LOG_DEBUG SPDLOG_DEBUG
#define LOG_INFO SPDLOG_INFO
#define LOG_WARN SPDLOG_WARN
#define LOG_ERROR SPDLOG_ERROR
#define LOG_CRITICAL SPDLOG_CRITICAL
#include <signal.h>
#include <spdlog/sinks/basic_file_sink.h>
#ifdef __ANDROID__
#include <spdlog/sinks/android_sink.h>
#endif
#include "log.h"
#if _WIN32
#include <Windows.h>
#endif
static void signal_handler(int signal)
{
LOG_ERROR("recv signal {}", signal);
spdlog::dump_backtrace();
}
void init_log(const char *logPath, spdlog::level::level_enum logLevel)
{
#if _WIN32
AttachConsole(ATTACH_PARENT_PROCESS);
#endif
#ifdef __ANDROID__
try
{
std::shared_ptr<spdlog::logger> android_logger = spdlog::android_logger_mt("android-logger");
spdlog::set_default_logger(android_logger);
}
catch (const std::exception & e)
{
LOG_WARN("init android logger failure, {}", e.what());
}
#else
if (logPath && strcmp(logPath, "") != 0)
{
std::shared_ptr<spdlog::logger> logger = spdlog::basic_logger_mt("logger", logPath);
logger->enable_backtrace(20);
logger->set_pattern("[%Y-%m-%d %T.%e] [%l] [%t] [%s:%#] [%!] %v");
spdlog::set_default_logger(logger);
spdlog::flush_every(std::chrono::seconds(3));
}
#endif
spdlog::default_logger()->set_level(logLevel);
}