userver: userver/logging/log_extra.hpp Source File
Loading...
Searching...
No Matches
log_extra.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/logging/log_extra.hpp
4/// @brief @copybrief logging::LogExtra
5
6#include <initializer_list>
7#include <string>
8#include <utility>
9#include <variant>
10
11#include <boost/container/container_fwd.hpp>
12
13#include <userver/compiler/select.hpp>
14#include <userver/logging/fwd.hpp>
15#include <userver/utils/fast_pimpl.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace tracing {
20
21class TagScope;
22
23class Span;
24
25} // namespace tracing
26
27namespace logging {
28
29class LogHelper;
30
31namespace impl {
32class TagWriter;
33} // namespace impl
34
35/// Extra tskv fields storage
36class LogExtra final {
37public:
38 using Value =
39 std::variant<std::string, int, long, long long, unsigned int, unsigned long, unsigned long long, float, double>;
40 using Key = std::string;
41 using Pair = std::pair<Key, Value>;
42
43 /// Specifies replacement policy for newly added values
44 enum class ExtendType {
45 kNormal, ///< Added value can be replaced
46 kFrozen, ///< Attempts to replace this value will be ignored
47 };
48
49 LogExtra() noexcept;
50
51 LogExtra(const LogExtra&);
52
53 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
54 LogExtra(LogExtra&&);
55
56 ~LogExtra();
57
58 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
59 LogExtra& operator=(LogExtra&&);
60
61 LogExtra& operator=(const LogExtra&);
62
63 /// Constructs LogExtra containing an initial batch of key-value pairs
64 LogExtra(std::initializer_list<Pair> initial, ExtendType extend_type = ExtendType::kNormal);
65
66 /// Adds a single key-value pair
67 void Extend(std::string key, Value value, ExtendType extend_type = ExtendType::kNormal);
68
69 /// Adds a single key-value pair
70 void Extend(Pair extra, ExtendType extend_type = ExtendType::kNormal);
71
72 /// Adds a batch of key-value pairs
73 void Extend(std::initializer_list<Pair> extra, ExtendType extend_type = ExtendType::kNormal);
74
75 /// @brief Merges contents of other LogExtra with existing key-value pairs
76 /// preserving freeze states
77 void Extend(const LogExtra& extra);
78
79 /// @brief Merges contents of other LogExtra with existing key-value pairs
80 /// preserving freeze states
81 void Extend(LogExtra&& extra);
82
83 /// @brief Creates a LogExtra with current thread's stacktrace if the default
84 /// log level is less or equal to DEBUG
85 static LogExtra StacktraceNocache() noexcept;
86
87 /// @brief Creates a LogExtra with current thread's stacktrace if the logger
88 /// log level is less or equal to DEBUG
89 static LogExtra StacktraceNocache(logging::LoggerRef logger) noexcept;
90
91 /// @brief Creates a LogExtra with current thread's stacktrace if the logger
92 /// log level is less or equal to DEBUG. Uses cache for
93 /// faster stacktrace symbolization.
94 static LogExtra Stacktrace() noexcept;
95
96 /// @brief Creates a LogExtra with current thread's stacktrace if the logger
97 /// log level is less or equal to DEBUG. Uses cache for
98 /// faster stacktrace symbolization.
99 static LogExtra Stacktrace(logging::LoggerRef logger) noexcept;
100
101 /// @brief Adds a range of key-value pairs
102 template <typename Iterator>
103 void ExtendRange(Iterator first, Iterator last, ExtendType extend_type = ExtendType::kNormal) {
104 // NOTE: must replace existing rewritable values
105 for (Iterator it = first; it != last; ++it) Extend(*it, extend_type);
106 }
107
108 /// @brief Marks specified value as frozen, all attempts to overwrite it will
109 /// be silently ignored.
110 void SetFrozen(const std::string& key);
111
112 friend class impl::TagWriter;
113 friend class tracing::Span;
114 friend class tracing::TagScope;
115
116private:
117 const Value& GetValue(std::string_view key) const;
118
119 class ProtectedValue final {
120 public:
121 ProtectedValue() = default;
122 ProtectedValue(Value value, bool frozen);
123 ProtectedValue(const ProtectedValue& other) = default;
124 ProtectedValue(ProtectedValue&& other) = default;
125
126 ProtectedValue& operator=(const ProtectedValue& other);
127 ProtectedValue& operator=(ProtectedValue&& other) noexcept;
128
129 bool IsFrozen() const;
130 void SetFrozen();
131 Value& GetValue() { return value_; }
132 const Value& GetValue() const { return value_; }
133 void AssignIgnoringFrozenness(ProtectedValue other);
134
135 private:
136 Value value_;
137 bool frozen_ = false;
138 };
139
140 static constexpr std::size_t kSmallVectorSize = 24;
141 static constexpr std::size_t kPimplSize =
142 compiler::SelectSize().ForLibCpp32(1168).ForLibCpp64(1560).ForLibStdCpp64(1944).ForLibStdCpp32(1356);
143 using MapItem = std::pair<Key, ProtectedValue>;
144 using Map = boost::container::small_vector<MapItem, kSmallVectorSize>;
145
146 void Extend(std::string key, ProtectedValue protected_value, ExtendType extend_type = ExtendType::kNormal);
147
148 void Extend(MapItem extra, ExtendType extend_type = ExtendType::kNormal);
149
150 std::pair<Key, ProtectedValue>* Find(std::string_view);
151
152 const std::pair<Key, ProtectedValue>* Find(std::string_view) const;
153
154 utils::FastPimpl<Map, kPimplSize, alignof(void*)> extra_;
155};
156
157extern const LogExtra kEmptyLogExtra;
158
159} // namespace logging
160
161USERVER_NAMESPACE_END