• 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 
15 #define PW_LOG_MODULE_NAME "This is the log module name!"
16 
17 // Create a fake version of the tokenization macro.
18 #undef PW_LOG_TOKENIZED_ENCODE_MESSAGE
19 #define PW_LOG_TOKENIZED_ENCODE_MESSAGE(payload, message, ...) \
20   CaptureTokenizerArgs(payload,                                \
21                        PW_MACRO_ARG_COUNT(__VA_ARGS__),        \
22                        message PW_COMMA_ARGS(__VA_ARGS__))
23 
24 #include <cstring>
25 #include <string_view>
26 #include <tuple>
27 
28 #include "gtest/gtest.h"
29 #include "pw_log_tokenized/log_tokenized.h"
30 #include "pw_preprocessor/arguments.h"
31 #include "pw_preprocessor/compiler.h"
32 
33 namespace pw::log_tokenized {
34 namespace {
35 
36 struct {
37   Metadata metadata = Metadata(0);
38   const char* format_string = "";
39   size_t arg_count = 0;
40 } last_log{};
41 
42 void CaptureTokenizerArgs(pw_tokenizer_Payload payload,
43                           size_t arg_count,
44                           const char* message,
45                           ...) PW_PRINTF_FORMAT(3, 4);
46 
CaptureTokenizerArgs(pw_tokenizer_Payload payload,size_t arg_count,const char * message,...)47 void CaptureTokenizerArgs(pw_tokenizer_Payload payload,
48                           size_t arg_count,
49                           const char* message,
50                           ...) {
51   last_log.metadata = payload;
52   last_log.format_string = message;
53   last_log.arg_count = arg_count;
54 }
55 
56 constexpr uintptr_t kModuleToken =
57     PW_TOKENIZER_STRING_TOKEN(PW_LOG_MODULE_NAME) &
58     ((1u << PW_LOG_TOKENIZED_MODULE_BITS) - 1);
59 
60 constexpr Metadata test1 = Metadata::Set<0, 0, 0>();
61 static_assert(test1.level() == 0);
62 static_assert(test1.module() == 0);
63 static_assert(test1.flags() == 0);
64 
65 constexpr Metadata test2 = Metadata::Set<3, 2, 1>();
66 static_assert(test2.level() == 3);
67 static_assert(test2.module() == 2);
68 static_assert(test2.flags() == 1);
69 
70 constexpr Metadata test3 = Metadata::Set<63, 65535, 1023>();
71 static_assert(test3.level() == 63);
72 static_assert(test3.module() == 65535);
73 static_assert(test3.flags() == 1023);
74 
TEST(LogTokenized,LogMetadata_Zero)75 TEST(LogTokenized, LogMetadata_Zero) {
76   PW_LOG_TOKENIZED_TO_GLOBAL_HANDLER_WITH_PAYLOAD(0, 0, "hello");
77   EXPECT_EQ(last_log.metadata.level(), 0u);
78   EXPECT_EQ(last_log.metadata.flags(), 0u);
79   EXPECT_EQ(last_log.metadata.module(), kModuleToken);
80   EXPECT_EQ(last_log.arg_count, 0u);
81 }
82 
TEST(LogTokenized,LogMetadata_DifferentValues)83 TEST(LogTokenized, LogMetadata_DifferentValues) {
84   PW_LOG_TOKENIZED_TO_GLOBAL_HANDLER_WITH_PAYLOAD(55, 36, "hello%s", "?");
85   EXPECT_EQ(last_log.metadata.level(), 55u);
86   EXPECT_EQ(last_log.metadata.flags(), 36u);
87   EXPECT_EQ(last_log.metadata.module(), kModuleToken);
88   EXPECT_EQ(last_log.arg_count, 1u);
89 }
90 
TEST(LogTokenized,LogMetadata_MaxValues)91 TEST(LogTokenized, LogMetadata_MaxValues) {
92   PW_LOG_TOKENIZED_TO_GLOBAL_HANDLER_WITH_PAYLOAD(63, 1023, "hello %d", 1);
93   EXPECT_EQ(last_log.metadata.level(), 63u);
94   EXPECT_EQ(last_log.metadata.flags(), 1023u);
95   EXPECT_EQ(last_log.metadata.module(), kModuleToken);
96   EXPECT_EQ(last_log.arg_count, 1u);
97 }
98 
99 }  // namespace
100 }  // namespace pw::log_tokenized
101