Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #pragma once
16 : :
17 : : #include <cstdint>
18 : : #include <span>
19 : :
20 : : namespace hpactor::log {
21 : :
22 : : inline constexpr uint8_t kMaxLogFields = 4;
23 : :
24 : : enum class LogFieldType : uint8_t {
25 : : kInt64,
26 : : kUInt64,
27 : : kDouble,
28 : : kBool,
29 : : kStringLiteral,
30 : : kPointer,
31 : : };
32 : :
33 : : struct LogField {
34 : : const char* name;
35 : : LogFieldType type;
36 : : union {
37 : : int64_t i64;
38 : : uint64_t u64;
39 : : double f64;
40 : : bool boolean;
41 : : const char* str;
42 : : const void* ptr;
43 : : } value;
44 : : };
45 : :
46 : : inline LogField field(const char* name, int64_t value) noexcept {
47 : : LogField f{};
48 : : f.name = name;
49 : : f.type = LogFieldType::kInt64;
50 : : f.value.i64 = value;
51 : : return f;
52 : : }
53 : :
54 : 416 : inline LogField field(const char* name, uint64_t value) noexcept {
55 : 416 : LogField f{};
56 : 416 : f.name = name;
57 : 416 : f.type = LogFieldType::kUInt64;
58 : 416 : f.value.u64 = value;
59 : 416 : return f;
60 : : }
61 : :
62 : : inline LogField field(const char* name, double value) noexcept {
63 : : LogField f{};
64 : : f.name = name;
65 : : f.type = LogFieldType::kDouble;
66 : : f.value.f64 = value;
67 : : return f;
68 : : }
69 : :
70 : : inline LogField field(const char* name, bool value) noexcept {
71 : : LogField f{};
72 : : f.name = name;
73 : : f.type = LogFieldType::kBool;
74 : : f.value.boolean = value;
75 : : return f;
76 : : }
77 : :
78 : 116 : inline LogField field_lit(const char* name, const char* value) noexcept {
79 : 116 : LogField f{};
80 : 116 : f.name = name;
81 : 116 : f.type = LogFieldType::kStringLiteral;
82 : 116 : f.value.str = value;
83 : 116 : return f;
84 : : }
85 : :
86 : : inline LogField field_ptr(const char* name, const void* value) noexcept {
87 : : LogField f{};
88 : : f.name = name;
89 : : f.type = LogFieldType::kPointer;
90 : : f.value.ptr = value;
91 : : return f;
92 : : }
93 : :
94 : : } // namespace hpactor::log
|