1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 // This is mostly a compile test to verify that the log backend is able to
16 // compile the constructs promised by the logging facade; and that when run,
19 // TODO(pwbug/88): Add verification of the actually logged statements.
22 #define PW_LOG_MODULE_NAME "TST"
23 #define PW_LOG_USE_SHORT_NAMES 1
24 #define PW_LOG_USE_ULTRA_SHORT_NAMES 1
25 #define PW_LOG_LEVEL PW_LOG_LEVEL_DEBUG
26 #include "pw_log/log.h"
29 #include "gtest/gtest.h"
31 // TODO(pwbug/86): Test unsigned integer logging (32 and 64 bit); test pointer
34 void LoggingFromFunction() { PW_LOG_INFO("From a function!"); }
38 TEST(BasicLog, DebugLevel) {
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);
43 PW_LOG_DEBUG("Here is a string: %s; with another string %s", "foo", "bar");
46 TEST(BasicLog, InfoLevel) {
47 PW_LOG_INFO("This log statement should be at INFO level; no args");
48 for (int i = 0; i < N; ++i) {
49 PW_LOG_INFO("Counting: %d", i);
51 PW_LOG_INFO("Here is a string: %s; with another string %s", "foo", "bar");
54 TEST(BasicLog, WarnLevel) {
55 PW_LOG_WARN("This log statement should be at WARN level; no args");
56 for (int i = 0; i < N; ++i) {
57 PW_LOG_WARN("Counting: %d", i);
59 PW_LOG_WARN("Here is a string: %s; with another string %s", "foo", "bar");
62 TEST(BasicLog, ErrorLevel) {
63 PW_LOG_ERROR("This log statement should be at ERROR level; no args");
64 for (int i = 0; i < N; ++i) {
65 PW_LOG_ERROR("Counting: %d", i);
67 PW_LOG_ERROR("Here is a string: %s; with another string %s", "foo", "bar");
70 TEST(BasicLog, CriticalLevel) {
71 PW_LOG_CRITICAL("Critical, emergency log. Device should not reboot");
74 TEST(BasicLog, ManualLevel) {
75 PW_LOG(PW_LOG_LEVEL_DEBUG, 0, "A manual DEBUG-level message");
76 PW_LOG(PW_LOG_LEVEL_DEBUG, 1, "A manual DEBUG-level message; with a flag");
78 PW_LOG(PW_LOG_LEVEL_INFO, 0, "A manual INFO-level message");
79 PW_LOG(PW_LOG_LEVEL_INFO, 1, "A manual INFO-level message; with a flag");
81 PW_LOG(PW_LOG_LEVEL_WARN, 0, "A manual WARN-level message");
82 PW_LOG(PW_LOG_LEVEL_WARN, 1, "A manual WARN-level message; with a flag");
84 PW_LOG(PW_LOG_LEVEL_ERROR, 0, "A manual ERROR-level message");
85 PW_LOG(PW_LOG_LEVEL_ERROR, 1, "A manual ERROR-level message; with a flag");
87 PW_LOG(PW_LOG_LEVEL_CRITICAL, 0, "A manual CRITICAL-level message");
89 PW_LOG_LEVEL_CRITICAL, 1, "A manual CRITICAL-level message; with a flag");
92 TEST(BasicLog, FromAFunction) { LoggingFromFunction(); }
94 TEST(BasicLog, CustomLogLevels) {
95 // Log levels other than the standard ones work; what each backend does is
96 // implementation defined.
97 PW_LOG(0, 0, "Custom log level: 0");
98 PW_LOG(1, 0, "Custom log level: 1");
99 PW_LOG(2, 0, "Custom log level: 2");
100 PW_LOG(3, 0, "Custom log level: 3");
101 PW_LOG(100, 0, "Custom log level: 100");
104 #define TEST_FAILED_LOG "IF THIS MESSAGE WAS LOGGED, THE TEST FAILED"
106 TEST(BasicLog, FilteringByLevel) {
107 #undef PW_LOG_SKIP_LOGS_WITH_LEVEL_LT
108 #define PW_LOG_SKIP_LOGS_WITH_LEVEL_LT PW_LOG_LEVEL_ERROR
110 PW_LOG_DEBUG(TEST_FAILED_LOG);
111 PW_LOG_INFO(TEST_FAILED_LOG);
112 PW_LOG_WARN(TEST_FAILED_LOG);
114 PW_LOG_ERROR("This log should appear as error status (and that's good)");
116 #undef PW_LOG_SKIP_LOGS_WITH_LEVEL_LT
117 #define PW_LOG_SKIP_LOGS_WITH_LEVEL_LT 0
120 TEST(BasicLog, FilteringByFlags) {
121 #undef PW_LOG_SKIP_LOGS_WITH_FLAGS
122 #define PW_LOG_SKIP_LOGS_WITH_FLAGS 1
124 // Flag is set so these should all get zapped.
125 PW_LOG(PW_LOG_LEVEL_INFO, 1, TEST_FAILED_LOG);
126 PW_LOG(PW_LOG_LEVEL_ERROR, 1, TEST_FAILED_LOG);
128 // However, a different flag bit should still log.
129 PW_LOG(PW_LOG_LEVEL_INFO, 1 << 1, "This flagged log is intended to appear");
130 PW_LOG(PW_LOG_LEVEL_ERROR, 1 << 1, "This flagged log is intended to appear");
132 #undef PW_LOG_SKIP_LOGS_WITH_FLAGS
133 #define PW_LOG_SKIP_LOGS_WITH_FLAGS 0
136 TEST(BasicLog, ChangingTheModuleName) {
137 #undef PW_LOG_MODULE_NAME
138 #define PW_LOG_MODULE_NAME "PQR"
139 PW_LOG_INFO("This has a custom module name");
140 PW_LOG_INFO("So does this");
143 TEST(BasicLog, ShortNames) {
144 LOG(PW_LOG_LEVEL_INFO, 0, "Shrt lg");
145 LOG_DEBUG("A debug log: %d", 1);
146 LOG_INFO("An info log: %d", 2);
147 LOG_WARN("A warning log: %d", 3);
148 LOG_ERROR("An error log: %d", 4);
149 LOG_CRITICAL("A critical log: %d", 4);
152 TEST(BasicLog, UltraShortNames) {
153 LOG(PW_LOG_LEVEL_INFO, 0, "Shrt lg");
154 DBG("A debug log: %d", 1);
155 INF("An info log: %d", 2);
156 WRN("A warning log: %d", 3);
157 ERR("An error log: %d", 4);
158 CRT("A critical log: %d", 4);
161 extern "C" void BasicLogTestPlainC();
163 TEST(BasicLog, FromPlainC) { BasicLogTestPlainC(); }
165 // Test that adding to the format string compiles correctly. If PW_COMMA_ARGS is
166 // used in PW_LOG_INFO and the other wrappers in pw_log/log.h, then these
167 // functions tests fail to compile, because the arguments end up out-of-order.
170 #define PW_LOG(level, flags, message, ...) \
171 DoNothingFakeFunction("%d/%d/%d: incoming transmission [" message "]", \
174 flags PW_COMMA_ARGS(__VA_ARGS__))
176 void DoNothingFakeFunction(const char*, ...) PW_PRINTF_FORMAT(1, 2);
178 void DoNothingFakeFunction(const char*, ...) {}
180 TEST(CustomFormatString, DebugLevel) {
181 PW_LOG_DEBUG("This log statement should be at DEBUG level; no args");
182 for (int i = 0; i < N; ++i) {
183 PW_LOG_DEBUG("Counting: %d", i);
185 PW_LOG_DEBUG("Here is a string: %s; with another string %s", "foo", "bar");
188 TEST(CustomFormatString, InfoLevel) {
189 PW_LOG_INFO("This log statement should be at INFO level; no args");
190 for (int i = 0; i < N; ++i) {
191 PW_LOG_INFO("Counting: %d", i);
193 PW_LOG_INFO("Here is a string: %s; with another string %s", "foo", "bar");
196 TEST(CustomFormatString, WarnLevel) {
197 PW_LOG_WARN("This log statement should be at WARN level; no args");
198 for (int i = 0; i < N; ++i) {
199 PW_LOG_WARN("Counting: %d", i);
201 PW_LOG_WARN("Here is a string: %s; with another string %s", "foo", "bar");
204 TEST(CustomFormatString, ErrorLevel) {
205 PW_LOG_ERROR("This log statement should be at ERROR level; no args");
206 for (int i = 0; i < N; ++i) {
207 PW_LOG_ERROR("Counting: %d", i);
209 PW_LOG_ERROR("Here is a string: %s; with another string %s", "foo", "bar");
212 TEST(CustomFormatString, CriticalLevel) {
213 PW_LOG_CRITICAL("Critical, emergency log. Device should not reboot");