iceoryx_doc  1.0.1
logstream.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 #ifndef IOX_UTILS_LOG_LOGSTREAM_HPP
17 #define IOX_UTILS_LOG_LOGSTREAM_HPP
18 
19 #include "iceoryx_utils/log/logcommon.hpp"
20 
21 #include <bitset>
22 #include <chrono>
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 
27 namespace iox
28 {
29 namespace log
30 {
31 // helper struct for SFINAE of LogStream& operator<<
32 struct LogHex
33 {
34 };
35 
36 struct LogHex8 : private LogHex
37 {
38  uint8_t value;
39  constexpr LogHex8(uint8_t value)
40  : value(value)
41  {
42  }
43 };
44 
45 struct LogHex16 : private LogHex
46 {
47  uint16_t value;
48  constexpr LogHex16(uint16_t value)
49  : value(value)
50  {
51  }
52 };
53 struct LogHex32 : private LogHex
54 {
55  uint32_t value;
56  constexpr LogHex32(uint32_t value)
57  : value(value)
58  {
59  }
60 };
61 struct LogHex64 : private LogHex
62 {
63  uint64_t value;
64  constexpr LogHex64(uint64_t value)
65  : value(value)
66  {
67  }
68 };
69 
70 // helper struct for SFINAE of LogStream& operator<<
71 struct LogBin
72 {
73 };
74 
75 struct LogBin8 : private LogBin
76 {
77  uint8_t value;
78  constexpr LogBin8(uint8_t value)
79  : value(value)
80  {
81  }
82 };
83 struct LogBin16 : private LogBin
84 {
85  uint16_t value;
86  constexpr LogBin16(uint16_t value)
87  : value(value)
88  {
89  }
90 };
91 struct LogBin32 : private LogBin
92 {
93  uint32_t value;
94  constexpr LogBin32(uint32_t value)
95  : value(value){};
96 };
97 struct LogBin64 : private LogBin
98 {
99  uint64_t value;
100  constexpr LogBin64(uint64_t value)
101  : value(value)
102  {
103  }
104 };
106 {
107  const uint8_t* data;
108  uint8_t size;
109 };
110 
111 class Logger;
112 
114 {
115  public:
116  LogStream(Logger& logger, LogLevel logLevel = LogLevel::kWarn);
117 
118  virtual ~LogStream();
119 
120  void Flush();
121 
122  LogStream& operator<<(const char* cstr) noexcept;
123 
124  LogStream& operator<<(const std::string& str) noexcept;
125 
126  template <typename T, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0>
127  LogStream& operator<<(const T val) noexcept
128  {
129  m_logEntry.message.append(std::to_string(val));
130  m_flushed = false;
131  return *this;
132  }
133 
134  template <typename T, typename std::enable_if<std::is_base_of<LogHex, T>::value, int>::type = 0>
135  LogStream& operator<<(const T val) noexcept
136  {
137  std::stringstream ss;
138  // the '+val' is there to not interpret the uint8_t as char and print the character instead of the hex value
139  ss << "0x" << std::hex << +val.value;
140  m_logEntry.message.append(ss.str());
141  m_flushed = false;
142  return *this;
143  }
144 
145  template <typename T, typename std::enable_if<std::is_base_of<LogBin, T>::value, int>::type = 0>
146  LogStream& operator<<(const T val) noexcept
147  {
148  m_logEntry.message.append("0b");
149  m_logEntry.message.append(std::bitset<std::numeric_limits<decltype(val.value)>::digits>(val.value).to_string());
150  m_flushed = false;
151  return *this;
152  }
153 
154  LogStream& operator<<(const LogRawBuffer& value) noexcept;
155 
156  private:
157  Logger& m_logger;
158  bool m_flushed{false};
159  LogEntry m_logEntry;
160 };
161 
162 LogStream& operator<<(LogStream& out, LogLevel value) noexcept;
163 
164 } // namespace log
165 } // namespace iox
166 
167 #endif // IOX_UTILS_LOG_LOGSTREAM_HPP
Definition: logstream.hpp:114
Definition: logger.hpp:35
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
Definition: logstream.hpp:84
Definition: logstream.hpp:92
Definition: logstream.hpp:98
Definition: logstream.hpp:76
Definition: logstream.hpp:72
Definition: logcommon.hpp:70
Definition: logstream.hpp:46
Definition: logstream.hpp:54
Definition: logstream.hpp:62
Definition: logstream.hpp:37
Definition: logstream.hpp:33
Definition: logstream.hpp:106