c++日志工具spdlog
spdlog日志是纯头文件,使用起来比较方便。使用时只需要简单的初始化即可,这里对其初始化做了一个简单的封装,这样使用起来更加方便。
封装类代码
头文件
cspdlog.h
#ifndef _CSPDLOG_H_
#define _CSPDLOG_H_#include "spdlog/spdlog.h"
#include "spdlog/fmt/bin_to_hex.h"
#include
#include class CSpdlog
{
protected:CSpdlog();~CSpdlog();static CSpdlog *m_instance;public:static CSpdlog *GetInstance();void Init(const std::string & name,const std::string &logPath, std::size_t max_size&#61;1048576, std::size_t max_file &#61; 2);void SetConsoleLogLevel(spdlog::level::level_enum log_level);void SetFileLogLevel(spdlog::level::level_enum log_level);private:std::vector<spdlog::sink_ptr> m_sinks;std::shared_ptr<spdlog::logger> m_logger;
};#endif
源文件
cspdlog.cpp
#include "cspdlog.h"#include
#include
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"CSpdlog::CSpdlog()
{}CSpdlog::~CSpdlog()
{}void CSpdlog::Init(const std::string & name, const std::string &log_path, std::size_t max_size, std::size_t max_file )
{try { auto console_sink &#61; std::make_shared<spdlog::sinks::stdout_color_sink_mt>();console_sink->set_level(spdlog::level::debug);console_sink->set_pattern("%^[%Y-%m-%d %H:%M:%S:%e] [%n] [tid: %t] [%l] %v%$");std::string logFile &#61; log_path &#43; "/" &#43; name &#43; ".txt";auto file_sink &#61; std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logFile, max_size, max_file);file_sink->set_pattern("[%Y-%m-%d %H:%M:%S:%e] [%n] [tid: %t] [%l] %v");file_sink->set_level(spdlog::level::warn);m_sinks.push_back(console_sink);m_sinks.push_back(file_sink);m_logger &#61; std::make_shared<spdlog::logger>(name, begin( m_sinks ), end( m_sinks ));spdlog::register_logger(m_logger);m_logger->flush_on(spdlog::level::level_enum::warn); }catch (const spdlog::spdlog_ex &ex){std::cout<<"Log initialization faild"<<ex.what()<<std::endl;}
}void CSpdlog::SetConsoleLogLevel(spdlog::level::level_enum log_level)
{m_logger->set_level(log_level);
}void CSpdlog::SetFileLogLevel(spdlog::level::level_enum log_level)
{m_sinks[1]->set_level(log_level);
}CSpdlog* CSpdlog::m_instance &#61; NULL;CSpdlog* CSpdlog::GetInstance()
{if ( m_instance &#61;&#61; NULL ){m_instance &#61; new CSpdlog;}return m_instance;
}
测试主程序
#include
#include
#include
#include using namespace std;int main()
{CSpdlog::GetInstance()->Init("test","./log"); CSpdlog::GetInstance()->SetConsoleLogLevel(spdlog::level::debug); CSpdlog::GetInstance()->SetFileLogLevel(spdlog::level::warn); auto logger &#61; spdlog::get("test"); logger->warn("test start.");int counter &#61; 0; while(1){logger->debug("debug msg, counter: {}",counter);logger->info("info msg, counter: {}",counter);logger->warn("warn msg, counter: {}",counter);logger->error("error msg, counter: {}",counter);logger->critical("critical msg, counter: {}",counter);logger->trace("trace msg, counter: {}",counter);usleep(500000);}return 0;
}
编译运行
我的通用Makefile
DEBUG_DIR&#61;.
SRC_DIR&#61;.
INC_DIR&#61;. ./spdlogSRC&#61;$(wildcard $(SRC_DIR)/*.cpp )
OBJS&#61;$(patsubst $(SRC_DIR)/%.cpp,$(DEBUG_DIR)/%.o,$(SRC))
TARGET&#61;test
INSTALL_PATH ?&#61; .
ARCH ?&#61;
CC&#61;$(ARCH)gcc
CPP&#61;$(ARCH)g&#43;&#43;
AR&#61;$(ARCH)ar
STRIP&#61;$(ARCH)stripCFLAGS &#43;&#61; -Wall -std&#61;c&#43;&#43;11
LDFLAGS &#43;&#61; -lpthreadCFLAGS &#43;&#61; $(foreach dir,$(INC_DIR),-I$(dir))all:$(TARGET)
$(TARGET): $(OBJS)$(CPP) $(OBJS) -o $&#64; $(CFLAGS) $(LDFLAGS)$(STRIP) $(TARGET)
$(DEBUG_DIR)/%.o: $(SRC_DIR)/%.cpp$(CPP) $(CFLAGS) -c $< -o $&#64;
clean:-rm $(OBJS) $(LIB_TARGET)
编译
make
g&#43;&#43; -Wall -std&#61;c&#43;&#43;11 -I. -I./spdlog -c cspdlog.cpp -o cspdlog.o
g&#43;&#43; -Wall -std&#61;c&#43;&#43;11 -I. -I./spdlog -c main.cpp -o main.o
g&#43;&#43; ./cspdlog.o ./main.o -o test -Wall -std&#61;c&#43;&#43;11 -I. -I./spdlog
strip test
运行
console输出&#xff1a;
文件输出&#xff1a;
$cat log/test.txt
[2021-08-17 15:30:59:526] [test] [tid: 20418] [warning] test start.
[2021-08-17 15:30:59:527] [test] [tid: 20418] [warning] warn msg, counter: 0
[2021-08-17 15:30:59:528] [test] [tid: 20418] [error] error msg, counter: 0
[2021-08-17 15:30:59:529] [test] [tid: 20418] [critical] critical msg, counter: 0
[2021-08-17 15:31:00:031] [test] [tid: 20418] [warning] warn msg, counter: 0
[2021-08-17 15:31:00:032] [test] [tid: 20418] [error] error msg, counter: 0
[2021-08-17 15:31:00:032] [test] [tid: 20418] [critical] critical msg, counter: 0
[2021-08-17 15:31:00:533] [test] [tid: 20418] [warning] warn msg, counter: 0
[2021-08-17 15:31:00:533] [test] [tid: 20418] [error] error msg, counter: 0
[2021-08-17 15:31:00:534] [test] [tid: 20418] [critical] critical msg, counter: 0
测试源代码以上传码云
地址&#xff1a;
https://gitee.com/fensnote/demo_code/tree/master/cpp/spdlog
微信公众号
fensnote