Logger

mrs_lib::Logger

class Logger

Wrapper for rclcpp::Logger with formatting using std::format.

This wrapper can be used to wrap rclcpp::Logger and call log functions without using the rclcpp macros.

The log methods use std::format to format the messages. This allows adding formatting for custom types by specializing the std::formatter struct.

Public Types

using Level = rclcpp::Logger::Level

Public Functions

inline explicit Logger(NodeInterfaces node)

Construct from node like object with the required interfaces.

inline void set_level(Level level)

Set output level of the stored logger.

This affects the wrapped rclcpp::Logger, so it will for all calls using that logger, not only using this wrapper.

template<typename ...Args>
inline void debug(std::format_string<Args...> fmt, Args&&... args)

Log at the debug specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format.

template<typename ...Args>
inline void info(std::format_string<Args...> fmt, Args&&... args)

Log at the info specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format.

template<typename ...Args>
inline void warning(std::format_string<Args...> fmt, Args&&... args)

Log at the warning specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format.

template<typename ...Args>
inline void error(std::format_string<Args...> fmt, Args&&... args)

Log at the error specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format.

template<typename ...Args>
inline void fatal(std::format_string<Args...> fmt, Args&&... args)

Log at the fatal specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void debug_once(std::format_string<Args...> fmt, Args&&... args)

Log once at the debug specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format. Only the first pass at each call site will generate the output, the rest will be ignored.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void info_once(std::format_string<Args...> fmt, Args&&... args)

Log once at the info specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format. Only the first pass at each call site will generate the output, the rest will be ignored.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void warning_once(std::format_string<Args...> fmt, Args&&... args)

Log once at the warning specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format. Only the first pass at each call site will generate the output, the rest will be ignored.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void error_once(std::format_string<Args...> fmt, Args&&... args)

Log once at the error specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format. Only the first pass at each call site will generate the output, the rest will be ignored.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void fatal_once(std::format_string<Args...> fmt, Args&&... args)

Log once at the fatal specified in the name.

Parameters:
  • fmt – Format string.

  • args – Arguments to format. Only the first pass at each call site will generate the output, the rest will be ignored.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void debug_throttle(std::chrono::milliseconds duration, std::format_string<Args...> fmt, Args&&... args)

Log at the debug specified in the name with a maximum rate.

Parameters:
  • duration – Minimum time between two consecutive logs from this call site.

  • fmt – Format string.

  • args – Arguments to format.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void info_throttle(std::chrono::milliseconds duration, std::format_string<Args...> fmt, Args&&... args)

Log at the info specified in the name with a maximum rate.

Parameters:
  • duration – Minimum time between two consecutive logs from this call site.

  • fmt – Format string.

  • args – Arguments to format.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void warning_throttle(std::chrono::milliseconds duration, std::format_string<Args...> fmt, Args&&... args)

Log at the warning specified in the name with a maximum rate.

Parameters:
  • duration – Minimum time between two consecutive logs from this call site.

  • fmt – Format string.

  • args – Arguments to format.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void error_throttle(std::chrono::milliseconds duration, std::format_string<Args...> fmt, Args&&... args)

Log at the error specified in the name with a maximum rate.

Parameters:
  • duration – Minimum time between two consecutive logs from this call site.

  • fmt – Format string.

  • args – Arguments to format.

template<auto LocTag = MRS_LIB_LOGGER_UNIQUE_TAG, typename ...Args>
inline void fatal_throttle(std::chrono::milliseconds duration, std::format_string<Args...> fmt, Args&&... args)

Log at the fatal specified in the name with a maximum rate.

Parameters:
  • duration – Minimum time between two consecutive logs from this call site.

  • fmt – Format string.

  • args – Arguments to format.

Example

Example of the logger usage.
 1using namespace std::chrono_literals;
 2
 3auto node = std::make_shared<rclcpp::Node>("logger_test_node");
 4auto logger = mrs_lib::Logger(*node);
 5logger.set_level(level);
 6
 7{
 8  logger.debug("Hi");
 9  logger.debug("Hello {}", "world");
10
11  for (size_t i = 0; i < 2; ++i)
12  {
13    logger.debug_once("Only Once");
14    logger.debug_once("Also this is only once");
15  }
16
17  for (size_t i = 0; i < 4; ++i)
18  {
19    logger.debug_throttle(100ms, "Throttled a: {}", i);
20    logger.debug_throttle(100ms, "Throttled b: {}", i);
21    std::this_thread::sleep_for(40ms);
22  }
23}