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/format.h>
16 
17 #include <string>
18 
19 namespace rosfmt
20 {
21 
22 template<typename... Args>
23 void print(
24  ros::console::FilterBase* filter, void* logger, ros::console::Level level,
25  const char* file, int line, const char* function,
26  const std::string& format, const Args&... args)
27 {
28  std::string s = fmt::format(format, args...);
29  std::stringstream ss;
30  ss << s;
31  ros::console::print(filter, logger, level, ss, file, line, function);
32 }
33 
34 }
35 
36 #define ROSFMT_PRINT_AT_LOCATION_WITH_FILTER(filter, ...) \
37  ::rosfmt::print(filter, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__)
38 
39 #define ROSFMT_PRINT_AT_LOCATION(...) \
40  ROSFMT_PRINT_AT_LOCATION_WITH_FILTER(NULL, __VA_ARGS__)
41 
51 #define ROSFMT_LOG_COND(cond, level, name, ...) \
52  do \
53  { \
54  ROSCONSOLE_DEFINE_LOCATION(cond, level, name); \
55  \
56  if (ROS_UNLIKELY(__rosconsole_define_location__enabled)) \
57  { \
58  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
59  } \
60  } while(false)
61 
68 #define ROSFMT_LOG_ONCE(level, name, ...) \
69  do \
70  { \
71  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
72  static bool hit = false; \
73  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(!hit)) \
74  { \
75  hit = true; \
76  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
77  } \
78  } while(false)
79 
87 #define ROSFMT_LOG_THROTTLE(period, level, name, ...) \
88  do \
89  { \
90  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
91  static double last_hit = 0.0; \
92  ::ros::Time now = ::ros::Time::now(); \
93  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(last_hit + period <= now.toSec())) \
94  { \
95  last_hit = now.toSec(); \
96  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
97  } \
98  } while(false)
99 
107 #define ROSFMT_LOG_DELAYED_THROTTLE(period, level, name, ...) \
108  do \
109  { \
110  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
111  ::ros::Time __ros_log_delayed_throttle__now__ = ::ros::Time::now(); \
112  static double __ros_log_delayed_throttle__last_hit__ = __ros_log_delayed_throttle__now__.toSec(); \
113  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(__ros_log_delayed_throttle__last_hit__ + period <= __ros_log_delayed_throttle__now__.toSec())) \
114  { \
115  __ros_log_delayed_throttle__last_hit__ = __ros_log_delayed_throttle__now__.toSec(); \
116  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
117  } \
118  } while(false)
119 
127 #define ROSFMT_LOG_FILTER(filter, level, name, ...) \
128  do \
129  { \
130  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
131  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && (filter)->isEnabled()) \
132  { \
133  ROSFMT_PRINT_AT_LOCATION_WITH_FILTER(filter, __VA_ARGS__); \
134  } \
135  } while(false)
136 
143 #define ROSFMT_LOG(level, name, ...) ROSFMT_LOG_COND(true, level, name, __VA_ARGS__)
144 
145 #include "macros_generated.h"
146 
147 #endif // C++11
148 
149 #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


rosfmt
Author(s):
autogenerated on Wed May 15 2019 02:54:11