Pigweed
 
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
21// The size of the stack-allocated argument encoding buffer to use by default.
22// A buffer of this size is allocated and used for the 4-byte token and for
23// encoding all arguments. It must be at least large enough for the token (4
24// bytes).
25//
26// This buffer does not need to be large to accommodate a good number of
27// tokenized string arguments. Integer arguments are usually encoded smaller
28// than their native size (e.g. 1 or 2 bytes for smaller numbers). All floating
29// point types are encoded as four bytes. Null-terminated strings are encoded
30// 1:1 in size, however, and can quickly fill up this buffer.
31#ifndef PW_LOG_TOKENIZED_ENCODING_BUFFER_SIZE_BYTES
32#define PW_LOG_TOKENIZED_ENCODING_BUFFER_SIZE_BYTES \
33 PW_TOKENIZER_CFG_ENCODING_BUFFER_SIZE_BYTES
34#endif // PW_LOG_TOKENIZED_ENCODING_BUFFER_SIZE_BYTES
35
36// This macro takes the PW_LOG format string and optionally transforms it. By
37// default, pw_log_tokenized specifies three fields as key-value pairs.
38#ifndef PW_LOG_TOKENIZED_FORMAT_STRING
39
40#define _PW_LOG_TOKENIZED_FIELD(name, contents) "■" name "♦" contents
41
47#define PW_LOG_TOKENIZED_FORMAT_STRING(module, message) \
48 _PW_LOG_TOKENIZED_FIELD("msg", message) \
49 _PW_LOG_TOKENIZED_FIELD("module", module) \
50 _PW_LOG_TOKENIZED_FIELD("file", __FILE__)
51
52#endif // PW_LOG_TOKENIZED_FORMAT_STRING
53
54// The log level, line number, flag bits, and module token are packed into the
55// tokenizer's payload argument, which is typically 32 bits. These macros
56// specify the number of bits to use for each field. A field with zero bits is
57// excluded.
58
61#ifndef PW_LOG_TOKENIZED_LEVEL_BITS
62#define PW_LOG_TOKENIZED_LEVEL_BITS PW_LOG_LEVEL_BITS
63#endif // PW_LOG_TOKENIZED_LEVEL_BITS
64
79#ifndef PW_LOG_TOKENIZED_LINE_BITS
80#define PW_LOG_TOKENIZED_LINE_BITS 11
81#endif // PW_LOG_TOKENIZED_LINE_BITS
82
84#ifndef PW_LOG_TOKENIZED_FLAG_BITS
85#define PW_LOG_TOKENIZED_FLAG_BITS 2
86#endif // PW_LOG_TOKENIZED_FLAG_BITS
87
91#ifndef PW_LOG_TOKENIZED_MODULE_BITS
92#define PW_LOG_TOKENIZED_MODULE_BITS 16
93#endif // PW_LOG_TOKENIZED_MODULE_BITS
94
95static_assert((PW_LOG_TOKENIZED_LEVEL_BITS + PW_LOG_TOKENIZED_LINE_BITS +
96 PW_LOG_TOKENIZED_FLAG_BITS + PW_LOG_TOKENIZED_MODULE_BITS) == 32,
97 "Log metadata fields must use 32 bits");
98
99#ifdef __cplusplus
100
101#include <cstddef>
102
103namespace pw::log_tokenized {
104
105// C++ constant for the encoding buffer size. Use this instead of the macro.
106inline constexpr size_t kEncodingBufferSizeBytes =
107 PW_LOG_TOKENIZED_ENCODING_BUFFER_SIZE_BYTES;
108
109} // namespace pw::log_tokenized
110
111#endif // __cplusplus