blob: 9622bf1fe5fc1014854a08933a0d6d741884f189 [file] [log] [blame]
Keir Mierleaf5e3582019-12-30 13:11:05 -08001// 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 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,
17// there is no crash.
18//
19// TODO(pwbug/88): Add verification of the actually logged statements.
20
Keir Mierle750bb582020-01-22 17:07:07 -080021// clang-format off
Keir Mierleaf5e3582019-12-30 13:11:05 -080022#define PW_LOG_MODULE_NAME "TST"
Keir Mierle750bb582020-01-22 17:07:07 -080023#define PW_LOG_USE_SHORT_NAMES 1
24#define PW_LOG_USE_ULTRA_SHORT_NAMES 1
25#include "pw_log/log.h"
26// clang-format on
Keir Mierleaf5e3582019-12-30 13:11:05 -080027
28#include "gtest/gtest.h"
Keir Mierleaf5e3582019-12-30 13:11:05 -080029
30// TODO(pwbug/86): Test unsigned integer logging (32 and 64 bit); test pointer
31// logging.
32
33void LoggingFromFunction() { PW_LOG_INFO("From a function!"); }
34
35const int N = 3;
36
37TEST(BasicLog, DebugLevel) {
38 PW_LOG_DEBUG("This log statement should be at DEBUG level; no args");
39 for (int i = 0; i < N; ++i) {
40 PW_LOG_DEBUG("Counting: %d", i);
41 }
42 PW_LOG_DEBUG("Here is a string: %s; with another string %s", "foo", "bar");
43}
44
45TEST(BasicLog, InfoLevel) {
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
53TEST(BasicLog, WarnLevel) {
54 PW_LOG_WARN("This log statement should be at WARN level; no args");
55 for (int i = 0; i < N; ++i) {
56 PW_LOG_WARN("Counting: %d", i);
57 }
58 PW_LOG_WARN("Here is a string: %s; with another string %s", "foo", "bar");
59}
60
61TEST(BasicLog, ErrorLevel) {
62 PW_LOG_ERROR("This log statement should be at ERROR level; no args");
63 for (int i = 0; i < N; ++i) {
64 PW_LOG_ERROR("Counting: %d", i);
65 }
66 PW_LOG_ERROR("Here is a string: %s; with another string %s", "foo", "bar");
67}
68
69TEST(BasicLog, CriticalLevel) {
70 PW_LOG_CRITICAL("Critical, emergency log. Device should not reboot");
71}
72
73TEST(BasicLog, ManualLevel) {
74 PW_LOG(PW_LOG_LEVEL_DEBUG, 0, "A manual DEBUG-level message");
75 PW_LOG(PW_LOG_LEVEL_DEBUG, 1, "A manual DEBUG-level message; with a flag");
76
77 PW_LOG(PW_LOG_LEVEL_INFO, 0, "A manual INFO-level message");
78 PW_LOG(PW_LOG_LEVEL_INFO, 1, "A manual INFO-level message; with a flag");
79
80 PW_LOG(PW_LOG_LEVEL_WARN, 0, "A manual WARN-level message");
81 PW_LOG(PW_LOG_LEVEL_WARN, 1, "A manual WARN-level message; with a flag");
82
83 PW_LOG(PW_LOG_LEVEL_ERROR, 0, "A manual ERROR-level message");
84 PW_LOG(PW_LOG_LEVEL_ERROR, 1, "A manual ERROR-level message; with a flag");
85
86 PW_LOG(PW_LOG_LEVEL_CRITICAL, 0, "A manual CRITICAL-level message");
87 PW_LOG(
88 PW_LOG_LEVEL_CRITICAL, 1, "A manual CRITICAL-level message; with a flag");
89}
90
91TEST(BasicLog, FromAFunction) { LoggingFromFunction(); }
92
93TEST(BasicLog, CustomLogLevels) {
94 // Log levels other than the standard ones work; what each backend does is
95 // implementation defined.
96 PW_LOG(0, 0, "Custom log level: 0");
97 PW_LOG(1, 0, "Custom log level: 1");
98 PW_LOG(2, 0, "Custom log level: 2");
99 PW_LOG(3, 0, "Custom log level: 3");
100 PW_LOG(100, 0, "Custom log level: 100");
101}
102
103#define TEST_FAILED_LOG "IF THIS MESSAGE WAS LOGGED, THE TEST FAILED"
104
105TEST(BasicLog, FilteringByLevel) {
106#undef PW_LOG_SKIP_LOGS_WITH_LEVEL_LT
107#define PW_LOG_SKIP_LOGS_WITH_LEVEL_LT PW_LOG_LEVEL_ERROR
108
109 PW_LOG_DEBUG(TEST_FAILED_LOG);
110 PW_LOG_INFO(TEST_FAILED_LOG);
111 PW_LOG_WARN(TEST_FAILED_LOG);
112
113 PW_LOG_ERROR("This log should appear as error status (and that's good)");
114
115#undef PW_LOG_SKIP_LOGS_WITH_LEVEL_LT
116#define PW_LOG_SKIP_LOGS_WITH_LEVEL_LT 0
117}
118
119TEST(BasicLog, FilteringByFlags) {
120#undef PW_LOG_SKIP_LOGS_WITH_FLAGS
121#define PW_LOG_SKIP_LOGS_WITH_FLAGS 1
122
123 // Flag is set so these should all get zapped.
124 PW_LOG(PW_LOG_LEVEL_INFO, 1, TEST_FAILED_LOG);
125 PW_LOG(PW_LOG_LEVEL_ERROR, 1, TEST_FAILED_LOG);
126
127 // However, a different flag bit should still log.
128 PW_LOG(PW_LOG_LEVEL_INFO, 1 << 1, "This flagged log is intended to appear");
129 PW_LOG(PW_LOG_LEVEL_ERROR, 1 << 1, "This flagged log is intended to appear");
130
131#undef PW_LOG_SKIP_LOGS_WITH_FLAGS
132#define PW_LOG_SKIP_LOGS_WITH_FLAGS 0
133}
134
135TEST(BasicLog, ChangingTheModuleName) {
136#undef PW_LOG_MODULE_NAME
137#define PW_LOG_MODULE_NAME "PQR"
138 PW_LOG_INFO("This has a custom module name");
139 PW_LOG_INFO("So does this");
140}
141
Keir Mierle750bb582020-01-22 17:07:07 -0800142TEST(BasicLog, ShortNames) {
143 LOG(PW_LOG_LEVEL_INFO, 0, "Shrt lg");
144 LOG_DEBUG("A debug log: %d", 1);
145 LOG_INFO("An info log: %d", 2);
146 LOG_WARN("A warning log: %d", 3);
147 LOG_ERROR("An error log: %d", 4);
148 LOG_CRITICAL("A critical log: %d", 4);
149}
150
151TEST(BasicLog, UltraShortNames) {
152 LOG(PW_LOG_LEVEL_INFO, 0, "Shrt lg");
153 DBG("A debug log: %d", 1);
154 INF("An info log: %d", 2);
155 WRN("A warning log: %d", 3);
156 ERR("An error log: %d", 4);
157 CRT("A critical log: %d", 4);
158}
159
Keir Mierleaf5e3582019-12-30 13:11:05 -0800160extern "C" {
161void BasicLogTestPlainC();
162};
163
164TEST(BasicLog, FromPlainC) { BasicLogTestPlainC(); }