C/C++ API Reference
Loading...
Searching...
No Matches
metadata.h
1// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstdint>
17
18#include "pw_log_tokenized/config.h"
19
20namespace pw {
21namespace log_tokenized {
22namespace internal {
23
24// Internal class for managing the metadata bit fields.
25template <typename T, unsigned kBits, unsigned kShift>
26struct BitField {
27 public:
28 static constexpr T Get(T value) { return (value >> kShift) & kMask; }
29 static constexpr T Shift(T value) {
30 return (value <= kMask ? value : T(0)) << kShift;
31 }
32
33 private:
34 static constexpr T kMask = (T(1) << kBits) - 1;
35};
36
37template <typename T, unsigned kShift>
38class BitField<T, 0, kShift> {
39 public:
40 static constexpr T Get(T) { return 0; }
41 static constexpr T Shift(T) { return 0; }
42};
43
44} // namespace internal
45
47
48// This class, which is aliased to pw::log_tokenized::Metadata below, is used to
49// access the log metadata packed into the tokenizer's payload argument.
50//
57template <unsigned kLevelBits,
58 unsigned kLineBits,
59 unsigned kFlagBits,
60 unsigned kModuleBits,
61 typename T = uintptr_t>
63 public:
64 template <T log_level = 0, T module = 0, T flags = 0, T line = 0>
65 static constexpr GenericMetadata Set() {
66 static_assert(log_level < (1 << kLevelBits), "The level is too large!");
67 static_assert(line < (1 << kLineBits), "The line number is too large!");
68 static_assert(flags < (1 << kFlagBits), "The flags are too large!");
69 static_assert(module < (1 << kModuleBits), "The module is too large!");
70
71 return GenericMetadata(BitsFromMetadata(log_level, module, flags, line));
72 }
73
77 constexpr GenericMetadata(T log_level, T module, T flags, T line)
78 : value_(BitsFromMetadata(log_level, module, flags, line)) {}
79
80 constexpr GenericMetadata(T value) : value_(value) {}
81
83 constexpr T level() const { return Level::Get(value_); }
84
87 constexpr T line_number() const { return Line::Get(value_); }
88
90 constexpr T flags() const { return Flags::Get(value_); }
91
94 constexpr T module() const { return Module::Get(value_); }
95
97 constexpr T value() const { return value_; }
98
99 private:
103 using Module =
105
106 static constexpr T BitsFromMetadata(T log_level, T module, T flags, T line) {
107 return Level::Shift(log_level) | Module::Shift(module) |
108 Flags::Shift(flags) | Line::Shift(line);
109 }
110
111 T value_;
112
113 static_assert(kLevelBits + kLineBits + kFlagBits + kModuleBits <=
114 sizeof(value_) * 8);
115};
116
127
128} // namespace log_tokenized
129} // namespace pw
Definition: metadata.h:62
#define PW_LOG_TOKENIZED_LINE_BITS
Definition: config.h:87
constexpr T flags() const
The flags provided to the log call.
Definition: metadata.h:90
constexpr T level() const
The log level of this message.
Definition: metadata.h:83
constexpr T line_number() const
Definition: metadata.h:87
constexpr T value() const
The underlying packed metadata.
Definition: metadata.h:97
constexpr GenericMetadata(T log_level, T module, T flags, T line)
Definition: metadata.h:77
#define PW_LOG_TOKENIZED_MODULE_BITS
Definition: config.h:99
#define PW_LOG_TOKENIZED_LEVEL_BITS
Definition: config.h:69
constexpr T module() const
Definition: metadata.h:94
#define PW_LOG_TOKENIZED_FLAG_BITS
Bits to use for implementation-defined flags. Defaults to 2.
Definition: config.h:92
The Pigweed namespace.
Definition: alignment.h:27
Definition: metadata.h:26