• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 // This is test verifies that the logging backend:
16 //
17 // - Compiles as plain C
18 // - Runs when compiled as C
19 //
20 // Unfortunately, since we do not know where the log sink actually goes, the
21 // logging functionality itself is not verified beyond compiling and running.
22 
23 #define PW_LOG_MODULE_NAME "CTS"
24 
25 #include "pw_log/log.h"
26 
27 #ifdef __cplusplus
28 #error "This file must be compiled as plain C to verify C compilation works."
29 #endif  // __cplusplus
30 
LoggingFromFunctionPlainC(void)31 static void LoggingFromFunctionPlainC(void) { PW_LOG_INFO("From a function!"); }
32 
33 static void CustomFormatStringTest(void);
34 
BasicLogTestPlainC(void)35 void BasicLogTestPlainC(void) {
36   int n = 3;
37 
38   // Debug level
39   PW_LOG_DEBUG("This log statement should be at DEBUG level; no args");
40   for (int i = 0; i < n; ++i) {
41     PW_LOG_DEBUG("Counting: %d", i);
42   }
43   PW_LOG_DEBUG("Here is a string: %s; with another string %s", "foo", "bar");
44 
45   // Info level
46   PW_LOG_INFO("This log statement should be at INFO level; no args");
47   for (int i = 0; i < n; ++i) {
48     PW_LOG_INFO("Counting: %d", i);
49   }
50   PW_LOG_INFO("Here is a string: %s; with another string %s", "foo", "bar");
51 
52   // Warn level
53   PW_LOG_WARN("This log statement should be at WARN level; no args");
54   for (int i = 0; i < n; ++i) {
55     PW_LOG_WARN("Counting: %d", i);
56   }
57   PW_LOG_WARN("Here is a string: %s; with another string %s", "foo", "bar");
58 
59   // Error level.
60   PW_LOG_ERROR("This log statement should be at ERROR level; no args");
61   for (int i = 0; i < n; ++i) {
62     PW_LOG_ERROR("Counting: %d", i);
63   }
64   PW_LOG_ERROR("Here is a string: %s; with another string %s", "foo", "bar");
65 
66   // Critical level.
67   PW_LOG_CRITICAL("This log is the last one; device should reboot");
68 
69   // Core log macro, with manually specified level and flags.
70   PW_LOG(PW_LOG_LEVEL_DEBUG,
71          PW_LOG_MODULE_NAME,
72          0,
73          "A manual DEBUG-level message");
74   PW_LOG(PW_LOG_LEVEL_DEBUG,
75          PW_LOG_MODULE_NAME,
76          1,
77          "A manual DEBUG-level message; with a flag");
78 
79   PW_LOG(
80       PW_LOG_LEVEL_INFO, PW_LOG_MODULE_NAME, 0, "A manual INFO-level message");
81   PW_LOG(PW_LOG_LEVEL_INFO,
82          PW_LOG_MODULE_NAME,
83          1,
84          "A manual INFO-level message; with a flag");
85 
86   PW_LOG(
87       PW_LOG_LEVEL_WARN, PW_LOG_MODULE_NAME, 0, "A manual WARN-level message");
88   PW_LOG(PW_LOG_LEVEL_WARN,
89          PW_LOG_MODULE_NAME,
90          1,
91          "A manual WARN-level message; with a flag");
92 
93   PW_LOG(PW_LOG_LEVEL_ERROR,
94          PW_LOG_MODULE_NAME,
95          0,
96          "A manual ERROR-level message");
97   PW_LOG(PW_LOG_LEVEL_ERROR,
98          PW_LOG_MODULE_NAME,
99          1,
100          "A manual ERROR-level message; with a flag");
101 
102   PW_LOG(PW_LOG_LEVEL_CRITICAL,
103          PW_LOG_MODULE_NAME,
104          0,
105          "A manual CRITICAL-level message");
106   PW_LOG(PW_LOG_LEVEL_CRITICAL,
107          PW_LOG_MODULE_NAME,
108          1,
109          "A manual CRITICAL-level message; with a flag");
110 
111   // Log levels other than the standard ones work; what each backend does is
112   // implementation defined.
113   PW_LOG(0, PW_LOG_MODULE_NAME, PW_LOG_FLAGS, "Custom log level: 0");
114   PW_LOG(1, PW_LOG_MODULE_NAME, PW_LOG_FLAGS, "Custom log level: 1");
115   PW_LOG(2, PW_LOG_MODULE_NAME, PW_LOG_FLAGS, "Custom log level: 2");
116   PW_LOG(3, PW_LOG_MODULE_NAME, PW_LOG_FLAGS, "Custom log level: 3");
117   PW_LOG(100, PW_LOG_MODULE_NAME, PW_LOG_FLAGS, "Custom log level: 100");
118 
119   // Logging from a function.
120   LoggingFromFunctionPlainC();
121 
122   // Changing the module name mid-file.
123 #undef PW_LOG_MODULE_NAME
124 #define PW_LOG_MODULE_NAME "XYZ"
125   PW_LOG_INFO("This has a custom module name");
126   PW_LOG_INFO("So does this");
127 
128   CustomFormatStringTest();
129 }
130 
131 #undef PW_LOG
132 #define PW_LOG(level, module, flags, message, ...)                       \
133   DoNothingFakeFunction(module,                                          \
134                         "%d/%d/%d: incoming transmission [" message "]", \
135                         level,                                           \
136                         __LINE__,                                        \
137                         flags PW_COMMA_ARGS(__VA_ARGS__))
138 
139 static void DoNothingFakeFunction(const char* module, const char* f, ...)
140     PW_PRINTF_FORMAT(2, 3);
141 
DoNothingFakeFunction(const char * module,const char * f,...)142 static void DoNothingFakeFunction(const char* module, const char* f, ...) {
143   (void)module;
144   (void)f;
145 }
146 
CustomFormatStringTest(void)147 static void CustomFormatStringTest(void) {
148   PW_LOG_DEBUG("Abc");
149   PW_LOG_INFO("Abc %d", 123);
150   PW_LOG_WARN("Abc %d %s", 123, "four");
151 }
152