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 <stdint.h> 17 18 #include "pw_log_tokenized/config.h" 19 #include "pw_preprocessor/util.h" 20 #include "pw_tokenizer/tokenize.h" 21 22 // TODO(hepler): Remove these includes. 23 #ifdef __cplusplus 24 #include "pw_log_tokenized/metadata.h" 25 #endif // __cplusplus 26 27 // This macro implements PW_LOG using pw_tokenizer. Users must implement 28 // pw_log_tokenized_HandleLog(uint32_t metadata, uint8_t* buffer, size_t size). 29 // The log level, module token, and flags are packed into the metadata argument. 30 // 31 // Two strings are tokenized in this macro: 32 // 33 // - The log format string, tokenized in the default tokenizer domain. 34 // - Log module name, masked to 16 bits and tokenized in the 35 // "pw_log_module_names" tokenizer domain. 36 // 37 // To use this macro, implement pw_log_tokenized_HandleLog(), which is defined 38 // in pw_log_tokenized/handler.h. The log metadata can be accessed using 39 // pw::log_tokenized::Metadata. For example: 40 // 41 // extern "C" void pw_log_tokenized_HandleLog( 42 // uint32_t payload, const uint8_t data[], size_t size) { 43 // pw::log_tokenized::Metadata metadata(payload); 44 // 45 // if (metadata.level() >= kLogLevel && ModuleEnabled(metadata.module())) { 46 // EmitLogMessage(data, size, metadata.flags()); 47 // } 48 // } 49 // 50 #define PW_LOG_TOKENIZED_TO_GLOBAL_HANDLER_WITH_PAYLOAD( \ 51 level, module, flags, message, ...) \ 52 do { \ 53 _PW_TOKENIZER_CONST uintptr_t _pw_log_tokenized_module_token = \ 54 PW_TOKENIZE_STRING_MASK("pw_log_module_names", \ 55 ((1u << PW_LOG_TOKENIZED_MODULE_BITS) - 1u), \ 56 module); \ 57 const uintptr_t _pw_log_tokenized_level = level; \ 58 PW_LOG_TOKENIZED_ENCODE_MESSAGE( \ 59 (_PW_LOG_TOKENIZED_LEVEL(_pw_log_tokenized_level) | \ 60 _PW_LOG_TOKENIZED_MODULE(_pw_log_tokenized_module_token) | \ 61 _PW_LOG_TOKENIZED_FLAGS(flags) | _PW_LOG_TOKENIZED_LINE(__LINE__)), \ 62 PW_LOG_TOKENIZED_FORMAT_STRING(module, message), \ 63 __VA_ARGS__); \ 64 } while (0) 65 66 // If the level field is present, clamp it to the maximum value. 67 #if PW_LOG_TOKENIZED_LEVEL_BITS == 0 68 #define _PW_LOG_TOKENIZED_LEVEL(value) ((uintptr_t)0) 69 #else 70 #define _PW_LOG_TOKENIZED_LEVEL(value) \ 71 (value < ((uintptr_t)1 << PW_LOG_TOKENIZED_LEVEL_BITS) \ 72 ? value \ 73 : ((uintptr_t)1 << PW_LOG_TOKENIZED_LEVEL_BITS) - 1) 74 #endif // PW_LOG_TOKENIZED_LEVEL_BITS 75 76 // If the line number field is present, shift it to its position. Set it to zero 77 // if the line number is too large for PW_LOG_TOKENIZED_LINE_BITS. 78 #if PW_LOG_TOKENIZED_LINE_BITS == 0 79 #define _PW_LOG_TOKENIZED_LINE(line) ((uintptr_t)0) 80 #else 81 #define _PW_LOG_TOKENIZED_LINE(line) \ 82 ((uintptr_t)(line < (1 << PW_LOG_TOKENIZED_LINE_BITS) ? line : 0) \ 83 << PW_LOG_TOKENIZED_LEVEL_BITS) 84 #endif // PW_LOG_TOKENIZED_LINE_BITS 85 86 // If the flags field is present, mask it and shift it to its position. 87 #if PW_LOG_TOKENIZED_FLAG_BITS == 0 88 #define _PW_LOG_TOKENIZED_FLAGS(value) ((uintptr_t)0) 89 #else 90 #define _PW_LOG_TOKENIZED_FLAGS(value) \ 91 (((uintptr_t)(value) & (((uintptr_t)1 << PW_LOG_TOKENIZED_FLAG_BITS) - 1)) \ 92 << (PW_LOG_TOKENIZED_LEVEL_BITS + PW_LOG_TOKENIZED_LINE_BITS)) 93 #endif // PW_LOG_TOKENIZED_FLAG_BITS 94 95 // If the module field is present, shift it to its position. 96 #if PW_LOG_TOKENIZED_MODULE_BITS == 0 97 #define _PW_LOG_TOKENIZED_MODULE(value) ((uintptr_t)0) 98 #else 99 #define _PW_LOG_TOKENIZED_MODULE(value) \ 100 ((uintptr_t)(value) << ((PW_LOG_TOKENIZED_LEVEL_BITS + \ 101 PW_LOG_TOKENIZED_LINE_BITS + \ 102 PW_LOG_TOKENIZED_FLAG_BITS))) 103 #endif // PW_LOG_TOKENIZED_MODULE_BITS 104 105 #define PW_LOG_TOKENIZED_ENCODE_MESSAGE(metadata, format, ...) \ 106 do { \ 107 PW_TOKENIZE_FORMAT_STRING( \ 108 PW_TOKENIZER_DEFAULT_DOMAIN, UINT32_MAX, format, __VA_ARGS__); \ 109 _pw_log_tokenized_EncodeTokenizedLog(metadata, \ 110 _pw_tokenizer_token, \ 111 PW_TOKENIZER_ARG_TYPES(__VA_ARGS__) \ 112 PW_COMMA_ARGS(__VA_ARGS__)); \ 113 } while (0) 114 115 PW_EXTERN_C_START 116 117 void _pw_log_tokenized_EncodeTokenizedLog(uint32_t metadata, 118 pw_tokenizer_Token token, 119 pw_tokenizer_ArgTypes types, 120 ...); 121 122 PW_EXTERN_C_END 123