最近在学习envoy的源码时, 发现项目中使用了一个名为spdlog的开源日志库. 该库在github上Start数达到9.6K, 基本上是C++日志库事实上的王者了.
这里放一段spdlog的示例代码, 让大家对spdlog的使用有一个基本的认识:
#include "spdlog/spdlog.h"#include "spdlog/cfg/env.h" // for loading levels from the environment variableint main(int, char *[]){
std::string key_val("very important message"); spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH); spdlog::warn("Easy padding in numbers like {:08d}, key_val={}", 12, key_val); 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_DEBUG("Support for floats {:03.2f}", 1.23456);}
可以看到, spdlog使用类似python的字符串格式化语法, 这也