• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <assert.h>
17 
18 #include "pw_log/levels.h"
19 #include "pw_log/options.h"
20 
21 // This macro takes the PW_LOG format string and optionally transforms it. By
22 // default, pw_log_tokenized specifies three fields as key-value pairs.
23 #ifndef PW_LOG_TOKENIZED_FORMAT_STRING
24 
25 #define _PW_LOG_TOKENIZED_FIELD(name, contents) "■" name "♦" contents
26 
27 #define PW_LOG_TOKENIZED_FORMAT_STRING(string)          \
28   _PW_LOG_TOKENIZED_FIELD("msg", string)                \
29   _PW_LOG_TOKENIZED_FIELD("module", PW_LOG_MODULE_NAME) \
30   _PW_LOG_TOKENIZED_FIELD("file", __FILE__)
31 
32 #endif  // PW_LOG_TOKENIZED_FORMAT_STRING
33 
34 // The log level, line number, flag bits, and module token are packed into the
35 // tokenizer's payload argument, which is typically 32 bits. These macros
36 // specify the number of bits to use for each field. A field with zero bits is
37 // excluded.
38 
39 // Bits to allocate for the log level.
40 #ifndef PW_LOG_TOKENIZED_LEVEL_BITS
41 #define PW_LOG_TOKENIZED_LEVEL_BITS PW_LOG_LEVEL_BITS
42 #endif  // PW_LOG_TOKENIZED_LEVEL_BITS
43 
44 // Including the line number can slightly increase code size. Without the line
45 // number, the log metadata argument is the same for all logs with the same
46 // level and flags. With the line number, each metadata value is unique and must
47 // be encoded as a separate word in the binary. Systems with extreme space
48 // constraints may exclude line numbers by setting this macro to 0.
49 //
50 // It is possible to include line numbers in tokenized log format strings, but
51 // that is discouraged because line numbers change whenever a file is edited.
52 // Passing the line number with the metadata is a lightweight way to include it.
53 #ifndef PW_LOG_TOKENIZED_LINE_BITS
54 #define PW_LOG_TOKENIZED_LINE_BITS 11
55 #endif  // PW_LOG_TOKENIZED_LINE_BITS
56 
57 // Bits to use for implementation-defined flags.
58 #ifndef PW_LOG_TOKENIZED_FLAG_BITS
59 #define PW_LOG_TOKENIZED_FLAG_BITS 2
60 #endif  // PW_LOG_TOKENIZED_FLAG_BITS
61 
62 // Bits to use for the tokenized version of PW_LOG_MODULE_NAME. Defaults to 16,
63 // which gives a ~1% probability of a collision with 37 module names.
64 #ifndef PW_LOG_TOKENIZED_MODULE_BITS
65 #define PW_LOG_TOKENIZED_MODULE_BITS 16
66 #endif  // PW_LOG_TOKENIZED_MODULE_BITS
67 
68 static_assert((PW_LOG_TOKENIZED_LEVEL_BITS + PW_LOG_TOKENIZED_LINE_BITS +
69                PW_LOG_TOKENIZED_FLAG_BITS + PW_LOG_TOKENIZED_MODULE_BITS) == 32,
70               "Log metadata fields must use 32 bits");
71