1 // Copyright 2022 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 // TODO: b/235289499 - Add verification of the actually logged statements.
15
16 // clang-format off
17 #define PW_LOG_MODULE_NAME "TST"
18 #define PW_LOG_LEVEL PW_LOG_LEVEL_DEBUG
19
20 #include "pw_log/glog_adapter.h"
21
22 #include "pw_unit_test/framework.h"
23 // clang-format on
24
25 namespace pw::log {
26 namespace {
27
28 volatile bool conditional;
29
TEST(Glog,Debug)30 TEST(Glog, Debug) { LOG(DEBUG) << "LOG(DEBUG) works"; }
31
TEST(Glog,ConditionalDebug)32 TEST(Glog, ConditionalDebug) {
33 conditional = true;
34 LOG_IF(DEBUG, conditional) << "LOG_IF(DEBUG, true) works";
35 conditional = false;
36 LOG_IF(DEBUG, conditional) << "You should not see this log";
37 }
38
TEST(Glog,Info)39 TEST(Glog, Info) { LOG(INFO) << "LOG(INFO) works"; }
40
TEST(Glog,ConditionalInfo)41 TEST(Glog, ConditionalInfo) {
42 conditional = true;
43 LOG_IF(INFO, conditional) << "LOG_IF(INFO, true) works";
44 conditional = false;
45 LOG_IF(INFO, conditional) << "You should not see this log";
46 }
47
TEST(Glog,Warning)48 TEST(Glog, Warning) { LOG(WARNING) << "LOG(WARNING) works"; }
49
TEST(Glog,ConditionalWarning)50 TEST(Glog, ConditionalWarning) {
51 conditional = true;
52 LOG_IF(WARNING, conditional) << "LOG_IF(WARNING, true) works";
53 conditional = false;
54 LOG_IF(WARNING, conditional) << "You should not see this log";
55 }
56
TEST(Glog,Error)57 TEST(Glog, Error) { LOG(ERROR) << "LOG(ERROR) works"; }
58
TEST(Glog,ConditionalError)59 TEST(Glog, ConditionalError) {
60 conditional = true;
61 LOG_IF(ERROR, conditional) << "LOG_IF(ERROR, true) works";
62 conditional = false;
63 LOG_IF(ERROR, conditional) << "You should not see this log";
64 }
65
TEST(Glog,Fatal)66 TEST(Glog, Fatal) {
67 conditional = false;
68 if (conditional) {
69 LOG(FATAL) << "LOG(FATAL) compiles but you should not see this log";
70 }
71 }
72
TEST(Glog,ConditionalFatal)73 TEST(Glog, ConditionalFatal) {
74 conditional = false;
75 LOG_IF(FATAL, conditional) << "LOG_IF(FATAL, false) compiles but you should "
76 << "not see this log";
77 }
78
TEST(Glog,Dfatal)79 TEST(Glog, Dfatal) {
80 conditional = false;
81 if (conditional) {
82 #if defined(NDEBUG)
83 LOG(DFATAL) << "LOG(DFATAL) works through PW_LOG_ERROR as NDEBUG is set";
84 #else // !defined(NDEBUG)
85 LOG(DFATAL) << "LOG(DFATAL) compiles but you should not see this log";
86 #endif // defined(NDEBUG)
87 }
88 }
89
TEST(Glog,ConditionalDfatal)90 TEST(Glog, ConditionalDfatal) {
91 #if defined(NDEBUG)
92 conditional = true;
93 LOG_IF(DFATAL, conditional) << "LOG_IF(DFATAL, true) works through "
94 << "PW_LOG_ERROR as NDEBUG is set";
95 #endif // defined(NDEBUG)
96 conditional = false;
97 LOG_IF(DFATAL, conditional) << "LOG_IF(DFATAL, false) compiles but you "
98 << "should not see this log";
99 }
100
101 } // namespace
102 } // namespace pw::log
103