microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
log_msg.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
9#include "../microfmt.hpp"
10#include <cstdint>
11#include <reloco/type_id.hpp>
12#include <source_location>
13#include <string_view>
14
15namespace microfmt::log {
16
17// ============================================================================
18// Severity Levels
19// ============================================================================
20
22enum class level : uint8_t { trace = 0, debug, info, warn, err, critical, off };
23
24[[nodiscard]] constexpr microfmt::string_view to_string_view(level lvl) noexcept {
25 switch (lvl) {
26 case level::trace:
27 return "trace";
28 case level::debug:
29 return "debug";
30 case level::info:
31 return "info";
32 case level::warn:
33 return "warn";
34 case level::err:
35 return "error";
36 case level::critical:
37 return "critical";
38 case level::off:
39 default:
40 return "off";
41 }
42}
43
44[[nodiscard]] constexpr microfmt::string_view to_short_string(level lvl) noexcept {
45 switch (lvl) {
46 case level::trace:
47 return "T";
48 case level::debug:
49 return "D";
50 case level::info:
51 return "I";
52 case level::warn:
53 return "W";
54 case level::err:
55 return "E";
56 case level::critical:
57 return "C";
58 case level::off:
59 default:
60 return "O";
61 }
62}
63
64// ============================================================================
65// Log Entry Record (Carries zero-copy metadata)
66// ============================================================================
67
69struct MICROFMT_API_CLASS RELOCO_POINTER log_msg {
70 microfmt::string_view logger_name{};
71 level lvl{level::info};
72 microfmt::instant time{microfmt::instant::now()};
73 microfmt::string_view payload{};
74 // NOLINTNEXTLINE(reloco-banned-std-type): portable C++20 interop type
75 std::source_location loc{std::source_location::current()};
76};
77
78} // namespace microfmt::log
79
80// See <reloco/type_id.hpp> for the full RELOCO_TYPE_ID_NAME rationale;
81// defined here, alongside log_msg's/level's own definitions.
#define MICROFMT_API_CLASS
Definition compat.hpp:181
RELOCO_TYPE_ID_NAME(microfmt::log::log_msg, "microfmt::log::log_msg")
Definition default_log_sink.template.hpp:51
constexpr microfmt::string_view to_short_string(level lvl) noexcept
Definition log_msg.hpp:44
level
Shared log severity levels in ascending order.
Definition log_msg.hpp:22
constexpr microfmt::string_view to_string_view(level lvl) noexcept
Definition log_msg.hpp:24
Structured log record forwarded by a logger to each sink.
Definition log_msg.hpp:69