C/C++ API Reference
Loading...
Searching...
No Matches
config.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 "pw_log/levels.h"
17#include "pw_log/options.h"
18#include "pw_polyfill/static_assert.h"
19#include "pw_tokenizer/config.h"
20
22
23// The size of the stack-allocated argument encoding buffer to use by default.
24// A buffer of this size is allocated and used for the 4-byte token and for
25// encoding all arguments. It must be at least large enough for the token (4
26// bytes).
27//
28// This buffer does not need to be large to accommodate a good number of
29// tokenized string arguments. Integer arguments are usually encoded smaller
30// than their native size (e.g. 1 or 2 bytes for smaller numbers). All floating
31// point types are encoded as four bytes. Null-terminated strings are encoded
32// 1:1 in size, however, and can quickly fill up this buffer.
33#ifndef PW_LOG_TOKENIZED_ENCODING_BUFFER_SIZE_BYTES
34#define PW_LOG_TOKENIZED_ENCODING_BUFFER_SIZE_BYTES \
35 PW_TOKENIZER_CFG_ENCODING_BUFFER_SIZE_BYTES
36#endif // PW_LOG_TOKENIZED_ENCODING_BUFFER_SIZE_BYTES
37
38#define PW_LOG_TOKENIZED_FIELD_PREFIX "■"
39#define PW_LOG_TOKENIZED_KEY_VALUE_SEPARATOR "♦"
40
41// This macro takes the PW_LOG format string and optionally transforms it. By
42// default, pw_log_tokenized specifies three fields as key-value pairs.
43#ifndef PW_LOG_TOKENIZED_FORMAT_STRING
44
45#define _PW_LOG_TOKENIZED_FIELD(name, contents) \
46 PW_LOG_TOKENIZED_FIELD_PREFIX name PW_LOG_TOKENIZED_KEY_VALUE_SEPARATOR \
47 contents
48
54#define PW_LOG_TOKENIZED_FORMAT_STRING(module, message) \
55 _PW_LOG_TOKENIZED_FIELD("msg", message) \
56 _PW_LOG_TOKENIZED_FIELD("module", module) \
57 _PW_LOG_TOKENIZED_FIELD("file", __FILE__)
58
59#endif // PW_LOG_TOKENIZED_FORMAT_STRING
60
61// The log level, line number, flag bits, and module token are packed into the
62// tokenizer's payload argument, which is typically 32 bits. These macros
63// specify the number of bits to use for each field. A field with zero bits is
64// excluded.
65
67#ifndef PW_LOG_TOKENIZED_LEVEL_BITS
68#define PW_LOG_TOKENIZED_LEVEL_BITS PW_LOG_LEVEL_BITS
69#endif // PW_LOG_TOKENIZED_LEVEL_BITS
70
85#ifndef PW_LOG_TOKENIZED_LINE_BITS
86#define PW_LOG_TOKENIZED_LINE_BITS 11
87#endif // PW_LOG_TOKENIZED_LINE_BITS
88
90#ifndef PW_LOG_TOKENIZED_FLAG_BITS
91#define PW_LOG_TOKENIZED_FLAG_BITS 2
92#endif // PW_LOG_TOKENIZED_FLAG_BITS
93
96#ifndef PW_LOG_TOKENIZED_MODULE_BITS
97#define PW_LOG_TOKENIZED_MODULE_BITS 16
98#endif // PW_LOG_TOKENIZED_MODULE_BITS
99
102 "Log metadata fields must use 32 bits");
103
104// If the level field is present, clamp it to the maximum value.
105#if PW_LOG_TOKENIZED_LEVEL_BITS == 0
106#define _PW_LOG_TOKENIZED_LEVEL(value) ((uintptr_t)0)
107#else
108#define _PW_LOG_TOKENIZED_LEVEL(value) \
109 (value < ((uintptr_t)1 << PW_LOG_TOKENIZED_LEVEL_BITS) \
110 ? value \
111 : ((uintptr_t)1 << PW_LOG_TOKENIZED_LEVEL_BITS) - 1)
112#endif // PW_LOG_TOKENIZED_LEVEL_BITS
113
114// If the line number field is present, shift it to its position. Set it to zero
115// if the line number is too large for PW_LOG_TOKENIZED_LINE_BITS.
116#if PW_LOG_TOKENIZED_LINE_BITS == 0
117#define _PW_LOG_TOKENIZED_LINE(line) ((uintptr_t)0)
118#else
119#define _PW_LOG_TOKENIZED_LINE(line) \
120 ((uintptr_t)(line < (1 << PW_LOG_TOKENIZED_LINE_BITS) ? line : 0) \
121 << PW_LOG_TOKENIZED_LEVEL_BITS)
122#endif // PW_LOG_TOKENIZED_LINE_BITS
123
124// If the flags field is present, mask it and shift it to its position.
125#if PW_LOG_TOKENIZED_FLAG_BITS == 0
126#define _PW_LOG_TOKENIZED_FLAGS(value) ((uintptr_t)0)
127#else
128#define _PW_LOG_TOKENIZED_FLAGS(value) \
129 (((uintptr_t)(value) & (((uintptr_t)1 << PW_LOG_TOKENIZED_FLAG_BITS) - 1)) \
130 << (PW_LOG_TOKENIZED_LEVEL_BITS + PW_LOG_TOKENIZED_LINE_BITS))
131#endif // PW_LOG_TOKENIZED_FLAG_BITS
132
133// If the module field is present, shift it to its position.
134#if PW_LOG_TOKENIZED_MODULE_BITS == 0
135#define _PW_LOG_TOKENIZED_MODULE(value) ((uintptr_t)0)
136#else
137#define _PW_LOG_TOKENIZED_MODULE(value) \
138 ((uintptr_t)(value) << ((PW_LOG_TOKENIZED_LEVEL_BITS + \
139 PW_LOG_TOKENIZED_LINE_BITS + \
140 PW_LOG_TOKENIZED_FLAG_BITS)))
141#endif // PW_LOG_TOKENIZED_MODULE_BITS
142
143#ifdef __cplusplus
144
145#include <cstddef>
146
147namespace pw::log_tokenized {
148
149// C++ constant for the encoding buffer size. Use this instead of the macro.
150inline constexpr size_t kEncodingBufferSizeBytes =
151 PW_LOG_TOKENIZED_ENCODING_BUFFER_SIZE_BYTES;
152
153} // namespace pw::log_tokenized
154
155#endif // __cplusplus
#define PW_LOG_TOKENIZED_LINE_BITS
Definition: config.h:86
#define PW_LOG_TOKENIZED_MODULE_BITS
Definition: config.h:97
#define PW_LOG_TOKENIZED_LEVEL_BITS
Bits to allocate for the log level. Defaults to PW_LOG_LEVEL_BITS (3).
Definition: config.h:68
#define PW_LOG_TOKENIZED_FLAG_BITS
Bits to use for implementation-defined flags. Defaults to 2.
Definition: config.h:91