gabime / spdlog

gabime / spdlog

spdlog Fast C++ logging library.

spdlog 一个快速的 C++ 日志库。

Install

Header-only version Copy the include folder to your build tree and use a C++11 compiler.

安装 仅头文件版本 将 include 文件夹复制到你的构建目录中,并使用 C++11 编译器即可。

Compiled version (recommended - much faster compile times)

$ git clone https://github.com/gabime/spdlog.git
$ cd spdlog && mkdir build && cd build
$ cmake .. && cmake --build .

see example CMakeLists.txt on how to use.

编译版本(推荐 - 编译速度快得多)

$ git clone https://github.com/gabime/spdlog.git
$ cd spdlog && mkdir build && cd build
$ cmake .. && cmake --build .

请参阅示例 CMakeLists.txt 以了解如何使用。

Platforms Linux, FreeBSD, OpenBSD, Solaris, AIX Windows (msvc 2013+, cygwin) macOS (clang 3.5+) Android

平台 Linux, FreeBSD, OpenBSD, Solaris, AIX Windows (msvc 2013+, cygwin) macOS (clang 3.5+) Android

Package managers: Debian: sudo apt install libspdlog-dev Homebrew: brew install spdlog MacPorts: sudo port install spdlog FreeBSD: pkg install spdlog Fedora: dnf install spdlog Gentoo: emerge dev-libs/spdlog Arch Linux: pacman -S spdlog openSUSE: sudo zypper in spdlog-devel ALT Linux: apt-get install libspdlog-devel vcpkg: vcpkg install spdlog conan: conan install --requires=spdlog/[*] conda: conda install -c conda-forge spdlog build2: depends: spdlog ^1.8.2

包管理器: Debian: sudo apt install libspdlog-dev Homebrew: brew install spdlog MacPorts: sudo port install spdlog FreeBSD: pkg install spdlog Fedora: dnf install spdlog Gentoo: emerge dev-libs/spdlog Arch Linux: pacman -S spdlog openSUSE: sudo zypper in spdlog-devel ALT Linux: apt-get install libspdlog-devel vcpkg: vcpkg install spdlog conan: conan install --requires=spdlog/[*] conda: conda install -c conda-forge spdlog build2: depends: spdlog ^1.8.2

Features

  • Very fast (see benchmarks below).
  • Headers only or compiled.
  • Feature-rich formatting, using the excellent fmt library.
  • Asynchronous mode (optional).
  • Custom formatting.
  • Multi/Single threaded loggers.
  • Various log targets:
    • Rotating log files.
    • Daily log files.
    • Console logging (colors supported).
    • syslog.
    • Windows event log.
    • Windows debugger (OutputDebugString(..)).
    • Log to Qt widgets (example).
  • Easily extendable with custom log targets.
  • Log filtering - log levels can be modified at runtime as well as compile time.
  • Support for loading log levels from argv or environment var.
  • Backtrace support - store debug messages in a ring buffer and display them later on demand.

特性

  • 非常快(见下文基准测试)。
  • 仅头文件或编译版本。
  • 功能丰富的格式化,使用优秀的 fmt 库。
  • 异步模式(可选)。
  • 自定义格式化。
  • 多线程/单线程记录器。
  • 多种日志目标:
    • 轮转日志文件。
    • 每日日志文件。
    • 控制台日志(支持颜色)。
    • syslog。
    • Windows 事件日志。
    • Windows 调试器 (OutputDebugString(..))。
    • 输出到 Qt 组件(示例)。
  • 可通过自定义日志目标轻松扩展。
  • 日志过滤 - 日志级别可以在运行时和编译时修改。
  • 支持从 argv 或环境变量加载日志级别。
  • 回溯支持 - 将调试消息存储在环形缓冲区中,并在需要时显示。

Usage samples (使用示例)

Basic usage (基本用法)

#include "spdlog/spdlog.h"

int main() {
    spdlog::info("Welcome to spdlog!");
    spdlog::error("Some error message with arg: {}", 1);
    spdlog::warn("Easy padding in numbers like {:08d}", 12);
    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::info("Positional args are {1} {0}..", "too", "supported");
    spdlog::info("{:<30}", "left aligned");

    spdlog::set_level(spdlog::level::debug); // Set *global* log level to debug
    spdlog::debug("This message should be displayed..");

    // change log pattern
    spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");

    // Compile time log levels
    // Note that this does not change the current log level, it will only
    // remove (depending on SPDLOG_ACTIVE_LEVEL) the call on the release code.
    SPDLOG_TRACE("Some trace message with param {}", 42);
    SPDLOG_DEBUG("Some debug message");
}

Create stdout/stderr logger object (创建标准输出/错误记录器对象)

#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"

void stdout_example() {
    // create a color multi-threaded logger
    auto console = spdlog::stdout_color_mt("console");
    auto err_logger = spdlog::stderr_color_mt("stderr");
    spdlog::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name)");
}

Basic file logger (基础文件记录器)

#include "spdlog/sinks/basic_file_sink.h"

void basic_logfile_example() {
    try {
        auto logger = spdlog::basic_logger_mt("basic_logger", "logs/basic-log.txt");
    } catch (const spdlog::spdlog_ex &ex) {
        std::cout << "Log init failed: " << ex.what() << std::endl;
    }
}

Rotating files (轮转文件)

#include "spdlog/sinks/rotating_file_sink.h"

void rotating_example() {
    // Create a file rotating logger with 5 MB size max and 3 rotated files
    auto max_size = 1048576 * 5;
    auto max_files = 3;
    auto logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", max_size, max_files);
}

Daily files (每日文件)

#include "spdlog/sinks/daily_file_sink.h"

void daily_example() {
    // Create a daily logger - a new file is created every day at 2:30 am
    auto logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
}

Backtrace support (回溯支持)

// Debug messages can be stored in a ring buffer instead of being logged immediately.
// This is useful to display debug logs only when needed (e.g. when an error happens).
// When needed, call dump_backtrace() to dump them to your log.

spdlog::enable_backtrace(32); // Store the latest 32 messages in a buffer.
// or my_logger->enable_backtrace(32)..

for(int i = 0; i < 100; i++) {
    spdlog::debug("Backtrace message {}", i); // not logged yet..
}

// e.g. if some error happened:
spdlog::dump_backtrace(); // log them now! show the last 32 messages
// or my_logger->dump_backtrace(32)..

Periodic flush (定期刷新)

// periodically flush all *registered* loggers every 3 seconds:
// warning: only use if all your loggers are thread-safe ("_mt" loggers)
spdlog::flush_every(std::chrono::seconds(3));

Stopwatch (秒表)

// Stopwatch support for spdlog
#include "spdlog/stopwatch.h"

void stopwatch_example() {
    spdlog::stopwatch sw;
    spdlog::debug("Elapsed {}", sw);
    spdlog::debug("Elapsed {:.3}", sw);
}

Log binary data in hex (以十六进制记录二进制数据)

#include "spdlog/fmt/bin_to_hex.h"

void binary_example() {
    auto console = spdlog::get("console");
    std::array<char, 80> buf;
    console->info("Binary example: {}", spdlog::to_hex(buf));
    console->info("Another binary example:{:n}", spdlog::to_hex(std::begin(buf), std::begin(buf) + 10));
}

Logger with multi sinks (多接收器记录器)

// create a logger with 2 targets, with different log levels and formats.
// The console will show only warnings or errors, while the file will log all.
void multi_sink_example() {
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(spdlog::level::warn);
    console_sink->set_pattern("%^[%l]%$ %v");
    // ...
}