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