当前位置:   article > 正文

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

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

#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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2_spdlog_输出到文件.cc

#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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

2.scons编译

SConstruct

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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/490062
推荐阅读
相关标签
  

闽ICP备14008679号