1.下载
git clone https://github.com/gabime/spdlog.git
2.编译
cd spdlog
mkdir build
cd build
cmake ..
make -j
3.安装
sudo make install
sudo apt-get install libspdlog-dev
4.运行
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/async.h>
int main(){
spdlog::info("hello world");
auto logger1 = spdlog::basic_logger_mt("basic", "basic.txt",false);
logger1->info("hello world from basic_sink");
auto logger2 = spdlog::basic_logger_mt<spdlog::async_factory>("async_basic", "basic.txt");
logger2->info("hello world async from basic_sink");
auto sink1 = std::make_shared<spdlog::sinks::ansicolor_sdout_sink_mt>("async_basic.txt", true);
auto sink2 = std::make_shared<spdlog::sinks::basic_file_sink_mt>("basic.txt", true);
sink1->set_pattern("[%^%L%$] %v")
auto logger3 = std::make_shared<spdlog::logger>("basic", spdlog::sinks_init_list{[0]=sink1, [1]=sink2});
auto logger4 = std::make_shared<spdlog::async_logger>("tp", spdlog::sinks_init_list{sink1,sink2}, spdlog::thread_pool());
//logger3->sinks().push_back(sink1);
//logger3->sinks().push_back(sink2);
spdlog::register_logger(logger3);
logger3->info("multy sinks");
logger4->info("logger4")
spdlog::flush_every(std::chrono::seconds(5));
return 0;
}
第一行是在命令行输出。
第二个是输出到文件,参数分别为logger名,文件名,是否为清空数据,false为追加数据
第三个是异步输出
剩下的是对上面步骤的拆分,可自定义。sink为输出,logger连接registry和sink。两种绑定方法都可以。
flush表示多久把用户缓冲区刷新到内核缓冲区。sink_it表示多久把数据刷新到用户缓冲区
pattern格式化