microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
tagged_log_sink_base.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2//
3// SPDX-License-Identifier: BSD-2-Clause
4
5#pragma once
6
23#include "../../microfmt.hpp"
24#include <cstddef>
25#include <string_view>
26
27namespace microfmt::log::detail {
28
36template <std::size_t TagCapacity> class tagged_log_sink_base {
37 static_assert(TagCapacity > 0, "Tag capacity must be at least 1 byte");
38
39public:
44 void set_tag(microfmt::string_view tag) noexcept {
45 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
46
47 tag_size_ = tag.size() < TagCapacity - 1 ? tag.size() : TagCapacity - 1;
48 for (std::size_t i = 0; i < tag_size_; ++i) {
49 tag_[i] = tag[i];
50 }
51 tag_[tag_size_] = '\0';
52
53 RELOCO_END_UNSAFE_BUFFER_USAGE;
54 }
55
56protected:
57 constexpr tagged_log_sink_base() noexcept = default;
58 explicit tagged_log_sink_base(microfmt::string_view tag) noexcept { set_tag(tag); }
59
65 [[nodiscard]] microfmt::string_view resolve_tag(microfmt::string_view logger_name,
66 char (&scratch)[TagCapacity]) const & noexcept RELOCO_LIFETIMEBOUND {
67 if (logger_name.empty()) {
68 return microfmt::string_view(tag_, tag_size_);
69 }
70
71 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
72
73 // The index bound is spelled out in the loop condition (not only via a
74 // precomputed n) so GCC on riscv64 -O3 can prove i < TagCapacity - 1 and
75 // does not emit a false -Wstringop-overflow after unrolling.
76 std::size_t n = 0;
77 while (n < TagCapacity - 1 && n < logger_name.size()) {
78 scratch[n] = logger_name[n];
79 ++n;
80 }
81 scratch[n] = '\0';
82
83 RELOCO_END_UNSAFE_BUFFER_USAGE;
84 return microfmt::string_view(scratch, n);
85 }
86
87 [[nodiscard]] microfmt::string_view resolve_tag(microfmt::string_view logger_name,
88 char (&scratch)[TagCapacity]) const && = delete;
89
90 char tag_[TagCapacity]{};
91 std::size_t tag_size_{0};
92};
93
94} // namespace microfmt::log::detail
CRTP-free base providing fixed-capacity tag storage and truncating tag resolution,...
Definition tagged_log_sink_base.hpp:36
constexpr tagged_log_sink_base() noexcept=default
char tag_[TagCapacity]
Definition tagged_log_sink_base.hpp:90
microfmt::string_view resolve_tag(microfmt::string_view logger_name, char(&scratch)[TagCapacity]) const &noexcept RELOCO_LIFETIMEBOUND
Resolves the tag to use for a record whose originating logger is named logger_name: that name (trunca...
Definition tagged_log_sink_base.hpp:65
std::size_t tag_size_
Definition tagged_log_sink_base.hpp:91
void set_tag(microfmt::string_view tag) noexcept
Replaces the default tag used for records whose logger has no name, truncating to fit if necessary.
Definition tagged_log_sink_base.hpp:44
microfmt::string_view resolve_tag(microfmt::string_view logger_name, char(&scratch)[TagCapacity]) const &&=delete
Fixed-capacity logger that formats records before dispatching to sinks.
Definition default_log_sink.template.hpp:125
Definition assert.hpp:19