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();
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 
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>
cras::rosgraphMsgLevelToLogLevel
::ros::console::Level rosgraphMsgLevelToLogLevel(uint8_t msgLevel)
Convert the given rosgraph_msgs::Log level constant to rosconsole logging level.
cras::LogHelper::initializeImpl
virtual void initializeImpl() const =0
Initialize the logger. It is not needed to check initialized as this function is only called when it ...
cras::LogHelper
This class (reps. its descendants) provides unified access to ROS logging functions,...
Definition: log_utils.h:52
cras::logLevelToRosgraphMsgLevel
int8_t logLevelToRosgraphMsgLevel(::ros::console::Level rosLevel)
Convert the given rosconsole logging level to rosgraph_msgs::Log level constant.
cras
Definition: any.hpp:15
cras::LogHelper::Ptr
::std::shared_ptr<::cras::LogHelper > Ptr
Pointer to LogHelper.
Definition: log_utils.h:55
cras::LogHelper::setLogLocationLevel
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.
ros
cras::LogHelper::initializeLogLocationImpl
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...
cras::WrapperLogHelper::wrapped
const ::cras::LogHelper * wrapped
Definition: log_utils.h:279
cras::LogHelper::initializeLogLocation
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...
cras::HasLogger
Convenience base class for providing this->log and getCrasLogger(). Just add it as a base to your cla...
Definition: log_utils.h:229
macros.h
ROS logging helpers macros. Do not include this file directly. Include log_utils.h instead....
cras::format
inline ::std::string format(::std::string format,...)
Definition: string_utils.hpp:280
string_utils.hpp
Utils for working with strings.
cras::LogHelper::LogHelper
LogHelper()
cras::LogHelper::getId
virtual const void * getId() const
Return a unique identifier of this logger. These IDs are used for determining which logger has alread...
ros::console::FilterBase
cras::LogHelper::ConstPtr
::std::shared_ptr< const ::cras::LogHelper > ConstPtr
Const pointer to LogHelper.
Definition: log_utils.h:56
console.h
getCrasLogger
::cras::LogHelperConstPtr getCrasLogger()
Get the default logger used for CRAS_* logging macros. It will never be null.
cras::WrapperLogHelper
Definition: log_utils.h:255
setCrasLogger
::cras::LogHelperConstPtr setCrasLogger(const ::cras::LogHelperConstPtr &log)
Set the default logger used by CRAS_* macros.
cras::LogHelper::getTimeNow
virtual ::ros::Time getTimeNow() const
Get current time (used for throttling messages). By default, ROS time is returned,...
cras::LogHelper::initialized
bool initialized
Whether initialize() has been called at least once. Subclasses are required to handle this invariant.
Definition: log_utils.h:68
cras::LogHelper::checkLogLocationEnabled
virtual void checkLogLocationEnabled(::ros::console::LogLocation *loc) const
Check whether the log location is enabled.
cras::LogHelper::log
void log(::ros::console::Level level, const char *format,...) const
Log a message using the given log severity.
cras::LogHelper::WrapperLogHelper
friend class WrapperLogHelper
Definition: log_utils.h:199
ros::console::levels::Level
Level
cras::LogHelper::print
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().
cras::LogHelperPtr
::cras::LogHelper::Ptr LogHelperPtr
Pointer to LogHelper.
Definition: log_utils.h:202
ros::Time
cras::RosconsoleLogHelper
Log helper relaying all of its work to the same mechanism used by ROS_* logging macros.
Definition: log_utils.h:208
cras::LogHelper::logString
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.
std
cras::LogHelperConstPtr
::cras::LogHelper::ConstPtr LogHelperConstPtr
Const pointer to LogHelper.
Definition: log_utils.h:203
cras::LogHelper::initialize
void initialize() const
Initialize the logger. This function does what it needs and then calls initializeImpl().
cras::HasLogger::log
::cras::LogHelperPtr log
Log helper.
Definition: log_utils.h:252
cras::LogHelper::setGlobalLogger
void setGlobalLogger() const
cras::LogHelper::operator=
LogHelper & operator=(const LogHelper &)=delete
cras::FilterBase
Definition: filter_base.hpp:40
cras::LogHelper::~LogHelper
virtual ~LogHelper()=default
restorePreviousCrasLogger
void restorePreviousCrasLogger()
ros::console::LogLocation


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Tue Nov 26 2024 03:49:04