xref: /aosp_15_r20/external/pigweed/pw_log_tokenized/public/pw_log_tokenized/gcc_partially_tokenized.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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_tokenized/log_tokenized.h"
17 
18 #if !defined(__cplusplus) || !defined(__GNUC__) || defined(__clang__)
19 
20 // If we're not compiling C++ or we're not using GCC, then tokenize the log.
21 #define PW_HANDLE_LOG PW_LOG_TOKENIZED_TO_GLOBAL_HANDLER_WITH_PAYLOAD
22 
23 #else  // defined(__cplusplus) && defined(__GNUC__) && !defined(__clang__)
24 
25 #include <string_view>
26 
27 #include "pw_log_string/handler.h"
28 
29 // GCC has a bug resulting in section attributes of templated functions being
30 // ignored. This in turn means that log tokenization cannot work for templated
31 // functions, because the token database entries are lost at build time.
32 // For more information see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70435
33 //
34 // To work around this, we defer to string logging only in templated contexts.
35 //
36 // __PRETTY_FUNCTION__ is suffixed with ' [with ...]' for templated contexts
37 // if the class and/or function is templated. For example:
38 // "Foo() [with T = char]".
39 #define PW_HANDLE_LOG(...)                                               \
40   do {                                                                   \
41     if constexpr (std::string_view(__PRETTY_FUNCTION__).back() == ']') { \
42       PW_LOG_STRING_HANDLE_MESSAGE(__VA_ARGS__);                         \
43     } else {                                                             \
44       PW_LOG_TOKENIZED_TO_GLOBAL_HANDLER_WITH_PAYLOAD(__VA_ARGS__);      \
45     }                                                                    \
46   } while (0)
47 
48 #endif  // !defined(__cplusplus) || !defined(__GNUC__) || defined(__clang__)
49