一、基本使用
0、描述
spd就是speed的縮寫,代表速度,主要是由c++11寫的。
支持異步回滾、單日記錄、格式化等等;
不需要鏈接庫或動態庫,直接源碼使用;
可創建多個不同的日志器;
1、在官網下載源碼
https://github.com/gabime/spdlog
2、解壓
include文件夾就是需要的源碼
3、將源碼放在工程里
4、例子:異步文件回滾日志
#include "spdlog/spdlog.h" #include "spdlog/async.h" #include "spdlog/sinks/rotating_file_sink.h" int main(int, char *[]) { //異步就一定需要線程或線程池+日志隊列,這里給一個線程,隊列10000個字節 spdlog::init_thread_pool(10000, 1);
//創建一個回滾日志,這里使用了工廠,類型是異步工廠,參數:日志名稱、路徑、文件大小、文件個數 auto file_logger = spdlog::rotating_logger_mt<spdlog::async_factory>("file_logger", "logs/basic-log.txt", 1024 * 1024 * 5, 10); int i = 0;
//需要輸出日志的等級 file_logger->set_level(spdlog::level::debug); while (i < 1000000) { file_logger->debug("Async message #{}", i); i++; }
//回收 spdlog::drop_all(); return 0; }
數字越小日志越新!
5、獲取某個日志器
spdlog::get("file_logger")->info("loggers can be retrieved from a global");
6、手動刷新
logger->flush()
PS:
1、參考:
官方wiki:https://github.com/gabime/spdlog/wiki/1.-QuickStart
https://www.cnblogs.com/fishily/p/14431499.html
https://blog.csdn.net/kai15058157346/article/details/113079579
二、編譯靜態庫
1、下載解壓源碼並在源碼根目錄下新建build文件夾
2、打開CMake Gui,並進行相關設置
注意:本人使用了VS2015編譯失敗,VS2017成功
3、生成解決方案
4、打開VS2017,打開sln
5、生成spdlog項目
注意可以選擇Debug和Release,各自生成,在build/Debug和build/Release下各自生成了lib靜態庫
6、新建lib文件夾,放入兩個lib
7、手動將源碼include文件夾拷貝出來,lib放在同一文件夾里,完成
PS:
1、格式設置
https://blog.csdn.net/shizheng163/article/details/79418190