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 // Enable old auto-detection of std::ostream operators
14 #ifndef ROSFMT_NO_DEPRECATED_OSTREAM
15 #ifndef FMT_DEPRECATED_OSTREAM
16 #define FMT_DEPRECATED_OSTREAM
17 #endif
18 #endif
19 
20 #include <ros/console.h>
21 
22 #include <fmt/core.h>
23 
24 #include <string>
25 
26 namespace rosfmt
27 {
28 
29 std::string vformat(fmt::string_view format_str, fmt::format_args args);
30 
31 template<typename ... Args>
32 std::string format(const std::string& formatString, const Args& ... args)
33 {
34  fmt::format_arg_store<fmt::format_context, Args...> as{args...};
35  return rosfmt::vformat(formatString, as);
36 }
37 
38 template<typename... Args>
39 void print(
40  ros::console::FilterBase* filter, void* logger, ros::console::Level level,
41  const char* file, int line, const char* function,
42  fmt::format_string<Args...> fmt, Args&&... args)
43 {
44  std::string s = rosfmt::vformat(fmt, fmt::make_format_args(std::forward<Args>(args)...));
45  std::stringstream ss;
46  ss << s;
47  ros::console::print(filter, logger, level, ss, file, line, function);
48 }
49 
50 }
51 
52 #define ROSFMT_PRINT_AT_LOCATION_WITH_FILTER(filter, ...) \
53  ::rosfmt::print(filter, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__)
54 
55 #define ROSFMT_PRINT_AT_LOCATION(...) \
56  ROSFMT_PRINT_AT_LOCATION_WITH_FILTER(NULL, __VA_ARGS__)
57 
67 #define ROSFMT_LOG_COND(cond, level, name, ...) \
68  do \
69  { \
70  ROSCONSOLE_DEFINE_LOCATION(cond, level, name); \
71  \
72  if (ROS_UNLIKELY(__rosconsole_define_location__enabled)) \
73  { \
74  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
75  } \
76  } while(false)
77 
84 #define ROSFMT_LOG_ONCE(level, name, ...) \
85  do \
86  { \
87  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
88  static bool hit = false; \
89  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(!hit)) \
90  { \
91  hit = true; \
92  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
93  } \
94  } while(false)
95 
103 #define ROSFMT_LOG_THROTTLE(period, level, name, ...) \
104  do \
105  { \
106  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
107  static double last_hit = 0.0; \
108  ::ros::Time now = ::ros::Time::now(); \
109  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(last_hit + period <= now.toSec())) \
110  { \
111  last_hit = now.toSec(); \
112  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
113  } \
114  } while(false)
115 
123 #define ROSFMT_LOG_DELAYED_THROTTLE(period, level, name, ...) \
124  do \
125  { \
126  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
127  ::ros::Time __ros_log_delayed_throttle__now__ = ::ros::Time::now(); \
128  static double __ros_log_delayed_throttle__last_hit__ = __ros_log_delayed_throttle__now__.toSec(); \
129  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(__ros_log_delayed_throttle__last_hit__ + period <= __ros_log_delayed_throttle__now__.toSec())) \
130  { \
131  __ros_log_delayed_throttle__last_hit__ = __ros_log_delayed_throttle__now__.toSec(); \
132  ROSFMT_PRINT_AT_LOCATION(__VA_ARGS__); \
133  } \
134  } while(false)
135 
143 #define ROSFMT_LOG_FILTER(filter, level, name, ...) \
144  do \
145  { \
146  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
147  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && (filter)->isEnabled()) \
148  { \
149  ROSFMT_PRINT_AT_LOCATION_WITH_FILTER(filter, __VA_ARGS__); \
150  } \
151  } while(false)
152 
159 #define ROSFMT_LOG(level, name, ...) ROSFMT_LOG_COND(true, level, name, __VA_ARGS__)
160 
161 #include "macros_generated.h"
162 
163 #endif // C++11
164 
165 #endif // include guard
s
XmlRpcServer s
ros::console::FilterBase
rosfmt
Definition: rosfmt.cpp:8
console.h
rosfmt::vformat
std::string vformat(fmt::string_view format_str, fmt::format_args args)
Definition: rosfmt.cpp:11
ros::console::levels::Level
Level
print
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 Thu Jul 13 2023 02:09:08