windows10 下使用 spdlog 總結(spdlog 1.7)
注意:
請選擇對c++11 支援 完善的編譯器, 為什麼vs2013不行,因為: spdlog一直在更新,VS2013並沒有完全支援C++11(踩坑總結)
1、準備:
A、spdlog 原始碼 :https://github.com/gabime/spdlog
B 、cmake gui:https://cmake.org/download/
C、vs2015 update3 or upper: 沒有下載地址? 點我
2、配置
A、解壓 spdlog 原始碼, 並開啟解壓後的資料夾,新建 build 資料夾
B、開啟 cmake, 新增原始碼目錄 和輸出目錄, 見下圖:
C、點選 cmake UI的 configure. 這裡演示,下面選擇的的是win32
D、configure 完成後, 選擇合適的工程, 再點選 generate生成對應的 工程檔案。
E、生成成功, 至此, 配置到此結束,
下面是使用方法: 踩坑總結:
1、按照 文件(https://github.com/gabime/spdlog/wiki)指示, 需要將庫安裝到作業系統的對應資料夾下去, 上面cmake中選擇生成的專案: install 可實現安裝。這樣, 我們就可以像使用系統庫檔案一樣使用他們了。 優點: 方便,無需修改程式碼,生成解決方案即可實現安裝,新手省卻很多麻煩,缺點: 相比直接拷貝到專案下使用(相對路徑)而言,這樣的方法靈活性不佳。
2、使用直接拷貝到專案資料夾下直接使用。優點: 方便靈活, 缺點,需要手動修改程式碼。
3、提出一種解決方案來解決2中出現的問題:封裝。 例如: 動態連結庫 或者 自己寫一個類 將其封裝起來,我們再使用類或者動態連結庫的介面實現記錄日誌。歡迎留言其他的方案。
4、安裝到系統指定目錄下的方法稱為: 方案1. 拷貝到專案資料夾下使用的方法稱為: 方案2
-------------------------------------------------------------------------------------------------------------------------------------------------------------
方案1:
1、開啟 cmake的輸出目錄(我這裡是在 原始碼解壓目錄下build資料夾), (注意:請使用管理員方式開啟VS201X ? 因為要將檔案安裝到指定的系統資料夾下,沒有對應的許可權,程式無法執行寫入)開啟解決方案 spdlog.sln(以實際生成的為準)。 若上面的配置你和我選擇的一樣,應該會得到下圖的專案:
2、選擇 專案:ALL_BUILD, 右鍵選擇生成。
3、待上一步生成結束後, 選擇 專案: INSTALL, 右鍵生成。 可以觀察到輸出視窗中 將 spdlog檔案安裝到系統的具體目錄。
安裝結束, 回到 spdlog教程:https://github.com/gabime/spdlog/wiki/1.-QuickStart. 試試自己的第一個spdlog把。也可以閱讀 原始碼目錄下的example資料夾下的example.cpp, 總結其使用方法。
--------- 方案1安裝使用結束------------------------
下面是方案2的使用。
方案2 配置上沒有 方案1複雜。只需要spdlog原始碼。 這裡演示, 自己建立了一個空的 Visual c++ 專案, 並手動添加了一個原始檔 main.cpp
步驟:
1、拷貝spdlog原始碼解壓目錄下的include資料夾到當前專案資料夾下。
2、直接在程式碼中對spdlog相關檔案進行引用即可。
新增標頭檔案以用。 ( spdlog有多種記錄日誌的方法,這裡僅 演示一種, 正在摸索其他方法)
#include "include/spdlog/spdlog.h"h" #include "include/spdlog/sinks/rotating_file_sink.h"
新增對 spdlog的空間引用。
using namespace spdlog;
呼叫介面,記錄日誌:
int main() { // Create a file rotating logger with 5mb size max and 3 rotated files. auto rotating_logger_a = spdlog::rotating_logger_mt("log_file_a", "logs/ra.txt", 1024 * 1024 * 5, 100); // testing for (int i = 0; i < 1024 * 10; i++) { rotating_logger_a->info("index = {}", i); cout << "index = " << i << endl; } spdlog::shutdown(); system("pause"); return 0; }
完整原始碼如下:
// -------------------------------------------------------------------- // 下面是對sdplog的檔案引用 #include "include/spdlog/spdlog.h" #include "include/spdlog/cfg/env.h" #include "include/spdlog/sinks/basic_file_sink.h" #include "include/spdlog/sinks/rotating_file_sink.h" #include <iostream> using namespace std; using namespace spdlog; int main() { // Create a file rotating logger with 5mb size max and 3 rotated files. auto rotating_logger_a = spdlog::rotating_logger_mt("log_file_a", "logs/ra.txt", 1024 * 1024 * 5, 100); // testing for (int i = 0; i < 1024 * 10; i++) { rotating_logger_a->info("index = {}", i); cout << "index = " << i << endl; } spdlog::shutdown(); system("pause"); return 0; }
3、編譯 或者 ctrl + shift + B.。 發現輸出 視窗中 提示錯誤:無法找到檔案。
重點來了: 這就是我說的, 需要手動修改程式碼。 雙擊錯誤提示, 定位到下圖:
改為下面的程式碼:
#include "include/spdlog/common.h" #include "include/spdlog/details/registry.h" #include "include/spdlog/logger.h" #include "include/spdlog/version.h" #include "include/spdlog/details/synchronous_factory.h"
再次編譯, 還是提示相同的錯誤型別, 繼續按照上面的方式改。改為相對路徑。因為這些檔案來自 拷貝 過來的 spdlog 原始碼。
重複編譯,改, 這樣的操作。 直到編譯成功如下圖:
F5, 執行結果:
成功。 證明可以正常使用spdlog的相關介面了。 再來看看 演示例子輸出資料夾情況:
後記:
1、繼續學習spdlog
2、針對方案2的缺點,可以 採用封裝的方法,更好的為應用程式服務
3、偷懶: 若你也是按 方案2 操作的, 那就把 演示專案資料夾下的include下的spdlog拷貝一份。下次直接用,不用再次修改啦。
4、以實際的spdlog的情況為準。本文僅為參考。