SPDLOG#

Introduction#

SPDLOG is a useful and complete logging library, available for C++. We tested it shortly with ROS2.

How to use ?#

Head over to the SPDLOG repository to get all the necessary informations. It is simple and their example are detailed.

Example of use with ROS2#

  1. CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(spdlogger)

# Default to C99
if(NOT CMAKE_C_STANDARD)
   set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
   set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
   add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(spdlog REQUIRED) # <--------------------------------------- add SPDLOG here

add_executable(logger_app src/logger_app.cpp)
target_include_directories(logger_app PUBLIC
   $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
   $<INSTALL_INTERFACE:include>)

target_link_libraries(logger_app PRIVATE spdlog::spdlog) # <------------ add SPDLOG here

install(TARGETS logger_app
   DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
   find_package(ament_lint_auto REQUIRED)
   # the following line skips the linter which checks for copyrights, uncomment the line when a copyright and license is not present in all source files
   set(ament_cmake_copyright_FOUND TRUE)

   # the following line skips cpplint (only works in a git repo), uncomment the line when this package is not in a git repo
   set(ament_cmake_cpplint_FOUND TRUE)
   ament_lint_auto_find_test_dependencies()
endif()

ament_package()
  1. package.xml

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
   <name>spdlogger</name>
   <version>0.0.0</version>
   <description>TODO: Package description</description>
   <maintainer email="gilles.mottiez@hevs.ch">spl</maintainer>
   <license>TODO: License declaration</license>

   <buildtool_depend>ament_cmake</buildtool_depend>

   <depend>spdlog</depend>

   <test_depend>ament_lint_auto</test_depend>
   <test_depend>ament_lint_common</test_depend>

   <export>
      <build_type>ament_cmake</build_type>
   </export>
</package>
  1. main.cpp

#include <cstdio>
#include "spdlog/spdlog.h"

int main(int argc, char ** argv)
{
   (void) argc;
   (void) argv;

   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..");
   spdlog::set_level(spdlog::level::info); // Set global log level to info
   spdlog::debug("This message should not be displayed..");

   return 0;
}

C++ Library