log_utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 // SPDX-License-Identifier: BSD-3-Clause
4 // SPDX-FileCopyrightText: Czech Technical University in Prague
5 
22 #include <cstdarg>
23 #include <memory>
24 #include <string>
25 
26 #include <ros/console.h>
27 
29 
30 namespace cras
31 {
32 
39 
46 
52 class LogHelper
53 {
54 public:
55  typedef ::std::shared_ptr<::cras::LogHelper> Ptr;
56  typedef ::std::shared_ptr<const ::cras::LogHelper> ConstPtr;
57 
58  // The class is not copyable/movable to prevent slicing
59 
60  LogHelper() = default;
61  virtual ~LogHelper() = default;
62  LogHelper(const LogHelper&) = delete;
63  LogHelper& operator=(const LogHelper&) = delete;
64  LogHelper(LogHelper &&) = delete;
65  LogHelper& operator=(LogHelper &&) = delete;
66 
68  mutable bool initialized {false};
69 
73  void initialize() const;
74 
75 protected:
80  virtual void initializeImpl() const = 0;
81 
82 public:
93  ::ros::console::LogLocation* loc, const std::string& name, ::ros::console::Level level) const;
94 
95 protected:
103  virtual void initializeLogLocationImpl(
104  ::ros::console::LogLocation* loc, const std::string& name, ::ros::console::Level level) const = 0;
105 
106 public:
112  virtual void setLogLocationLevel(::ros::console::LogLocation* loc, ::ros::console::Level level) const;
113 
118  virtual void checkLogLocationEnabled(::ros::console::LogLocation* loc) const;
119 
129  virtual void logString(void* logger, ::ros::console::Level level, const ::std::string& str, const char* file,
130  uint32_t line, const char* function) const = 0;
131 
137  virtual ::ros::Time getTimeNow() const;
138 
144  virtual const void* getId() const;
145 
149  void print(::ros::console::FilterBase* filter, void* logger, ::ros::console::Level level,
150  const char* file, int line, const char* function, const char* fmt, ...) const ROSCONSOLE_PRINTF_ATTRIBUTE(8, 9);
151 
155  void print(::ros::console::FilterBase* filter, void* logger, ::ros::console::Level level,
156  const char* file, int line, const char* function, ::std::string fmt, ...) const;
157 
161  void print(::ros::console::FilterBase* filter, void* logger, ::ros::console::Level level,
162  const ::std::stringstream& ss, const char* file, int line, const char* function) const;
163 
167  void print(::ros::console::FilterBase* filter, void* logger, ::ros::console::Level level, const ::std::string& str,
168  const char* file, int line, const char* function) const;
169 
170  [[deprecated("This function will be removed in a future release.")]]
171  void setGlobalLogger() const;
172 
179  [[deprecated("Use CRAS_* logging macros instead.")]]
180  void log(::ros::console::Level level, const char* format, ...) const;
181 
188  [[deprecated("Use CRAS_* logging macros instead.")]]
189  void log(::ros::console::Level level, ::std::string format, ...) const;
190 
196  [[deprecated("Use CRAS_* logging macros instead.")]]
197  void print(::ros::console::Level level, const ::std::string& text) const;
198 
199  friend class WrapperLogHelper;
200 };
201 
202 typedef ::cras::LogHelper::Ptr LogHelperPtr;
203 typedef ::cras::LogHelper::ConstPtr LogHelperConstPtr;
204 
209 {
210 public:
211  void setLogLocationLevel(::ros::console::LogLocation* loc, ::ros::console::Level level) const override;
212 
213  void checkLogLocationEnabled(::ros::console::LogLocation* loc) const override;
214 
215  void logString(void* logger, ::ros::console::Level level, const ::std::string& str, const char* file, uint32_t line,
216  const char* function) const override;
217 
218 protected:
219  void initializeImpl() const override;
220 
222  ::ros::console::LogLocation* loc, const ::std::string& name, ::ros::console::Level level) const override;
223 };
224 
230 {
231 public:
237 
243 
249 
250 protected:
253 };
254 
255 class [[deprecated("This wrapper should only be used to provide backward compatibility.")]]
257 {
258 public:
259  explicit WrapperLogHelper(const ::cras::LogHelper* wrapped);
260 
261  void setLogLocationLevel(::ros::console::LogLocation* loc, ::ros::console::Level level) const override;
262 
263  void checkLogLocationEnabled(::ros::console::LogLocation* loc) const override;
264 
265  void logString(void* logger, ::ros::console::Level level, const ::std::string& str, const char* file, uint32_t line,
266  const char* function) const override;
267 
268  ::ros::Time getTimeNow() const override;
269 
270  const void* getId() const override;
271 
272 protected:
273  void initializeImpl() const override;
274 
276  ::ros::console::LogLocation* loc, const ::std::string& name, ::ros::console::Level level) const override;
277 
278 private:
279  const ::cras::LogHelper* wrapped;
280 };
281 
282 }
283 
289 
296 
297 [[deprecated("This function will be removed in a future release.")]]
299 
301 #include <cras_cpp_common/log_utils/deprecated/macros.h>
LogHelper()=default
::cras::LogHelperConstPtr getCrasLogger()
Get the default logger used for CRAS_* logging macros. It will never be null.
void setGlobalLogger() const
void print(::ros::console::FilterBase *filter, void *logger, ::ros::console::Level level, const char *file, int line, const char *function, const char *fmt,...) const ROSCONSOLE_PRINTF_ATTRIBUTE(8
Print function used by the macros. It basically just relays its work to logString().
void log(::ros::console::Level level, const char *format,...) const
Log a message using the given log severity.
::std::shared_ptr< const ::cras::LogHelper > ConstPtr
Const pointer to LogHelper.
Definition: log_utils.h:56
::cras::LogHelperPtr log
Log helper.
Definition: log_utils.h:252
ROS logging helpers macros. Do not include this file directly. Include log_utils.h instead...
virtual void logString(void *logger, ::ros::console::Level level, const ::std::string &str, const char *file, uint32_t line, const char *function) const =0
Write the given string to the log.
::cras::LogHelperConstPtr setCrasLogger(const ::cras::LogHelperConstPtr &log)
Set the default logger used by CRAS_* macros.
::cras::LogHelper::ConstPtr LogHelperConstPtr
Const pointer to LogHelper.
Definition: log_utils.h:203
int8_t logLevelToRosgraphMsgLevel(::ros::console::Level rosLevel)
Convert the given rosconsole logging level to rosgraph_msgs::Log level constant.
Log helper relaying all of its work to the same mechanism used by ROS_* logging macros.
Definition: log_utils.h:208
bool initialized
Whether initialize() has been called at least once. Subclasses are required to handle this invariant...
Definition: log_utils.h:68
virtual void initializeImpl() const =0
Initialize the logger. It is not needed to check initialized as this function is only called when it ...
LogHelper & operator=(const LogHelper &)=delete
friend class WrapperLogHelper
Definition: log_utils.h:199
virtual void initializeLogLocationImpl(::ros::console::LogLocation *loc, const std::string &name, ::ros::console::Level level) const =0
Initialize the given log location with proper data for later use. This function is only called if the...
#define ROSCONSOLE_PRINTF_ATTRIBUTE(a, b)
::std::shared_ptr<::cras::LogHelper > Ptr
Pointer to LogHelper.
Definition: log_utils.h:55
inline ::std::string format(const char *format, ::va_list args)
virtual void setLogLocationLevel(::ros::console::LogLocation *loc, ::ros::console::Level level) const
Set level of a log location. This is only called when global logging level changes.
virtual void checkLogLocationEnabled(::ros::console::LogLocation *loc) const
Check whether the log location is enabled.
Definition: any.hpp:15
void restorePreviousCrasLogger()
void initialize() const
Initialize the logger. This function does what it needs and then calls initializeImpl().
::ros::console::Level rosgraphMsgLevelToLogLevel(uint8_t msgLevel)
Convert the given rosgraph_msgs::Log level constant to rosconsole logging level.
void initializeLogLocation(::ros::console::LogLocation *loc, const std::string &name, ::ros::console::Level level) const
Initialize the given log location with proper data for later use. This is the function called from ma...
Convenience base class for providing this->log and getCrasLogger(). Just add it as a base to your cla...
Definition: log_utils.h:229
virtual ::ros::Time getTimeNow() const
Get current time (used for throttling messages). By default, ROS time is returned, with fallback to wall time if ROS time is not initialized.
Utils for working with strings.
virtual const void * getId() const
Return a unique identifier of this logger. These IDs are used for determining which logger has alread...
This class (reps. its descendants) provides unified access to ROS logging functions, be it ROS_* or NODELET_*.
Definition: log_utils.h:52
virtual ~LogHelper()=default
::cras::LogHelper::Ptr LogHelperPtr
Pointer to LogHelper.
Definition: log_utils.h:202


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sat Jun 17 2023 02:32:53