安装

编译、运行平台

  • Linux,FreeBSD,OpenBSD,Solaris,AIX
  • Windows(MSVC 2013+,cygwin)
  • MacOS(clang 3.5+)
  • Andriod

编译、安装

直接拷贝include文件到用户的项目

由于spdlog是基于头文件实现的,实现代码都位于include目录下的头文件中。因此,可以拷贝include文件夹到你的构建树中,使用C++11编译器。

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

项目(example示例)的CMake文件可以这样写:

# Copyright(c) 2019 spdlog authors Distributed under the MIT License (http://opensource.org/licenses/MIT)

cmake_minimum_required(VERSION 3.10)
project(spdlog_examples CXX)

if(NOT TARGET spdlog)
    # Stand-alone build
    find_package(spdlog REQUIRED)
endif()

# ---------------------------------------------------------------------------------------
# Example of using pre-compiled library
# ---------------------------------------------------------------------------------------
add_executable(example example.cpp)
target_link_libraries(example PRIVATE spdlog::spdlog)

# ---------------------------------------------------------------------------------------
# Example of using header-only library
# ---------------------------------------------------------------------------------------
if(SPDLOG_BUILD_EXAMPLE_HO)
    add_executable(example_header_only example.cpp)
    target_link_libraries(example_header_only PRIVATE spdlog::spdlog_header_only)
endif()

Windows下编译

使用git clone将源码下载到本地后,有两种编译方式:1)使用CMake GUI + VS 2022;2)使用VS 2022直接编译。

使用CMake-GUI步骤:
(1)设置源码路径、build路径、根据需要设置CMAKE_INSTALL_PREFIX(安装路径)

(2)然后依次点击Configure、Generate按钮,出现如图所示”Generating done”提示,说明生成VS工程文件。

(3)接下来,需要用VS (我用的VS Community 2022)打开build路径下的.sln解决方案文件,打开成功后如下图所示:

注意:如果CMAK_INSTALL_PREFIX指定的目录是系统盘,如C盘,需要管理员方式打开VS 2022,否则可能没有权限安装文件到系统盘。

(4)右键ALL_BUILD点击生成,从而编译目标,右键INSTALL点击生成,会安装库文件到前面CMAK_INSTALL_PREFIX指定的目录。

会生成2个文件夹include包含头文件,lib包含库文件。

至此,说明编译、安装成功。接下来,在自己的项目中引入并使用spdlog。

使用spdlog

Windows下使用spdlog

通过一个例程,演示如何在VS 2022上的APP工程使用spdlog记录日志。

1)设置头文件路径
首先将头文件路径(include)包含进项目的搜索头文件路径,或者直接将include目录下的文件拷贝到编译器MSVC的头文件目录。

2)设置库文件路径
将lib目录下的spdlogd.lib库文件路径包含进链接器的库文件路径,或者直接将lib目录下的文件拷贝到编译器MSVS的库文件目录。

3)在APP源码中#include spdlog库头文件及测试代码

以下代码来自spdlog自带的example/example.cpp例程(也可以从这个地址拷贝 Basic example | 1.QuickStart | spdlog wiki

#include <iostream>

#include <spdlog/spdlog.h>
#include <spdlog/cfg/env.h>  // support for loading levels from the environment  variable
#include <spdlog/fmt/ostr.h> // support for user defined types

using namespace std;
using namespace spdlog;

int main()
{
       // Set the log level to "info" and mylogger to "trace":
       // SPDLOG_LEVEL=info,mylogger=trace && ./example
       spdlog::cfg::load_env_levels();
       spdlog::info("Welcome to spdlog version {}.{}.{}  !", SPDLOG_VER_MAJOR,  SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
       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("{:>8} aligned, {:<8} aligned", "right", "left");
       return 0;
}

4)编译、运行APP
可从终端观察到日志输出如下:

参考

windows10 下使用 spdlog 总结(spdlog 1.7)

原文地址:http://www.cnblogs.com/fortunely/p/16795289.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性