05_c/c++开源库 spdlog日志库

1.简介与安装

spdlog 是一个用于 C++ 的高性能、易用的日志库。它提供了丰富的日志功能,包括多种日志级别、格式化输出、异步日志、自定义日志接收器等。spdlog 是一个轻量级的库,性能优越,非常适合用于需要高性能日志记录的场景。

特点

高性能:spdlog 使用了高效的日志记录机制,性能优越。

易用性:spdlog 提供了简单易用的 API,方便用户快速上手。

丰富的日志功能:支持多种日志级别、格式化输出、异步日志、自定义日志接收器等。日志大小,日志数量配置

跨平台:spdlog 支持多种平台,包括 Windows、Linux、macOS 等。

开源:spdlog 是一个开源项目,可以免费使用。

安装

sudo apt install libspdlog-dev

编译依赖

编译依赖
pkg-config spdlog --cflags --libs

-DSPDLOG_COMPILED_LIB -lspdlog -pthread

编译选项: -DSPDLOG_COMPILED_LIB

链接选项: -lspdlog -pthread

2.实例

1.代码

1_spdlog_输出终端.cc

c++ 复制代码
#include "spdlog/spdlog.h"

int main(void)
{
    spdlog::info("info msg");
    spdlog::set_level(spdlog::level::debug);
    spdlog::debug("int {0:d} {1:4f}", 12, 3.14);
    return 0;
}

2_spdlog_输出到文件.cc

c++ 复制代码
#include <stdint.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>
using namespace std;

int main(void)
{
    uint32_t file_size = 1024 * 1024 * 5; // 日志文件大小
    uint32_t file_num = 3;                // 文件备份数量
    string tag = "spdlog_test";
    string log_path = "logs/" + tag + ".log"; // 文件路径名
    auto logger = spdlog::rotating_logger_mt(tag, log_path, file_size, file_num);
    spdlog::set_default_logger(logger);
    spdlog::set_level(spdlog::level::info);
    spdlog::set_pattern("%Y%m%d-%H:%M:%S.%f [%n:%l] <%g:%#> %v");

    spdlog::info("start spdlog");
    SPDLOG_LOGGER_INFO(logger, "Support for floats {}", "hello world");

    spdlog::drop("logger");
    spdlog::shutdown();
    
    return 0;
}

2.scons编译

SConstruct

python 复制代码
env = Environment()
env["PROGSUFFIX"] = ".out"              # 可执行文件后缀.out
env["CCFLAGS"] = " -g3 -O0 -Wall"       # gdb 调试
env.MergeFlags(["!pkg-config --cflags --libs spdlog"])

env.Program("1_spdlog_输出终端.cc")
env.Program("2_spdlog_输出到文件.cc")

scons

scons: Reading SConscript files ...

scons: done reading SConscript files.

scons: Building targets ...

g++ -o 1_spdlog_输出终端.o -c -g3 -O0 -Wall -DSPDLOG_COMPILED_LIB 1_spdlog_输出终端.cc

g++ -o 1_spdlog_输出终端.out 1_spdlog_输出终端.o -lspdlog -pthread

g++ -o 2_spdlog_输出到文件.o -c -g3 -O0 -Wall -DSPDLOG_COMPILED_LIB 2_spdlog_输出到文件.cc

g++ -o 2_spdlog_输出到文件.out 2_spdlog_输出到文件.o -lspdlog -pthread

scons: done building targets.

3.验证测试

./1_spdlog_输出终端.out

2024-04-23 23:27:19.584 info info msg

2024-04-23 23:27:19.584 debug int 12 3.140000

./2_spdlog_输出到文件.out
cat logs/spdlog_test.log

20240423-23:27:21.353401 spdlog_test:info <:> start spdlog

20240423-23:27:21.353423 spdlog_test:info <2_spdlog_输出到文件.cc:18> Support for floats hello world

3.其它实例

gitee在线代码

1_spdlog_输出终端.cc

2_spdlog_输出到文件.cc

3_spdlog_定时输出文件.cc

4_spdlog_不同的logger_函数输出效果.cc

11_sdblog_格式化实例.cc

12_spdlog_输出到文件.cc

13_spdlog_自定义格式输出_pattern.cc

C++日志记录库 spdlog简介 :

https://blog.csdn.net/alwaysrun/article/details/122771208

https://cloud.tencent.com/developer/article/2102105
官网实例: https://github.com/gabime/spdlog


相关推荐
晓蛋3 天前
c语言指的是什么意思
c语言·编译器·编程开发·集成开发环境·程序实例
倒头就睡的小比特3 天前
算法竞赛C++常用的STL
c++·算法
不悔哥3 天前
开源OV-Watch:怎么做一个智能手表
单片机·开源·嵌入式
weilx12343 天前
C++笔记-文件IO-<fcntl.h>
c++
家和803 天前
Carla仿真系列:4_在 Carla 的 Tesla 上装 4 路摄像头,并创建网格为环视拼接做准备
开源
傲世仙尊3 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
牵猫散步的鱼儿3 天前
重载、重写(覆盖)、重定义区别
c语言
感谢地心引力3 天前
我用 Doubao-Seed-2.1-pro 做了一个深度融入 AI 功能的本地知识库软件
ai·开源·seed·markdown·豆包
alsmile3 天前
Node-RED 之外,国产规则引擎的新方案:基于标准语法,Go 先行实现
后端·开源·go
phltxy3 天前
C 语言指针:从内存地址到灵活的数据访问
c语言