项目的github网址:
https://github.com/gabime/spdlog
下载、编译源码:
$ git clone https://github.com/gabime/spdlog.git
$ cd spdlog && mkdir build && cd build
$ cmake .、 && make -j16
$ ls
发现有 libspdlog.a
红色字的操作可以加快应用程序编译速度,但是不此处不make也不影响使用。
应用:
在任意开发练习目录中创建源文件 helloSpdlog.cc,文件内容如下:
//helloSpdlog.cc#include "spdlog/spdlog.h"int main() { spdlog::info("Welcome to spdlog!"); spdlog::error("Some error message with arg: {}", 1); spdlog::warn("Easy padding in numbers like {:08d}", 12); spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); spdlog::info("Support for floats {:03.2f}", 1.23456); spdlog::info("Positional args are {1} {0}..", "too", "supported"); spdlog::info("{:<30}", "left aligned"); spdlog::set_level(spdlog::level::debug); // Set global log level to debug spdlog::debug("This message should be displayed.."); // change log pattern spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v"); // Compile time log levels // define SPDLOG_ACTIVE_LEVEL to desired level SPDLOG_TRACE("Some trace message with param {}", 42); SPDLOG_DEBUG("Some debug message");}
在开发目录中编译:
$ g++ helloSpdlog.cc -o helloSpdlog -std=c++11 -I /home/xiaoming/github/spdlog/include -lspdlog
或者加上 -L选项:
$ g++ helloSpdlog.cc -o helloSpdlog -std=c++11 -I /home/xiaoming/github/spdlog/include -L /home/xiaoming/github/spd -lspdlog
运行程序:
$ ./helloSpdlog
输出如下内容:
[2022-02-14 13:49:37.300] [info] Welcome to spdlog!
[2022-02-14 13:49:37.300] [error] Some error message with arg: 1
[2022-02-14 13:49:37.300] [warning] Easy padding in numbers like 00000012
[2022-02-14 13:49:37.300] [critical] Support for int: 42; hex: 2a; oct: 52; bin: 101010
[2022-02-14 13:49:37.300] [info] Support for floats 1.23
[2022-02-14 13:49:37.300] [info] Positional args are supported too..
[2022-02-14 13:49:37.300] [info] left aligned
[2022-02-14 13:49:37.300] [debug] This message should be displayed..
关于 spdlog 输出格式控制:
1、QuickStart · gabime/spdlog Wiki · GitHub
GitHub - fmtlib/fmt: A modern formatting library
Format String Syntax — fmt 8.1.1 documentation