超级快的C++日志库:spdlog

jopen 10年前

spdlog 是一个只包含头文件,兼容 C++11。快速的 C++ 日志库。

安装:只需将文件复制到你的构建,然后使用一个C++11编译器。

#include <iostream>  #include "spdlog/spdlog.h"     int main(int, char* [])  {      namespace spd = spdlog;         try      {          std::string filename = "spdlog_example";          auto console = spd::stdout_logger_mt("console");          console->info("Welcome to spdlog!") ;          console->info() << "Creating file " << filename << "..";             auto file_logger = spd::rotating_logger_mt("file_logger", filename, 1024 * 1024 * 5, 3);          file_logger->info("Log file message number", 1);             for (int i = 0; i < 100; ++i)          {              auto square = i*i;              file_logger->info() << i << '*' << i << '=' << square << " (" << "0x" << std::hex << square << ")";          }             // Change log level to all loggers to warning and above          spd::set_level(spd::level::WARN);          console->info("This should not be displayed");          console->warn("This should!");          spd::set_level(spd::level::INFO);             // Change format pattern to all loggers          spd::set_pattern(" **** %Y-%m-%d %H:%M:%S.%e %l **** %v");          spd::get("console")->info("This is another message with different format");      }      catch (const spd::spdlog_ex& ex)      {          std::cout << "Log failed: " << ex.what() << std::endl;      }      return 0;  }

特性:

  • 非常快 – 性能是主要目的(see becnhmarks below)
  • Headers only
  • 没有依赖
  • 支持多个平台 - Linux / Windows on 32/64 bits
  • 多个/单个线程日志器Mult/Single threaded loggers
  • Rotating log files
  • Daily log files
  • Console logging
  • Optional async logging
  • Logging levels
  • Custom formatting with user defined patterns

项目主页:http://www.open-open.com/lib/view/home/1415152358949