mac OSX使用spdlog1.7
阿新 • • 發佈:2020-07-15
注意⚠️ 請選擇對c++11支援較為完善的編譯器,因為spdlog一直更新。
本文將介紹mac osX 10.14.6使用clang++編譯main.cpp中使用的spdlog程式碼.
1、準備
A、spdlog原始碼:https://github.com/gabime/spdlog
B、clang++, 沒安裝? 可以通過安裝Xcode新版 或者 參看:https://clang.llvm.org/cxx_status.html
2、解壓
將下載的spdlog原始碼, 解壓。
3、建立main.cpp檔案
A、開啟終端,我這裡演示來到桌面, 建立資料夾cmd_demo,並建立main.cpp。
B、使用VSCode開啟並鍵入程式碼, 見下圖。 程式碼如下。
main.cpp程式碼:
#include <iostream>
#include "spdlog/spdlog.h"
#include "spdlog/sinks/rotating_file_sink.h"
void rotating_example()
{
// Create a file rotating logger with 5mb size max and 3 rotated files
auto max_size = 1048576 * 5;
auto max_files = 3 ;
auto logger = spdlog::rotating_logger_mt("log_file", "logs/rotating.txt", max_size, max_files);
for (int index = 0; index < 10; index++)
{
logger->info("index = {}", index);
spdlog::info("index = {}", index);
}
}
int main(int argc, char *argv[])
{
rotating_example();
spdlog::info( "hello world!");
return 0;
}
C、使用clang++ 編譯輸出
回到spdlog解壓目錄,將spdlog原始碼目錄下的 inlcude 目錄拷貝到建立的cmd_demo資料夾下 。 目錄結構如下:
終端錄入並執行:
clang++ -o main main.cpp -I"./include" -std=c++11
檢視輸出,發現 生成 main
D、執行 main, 可見 輸出 和 日誌檔案
日誌檔案輸出
------完---------
本文僅作參考,請以實際情況為準