logger.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_PROTOCOL_LOGGER_HPP_INCLUDED
6 #define UAVCAN_PROTOCOL_LOGGER_HPP_INCLUDED
7 
8 #include <uavcan/time.hpp>
10 #include <uavcan/protocol/debug/LogMessage.hpp>
13 #include <cstdlib>
14 
15 #if !defined(UAVCAN_CPP_VERSION) || !defined(UAVCAN_CPP11)
16 # error UAVCAN_CPP_VERSION
17 #endif
18 
19 namespace uavcan
20 {
28 {
29 public:
31 
32  virtual ~ILogSink() { }
33 
38  virtual LogLevel getLogLevel() const { return protocol::debug::LogLevel::DEBUG; }
39 
44  virtual void log(const protocol::debug::LogMessage& message) = 0;
45 };
46 
59 {
60 public:
62 
67  static LogLevel getLogLevelAboveAll() { return (1U << protocol::debug::LogLevel::FieldTypes::value::BitLen) - 1U; }
68 
69 private:
70  enum { DefaultTxTimeoutMs = 2000 };
71 
73  protocol::debug::LogMessage msg_buf_;
74  LogLevel level_;
76 
77  LogLevel getExternalSinkLevel() const
78  {
79  return (external_sink_ == UAVCAN_NULLPTR) ? getLogLevelAboveAll() : external_sink_->getLogLevel();
80  }
81 
82 public:
83  explicit Logger(INode& node)
84  : logmsg_pub_(node)
85  , external_sink_(UAVCAN_NULLPTR)
86  {
88  setTxTimeout(MonotonicDuration::fromMSec(DefaultTxTimeoutMs));
89  UAVCAN_ASSERT(getTxTimeout() == MonotonicDuration::fromMSec(DefaultTxTimeoutMs));
90  }
91 
98  {
99  const int res = logmsg_pub_.init(priority);
100  if (res < 0)
101  {
102  return res;
103  }
104  return 0;
105  }
106 
118  int log(const protocol::debug::LogMessage& message)
119  {
120  int retval = 0;
121  if (message.level.value >= getExternalSinkLevel())
122  {
123  external_sink_->log(message);
124  }
125  if (message.level.value >= level_)
126  {
127  retval = logmsg_pub_.broadcast(message);
128  }
129  return retval;
130  }
131 
138  LogLevel getLevel() const { return level_; }
139  void setLevel(LogLevel level) { level_ = level; }
140 
147  ILogSink* getExternalSink() const { return external_sink_; }
148  void setExternalSink(ILogSink* sink) { external_sink_ = sink; }
149 
154  MonotonicDuration getTxTimeout() const { return logmsg_pub_.getTxTimeout(); }
155  void setTxTimeout(MonotonicDuration val) { logmsg_pub_.setTxTimeout(val); }
156 
174 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
175 
176  template <typename... Args>
177  int log(LogLevel level, const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT;
178 
179  template <typename... Args>
180  inline int logDebug(const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT
181  {
182  return log(protocol::debug::LogLevel::DEBUG, source, format, args...);
183  }
184 
185  template <typename... Args>
186  inline int logInfo(const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT
187  {
188  return log(protocol::debug::LogLevel::INFO, source, format, args...);
189  }
190 
191  template <typename... Args>
192  inline int logWarning(const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT
193  {
194  return log(protocol::debug::LogLevel::WARNING, source, format, args...);
195  }
196 
197  template <typename... Args>
198  inline int logError(const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT
199  {
200  return log(protocol::debug::LogLevel::ERROR, source, format, args...);
201  }
202 
203 #else
204 
205  int log(LogLevel level, const char* source, const char* text) UAVCAN_NOEXCEPT
206  {
207  #if UAVCAN_EXCEPTIONS
208  try
209  #endif
210  {
211  if (level >= level_ || level >= getExternalSinkLevel())
212  {
213  msg_buf_.level.value = level;
214  msg_buf_.source = source;
215  msg_buf_.text = text;
216  return log(msg_buf_);
217  }
218  return 0;
219  }
220  #if UAVCAN_EXCEPTIONS
221  catch (...)
222  {
223  return -ErrFailure;
224  }
225  #endif
226  }
227 
228  int logDebug(const char* source, const char* text) UAVCAN_NOEXCEPT
229  {
230  return log(protocol::debug::LogLevel::DEBUG, source, text);
231  }
232 
233  int logInfo(const char* source, const char* text) UAVCAN_NOEXCEPT
234  {
235  return log(protocol::debug::LogLevel::INFO, source, text);
236  }
237 
238  int logWarning(const char* source, const char* text) UAVCAN_NOEXCEPT
239  {
240  return log(protocol::debug::LogLevel::WARNING, source, text);
241  }
242 
243  int logError(const char* source, const char* text) UAVCAN_NOEXCEPT
244  {
245  return log(protocol::debug::LogLevel::ERROR, source, text);
246  }
247 
248 #endif
249 
252 };
253 
254 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
255 
256 template <typename... Args>
257 int Logger::log(LogLevel level, const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT
258 {
259 #if UAVCAN_EXCEPTIONS
260  try
261 #endif
262  {
263  if (level >= level_ || level >= getExternalSinkLevel())
264  {
265  msg_buf_.level.value = level;
266  msg_buf_.source = source;
267  msg_buf_.text.clear();
269  formatter.write(format, args...);
270  return log(msg_buf_);
271  }
272  return 0;
273  }
274 #if UAVCAN_EXCEPTIONS
275  catch (...)
276  {
277  return -ErrFailure;
278  }
279 #endif
280 }
281 
282 #endif
283 
284 }
285 
286 #endif // UAVCAN_PROTOCOL_LOGGER_HPP_INCLUDED
MonotonicDuration getTxTimeout() const
void setExternalSink(ILogSink *sink)
Definition: logger.hpp:148
int logDebug(const char *source, const char *format, Args... args) UAVCAN_NOEXCEPT
Definition: logger.hpp:180
int logWarning(const char *source, const char *format, Args... args) UAVCAN_NOEXCEPT
Definition: logger.hpp:192
ILogSink * getExternalSink() const
Definition: logger.hpp:147
Publisher< protocol::debug::LogMessage > logmsg_pub_
Definition: logger.hpp:72
StorageType< typename protocol::debug::LogLevel::FieldTypes::value >::Type LogLevel
Definition: logger.hpp:30
int log(const protocol::debug::LogMessage &message)
Definition: logger.hpp:118
MonotonicDuration getTxTimeout() const
Definition: logger.hpp:154
void write(const char *text)
static LogLevel getLogLevelAboveAll()
Definition: logger.hpp:67
virtual LogLevel getLogLevel() const
Definition: logger.hpp:38
level
Definition: dsdl/test.py:6
void setTxTimeout(MonotonicDuration tx_timeout)
int logError(const char *source, const char *format, Args... args) UAVCAN_NOEXCEPT
Definition: logger.hpp:198
format
Definition: dsdl/test.py:6
int logInfo(const char *source, const char *format, Args... args) UAVCAN_NOEXCEPT
Definition: logger.hpp:186
ILogSink * external_sink_
Definition: logger.hpp:75
void setLevel(LogLevel level)
Definition: logger.hpp:139
LogLevel getExternalSinkLevel() const
Definition: logger.hpp:77
LogLevel level_
Definition: logger.hpp:74
protocol::debug::LogMessage msg_buf_
Definition: logger.hpp:73
void setTxTimeout(MonotonicDuration val)
Definition: logger.hpp:155
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
Logger(INode &node)
Definition: logger.hpp:83
virtual ~ILogSink()
Definition: logger.hpp:32
virtual void log(const protocol::debug::LogMessage &message)=0
static const TransferPriority Lowest
Definition: transfer.hpp:42
int init(const TransferPriority priority=TransferPriority::Lowest)
Definition: logger.hpp:97
LogLevel getLevel() const
Definition: logger.hpp:138
int broadcast(const DataType &message)
Definition: publisher.hpp:52
ILogSink::LogLevel LogLevel
Definition: logger.hpp:61


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39