CVB++ 15.0
Loading...
Searching...
No Matches
log.hpp
1#pragma once
2
3
4#include "module.hpp"
5#include "attribute_map.hpp"
6
7namespace Cvb
8{
9 CVB_BEGIN_INLINE_NS
10
11 namespace Telemetry
12 {
14
19 class Log : public AttributeMap
20 {
21 public:
23
30 static Log Create()
31 {
32 return {};
33 }
34
36
42 static Log Create(LogLevel logLevel)
43 {
44 return Log{logLevel};
45 }
46
47 Log(const Log &) = delete;
48 Log &operator=(const Log &) = delete;
49 Log(Log &&other) noexcept
50 : AttributeMap(std::move(other))
51 , message_(std::move(other.message_))
52 , logLevel_(std::move(other.logLevel_))
53 {
54 }
55
56 Log &operator=(Log &&other) noexcept
57 {
58 if (this != &other)
59 {
60 AttributeMap::operator=(std::move(other));
61 message_ = std::move(other.message_);
62 logLevel_ = std::move(other.logLevel_);
63 }
64 return *this;
65 }
66
67 ~Log()
68 {
69 if (!handle_)
70 return;
71
72 Flush();
73 CExports::CVTSendLog(handle_, message_.c_str());
74 handle_ = nullptr;
75 }
76
78
82 explicit operator bool() const noexcept
83 {
84 return handle_ != nullptr;
85 }
86
87 const std::string& Message() const noexcept
88 {
89 return message_;
90 }
91
92
93 LogLevel Loglevel() const noexcept
94 {
95 return logLevel_;
96 }
97
98 template <class...>
99 using void_t = void;
100
101 template <typename T, typename = void>
102 struct is_ostreamable : std::false_type
103 {
104 };
105
106 template <typename T>
107 struct is_ostreamable<T, void_t<decltype(std::declval<std::ostream &>() << std::declval<T>())>> : std::true_type
108 {
109 };
110
112 template <typename T, typename = typename std::enable_if<is_ostreamable<T>::value>::type>
113 Log &operator<<(const T &value)
114 {
116 oss << value; // Convert the value to a string if it can be output to a stream
117 message_ += oss.str();
118 return *this;
119 }
120
122 {
123 (*this)(kv.first, kv.second);
124 return *this;
125 }
126
127 private:
128 explicit Log(LogLevel logLevel)
129 : AttributeMap()
130 , logLevel_(logLevel)
131 {
132 auto &module = Module::Get();
133 if (!module.WouldLog())
134 return;
135 CExports::CVTLOG handle = nullptr;
136 CExports::CVTCreateLog(module.Handle(), static_cast<CExports::CVTLogLevel>(logLevel), handle);
137 handle_ = handle;
138 }
139
140 Log() noexcept = default;
141
142 LogLevel logLevel_ = LogLevel::DEBUG;
143 std::string message_;
144 };
145 } // namespace Telemetry
146
147 CVB_END_INLINE_NS
148} // namespace Cvb
A attribute map for telemetry data.
Definition attribute_map.hpp:32
A log object to stream messages.
Definition log.hpp:20
Log & operator<<(const T &value)
Operator for types compatible with std::ostream.
Definition log.hpp:113
static Log Create(LogLevel logLevel)
Creates a log with the given log level.
Definition log.hpp:42
static Log Create()
Creates a dummy log.
Definition log.hpp:30
T move(T... args)
Namespace for telemetry.
Definition attribute_map.hpp:16
LogLevel
Log level for telemetry.
Definition telemetry.hpp:31
@ DEBUG
Logs for debugging, these should be avoided especially in a critical code paths.
Definition telemetry.hpp:33
Root namespace for the Image Manager interface.
Definition version.hpp:11