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


相关推荐
说私域1 小时前
定制开发开源AI智能名片S2B2C商城小程序在互联网族群化中的作用与影响
人工智能·小程序·开源
黄贵根1 小时前
C++STL系列-04. list和forward_list
c++·list
说私域1 小时前
开源AI大模型AI智能名片S2B2C商城小程序在互联网族群化中的作用与影响
人工智能·小程序·开源
GitCode官方1 小时前
告别环境地狱!Java生态“AI原生”解决方案入驻 GitCode
java·开源·gitcode
羑悻的小杀马特2 小时前
CMake 全流程开发实战:从零开始掌握C++项目构建、测试到一键分发的完整解决方案
c++·cmake
T1an-12 小时前
C++版单例模式-现代化简洁写法
c++·单例模式
一拳一个呆瓜5 小时前
【MFC】对话框属性:Absolute Align(绝对对齐)
c++·mfc
爱编程的化学家6 小时前
代码随想录算法训练营第六天 - 哈希表2 || 454.四数相加II / 383.赎金信 / 15.三数之和 / 18.四数之和
数据结构·c++·算法·leetcode·双指针·哈希
许怀楠8 小时前
【主页介绍】
linux·c++·贪心算法·visual studio
木心爱编程10 小时前
C++链表实战:STL与手动实现详解
开发语言·c++·链表