13 std::shared_lock lock(other.
mutex_);
18 std::unique_lock lock(other.mutex_);
19 data_ = std::move(other.data_);
32 std::scoped_lock lock(mutex_, other.mutex_);
33 data_ = std::move(other.data_);
45 std::unique_lock lock(
mutex_);
46 data_[std::string(key)] =
entry{std::move(value), category};
51 std::string_view trace_id,
52 std::string_view span_id,
53 std::optional<std::string_view> parent_span_id) {
54 std::unique_lock lock(
mutex_);
64 std::string_view request_id,
65 std::optional<std::string_view> correlation_id) {
66 std::unique_lock lock(
mutex_);
75 std::unique_lock lock(
mutex_);
96 std::shared_lock lock(
mutex_);
97 auto it =
data_.find(std::string(key));
98 if (it !=
data_.end()) {
99 return it->second.value;
105 std::string_view default_value)
const {
106 auto value =
get(key);
108 return std::string(default_value);
110 if (
auto* str = std::get_if<std::string>(&*value)) {
113 return std::string(default_value);
117 std::shared_lock lock(
mutex_);
118 auto it =
data_.find(std::string(key));
119 if (it !=
data_.end()) {
120 return it->second.category;
130 std::shared_lock lock(
mutex_);
131 return data_.find(std::string(key)) !=
data_.end();
135 std::shared_lock lock(
mutex_);
136 return data_.empty();
140 std::shared_lock lock(
mutex_);
145 std::shared_lock lock(
mutex_);
146 std::vector<std::string>
result;
148 for (
const auto& [key, _] :
data_) {
155 std::shared_lock lock(
mutex_);
156 std::vector<std::string>
result;
170 std::unique_lock lock(
mutex_);
171 data_.erase(std::string(key));
175 std::unique_lock lock(
mutex_);
180 std::unique_lock lock(
mutex_);
181 for (
auto it =
data_.begin(); it !=
data_.end();) {
182 if (it->second.category == category) {
183 it =
data_.erase(it);
195 std::shared_lock lock(
mutex_);
199 [&
result, &key](
const auto& val) {
200 using T = std::decay_t<
decltype(val)>;
201 if constexpr (std::is_same_v<T, std::monostate>) {
203 }
else if constexpr (std::is_same_v<T, bool>) {
205 }
else if constexpr (std::is_same_v<T, int64_t>) {
207 }
else if constexpr (std::is_same_v<T, double>) {
209 }
else if constexpr (std::is_same_v<T, std::string>) {
220 for (
const auto& [key,
entry] : other.
data_) {
221 if (overwrite ||
data_.find(key) ==
data_.end()) {
Unified interface for managing all types of logging context.
bool empty() const
Check if the context is empty.
unified_log_context()=default
Default constructor.
unified_log_context & set_trace(std::string_view trace_id, std::string_view span_id, std::optional< std::string_view > parent_span_id=std::nullopt)
Set trace context.
std::vector< std::string > keys() const
Get all keys in the context.
unified_log_context & set_otel(const otlp::otel_context &ctx)
Set OpenTelemetry context.
unified_log_context & merge(const unified_log_context &other, bool overwrite=true)
Merge another context into this one.
unified_log_context & set_request(std::string_view request_id, std::optional< std::string_view > correlation_id=std::nullopt)
Set request context.
bool has(std::string_view key) const
Check if a key exists in the context.
std::optional< context_value > get(std::string_view key) const
Get a context value by key.
size_t size() const
Get the number of entries.
std::optional< context_category > get_category(std::string_view key) const
Get the category of a context entry.
void clear()
Clear all entries from the context.
std::unordered_map< std::string, entry > data_
std::string get_string(std::string_view key, std::string_view default_value="") const
Get a context value as string.
unified_log_context & set(std::string_view key, context_value value, context_category category=context_category::custom)
Set a context value.
void remove(std::string_view key)
Remove a specific key from the context.
log_fields to_fields() const
Export context to log_fields format.
unified_log_context & operator=(const unified_log_context &other)
Copy assignment operator.
std::unordered_map< std::string, log_value > log_fields
Type alias for structured fields map.
std::variant< std::monostate, bool, int64_t, double, std::string > context_value
Value type for unified context storage.
context_category
Categories for organizing context entries.
@ trace
Distributed tracing (trace_id, span_id, parent_span_id)
@ request
Request metadata (request_id, correlation_id)
@ otel
OpenTelemetry specific fields.
OpenTelemetry context for trace correlation.
Internal entry structure.
context_category category
Unified interface for managing all types of logging context.