rosfmt.h
Go to the documentation of this file.
1 // rosfmt - type-safe ROS_* logging macros
2 // Author: Max Schwarz <max.schwarz@ais.uni-bonn.de>
3 
4 #ifndef ROSFMT_H
5 #define ROSFMT_H
6 
7 #if __cplusplus < 201103L
8 #error rosfmt needs C++11 support. Suggestion: add "set(CMAKE_CXX_STANDARD 11)" \
9  to your CMakeLists.txt.
10 
11 #else
12 
13 #include <ros/console.h>
14 
15 #include <fmt/core.h>
16 
17 #include <string>
18 
19 namespace rosfmt
20 {
21 
22 std::string vformat(fmt::string_view format_str, fmt::format_args args);
23 
24 template<typename ... Args>
25 std::string format(const std::string& formatString, const Args& ... args)
26 {
27  fmt::format_arg_store<fmt::format_context, Args...> as{args...};
28  return rosfmt::vformat(formatString, as);
29 }
30 
31 template<typename... Args>
32 void print(
33  ros::console::FilterBase* filter, void* logger, ros::console::Level level,
34  const char* file, int line, const char* function,
35  const std::string& format, const Args&... args)
36 {
37  std::string s = rosfmt::format(format, args...);
38  std::stringstream ss;
39  ss << s;
40  ros::console::print(filter, logger, level, ss, file, line, function);
41 }
42 
43 }
44 
45 #define ROSFMT_PRINT_AT_LOCATION_WITH_FILTER(filter, ...) \
46  ::rosfmt::print(filter, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__)
47 
48 #define ROSFMT_PRINT_AT_LOCATION(...) \
49  ROSFMT_PRINT_AT_LOCATION_WITH_FILTER(NULL, __VA_ARGS__)
50 
60 #define ROSFMT_LOG_COND(cond, level, name, ...) \
61  do \
62  { \
63  ROSCONSOLE_DEFINE_LOCATION(cond, level, name); \
64  \
65  if (ROS_UNLIKELY(__rosconsole_define_location__enabled)) \
66  { \
67  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
68  } \
69  } while(false)
70 
77 #define ROSFMT_LOG_ONCE(level, name, ...) \
78  do \
79  { \
80  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
81  static bool hit = false; \
82  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(!hit)) \
83  { \
84  hit = true; \
85  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
86  } \
87  } while(false)
88 
96 #define ROSFMT_LOG_THROTTLE(period, level, name, ...) \
97  do \
98  { \
99  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
100  static double last_hit = 0.0; \
101  ::ros::Time now = ::ros::Time::now(); \
102  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(last_hit + period <= now.toSec())) \
103  { \
104  last_hit = now.toSec(); \
105  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
106  } \
107  } while(false)
108 
116 #define ROSFMT_LOG_DELAYED_THROTTLE(period, level, name, ...) \
117  do \
118  { \
119  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
120  ::ros::Time __ros_log_delayed_throttle__now__ = ::ros::Time::now(); \
121  static double __ros_log_delayed_throttle__last_hit__ = __ros_log_delayed_throttle__now__.toSec(); \
122  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(__ros_log_delayed_throttle__last_hit__ + period <= __ros_log_delayed_throttle__now__.toSec())) \
123  { \
124  __ros_log_delayed_throttle__last_hit__ = __ros_log_delayed_throttle__now__.toSec(); \
125  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
126  } \
127  } while(false)
128 
136 #define ROSFMT_LOG_FILTER(filter, level, name, ...) \
137  do \
138  { \
139  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
140  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && (filter)->isEnabled()) \
141  { \
142  ROSFMT_PRINT_AT_LOCATION_WITH_FILTER(filter, __VA_ARGS__); \
143  } \
144  } while(false)
145 
152 #define ROSFMT_LOG(level, name, ...) ROSFMT_LOG_COND(true, level, name, __VA_ARGS__)
153 
154 #include "macros_generated.h"
155 
156 #endif // C++11
157 
158 #endif // include guard
XmlRpcServer s
ROSCONSOLE_DECL void print(FilterBase *filter, void *logger, Level level, const char *file, int line, const char *function, const char *fmt,...) ROSCONSOLE_PRINTF_ATTRIBUTE(7
Definition: rosfmt.cpp:8
std::string vformat(fmt::string_view format_str, fmt::format_args args)
Definition: rosfmt.cpp:11


rosfmt
Author(s):
autogenerated on Tue Jan 5 2021 03:47:40