• 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, 0, "A manual DEBUG-level message");
71   PW_LOG(PW_LOG_LEVEL_DEBUG, 1, "A manual DEBUG-level message; with a flag");
72 
73   PW_LOG(PW_LOG_LEVEL_INFO, 0, "A manual INFO-level message");
74   PW_LOG(PW_LOG_LEVEL_INFO, 1, "A manual INFO-level message; with a flag");
75 
76   PW_LOG(PW_LOG_LEVEL_WARN, 0, "A manual WARN-level message");
77   PW_LOG(PW_LOG_LEVEL_WARN, 1, "A manual WARN-level message; with a flag");
78 
79   PW_LOG(PW_LOG_LEVEL_ERROR, 0, "A manual ERROR-level message");
80   PW_LOG(PW_LOG_LEVEL_ERROR, 1, "A manual ERROR-level message; with a flag");
81 
82   PW_LOG(PW_LOG_LEVEL_CRITICAL, 0, "A manual CRITICAL-level message");
83   PW_LOG(
84       PW_LOG_LEVEL_CRITICAL, 1, "A manual CRITICAL-level message; with a flag");
85 
86   // Log levels other than the standard ones work; what each backend does is
87   // implementation defined.
88   PW_LOG(0, PW_LOG_DEFAULT_FLAGS, "Custom log level: 0");
89   PW_LOG(1, PW_LOG_DEFAULT_FLAGS, "Custom log level: 1");
90   PW_LOG(2, PW_LOG_DEFAULT_FLAGS, "Custom log level: 2");
91   PW_LOG(3, PW_LOG_DEFAULT_FLAGS, "Custom log level: 3");
92   PW_LOG(100, PW_LOG_DEFAULT_FLAGS, "Custom log level: 100");
93 
94   // Logging from a function.
95   LoggingFromFunctionPlainC();
96 
97   // Changing the module name mid-file.
98 #undef PW_LOG_MODULE_NAME
99 #define PW_LOG_MODULE_NAME "XYZ"
100   PW_LOG_INFO("This has a custom module name");
101   PW_LOG_INFO("So does this");
102 
103   CustomFormatStringTest();
104 }
105 
106 #undef PW_LOG
107 #define PW_LOG(level, flags, message, ...)                               \
108   DoNothingFakeFunction("%d/%d/%d: incoming transmission [" message "]", \
109                         level,                                           \
110                         __LINE__,                                        \
111                         flags PW_COMMA_ARGS(__VA_ARGS__))
112 
113 static void DoNothingFakeFunction(const char* f, ...) PW_PRINTF_FORMAT(1, 2);
114 
DoNothingFakeFunction(const char * f,...)115 static void DoNothingFakeFunction(const char* f, ...) { (void)f; }
116 
CustomFormatStringTest(void)117 static void CustomFormatStringTest(void) {
118   PW_LOG_DEBUG("Abc");
119   PW_LOG_INFO("Abc %d", 123);
120   PW_LOG_WARN("Abc %d %s", 123, "four");
121 }
122