• 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 // Bits to allocate for the line number. Defaults to 11 (up to line 2047). If
45 // the line number is too large to be represented by this field, line is
46 // reported as 0.
47 //
48 // Including the line number can slightly increase code size. Without the line
49 // number, the log metadata argument is the same for all logs with the same
50 // level and flags. With the line number, each metadata value is unique and must
51 // be encoded as a separate word in the binary. Systems with extreme space
52 // constraints may exclude line numbers by setting this macro to 0.
53 //
54 // It is possible to include line numbers in tokenized log format strings, but
55 // that is discouraged because line numbers change whenever a file is edited.
56 // Passing the line number with the metadata is a lightweight way to include it.
57 #ifndef PW_LOG_TOKENIZED_LINE_BITS
58 #define PW_LOG_TOKENIZED_LINE_BITS 11
59 #endif  // PW_LOG_TOKENIZED_LINE_BITS
60 
61 // Bits to use for implementation-defined flags.
62 #ifndef PW_LOG_TOKENIZED_FLAG_BITS
63 #define PW_LOG_TOKENIZED_FLAG_BITS 2
64 #endif  // PW_LOG_TOKENIZED_FLAG_BITS
65 
66 // Bits to use for the tokenized version of PW_LOG_MODULE_NAME. Defaults to 16,
67 // which gives a ~1% probability of a collision with 37 module names.
68 #ifndef PW_LOG_TOKENIZED_MODULE_BITS
69 #define PW_LOG_TOKENIZED_MODULE_BITS 16
70 #endif  // PW_LOG_TOKENIZED_MODULE_BITS
71 
72 static_assert((PW_LOG_TOKENIZED_LEVEL_BITS + PW_LOG_TOKENIZED_LINE_BITS +
73                PW_LOG_TOKENIZED_FLAG_BITS + PW_LOG_TOKENIZED_MODULE_BITS) == 32,
74               "Log metadata fields must use 32 bits");
75 
76 // The macro to use to tokenize the log and its arguments. Defaults to
77 // PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD. Projects may define their own
78 // version of this macro that uses a different underlying function, if desired.
79 #ifndef PW_LOG_TOKENIZED_ENCODE_MESSAGE
80 #define PW_LOG_TOKENIZED_ENCODE_MESSAGE \
81   PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD
82 #endif  // PW_LOG_TOKENIZED_ENCODE_MESSAGE
83