blob: 71d93be25d4c7111bf0122d1b25922bda8edce56 [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 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
31void LoggingFromFunctionPlainC() { PW_LOG_INFO("From a function!"); }
32
33void BasicLogTestPlainC() {
34 int n = 3;
35
36 // Debug level
37 PW_LOG_DEBUG("This log statement should be at DEBUG level; no args");
38 for (int i = 0; i < n; ++i) {
39 PW_LOG_DEBUG("Counting: %d", i);
40 }
41 PW_LOG_DEBUG("Here is a string: %s; with another string %s", "foo", "bar");
42
43 // Info level
44 PW_LOG_INFO("This log statement should be at INFO level; no args");
45 for (int i = 0; i < n; ++i) {
46 PW_LOG_INFO("Counting: %d", i);
47 }
48 PW_LOG_INFO("Here is a string: %s; with another string %s", "foo", "bar");
49
50 // Warn level
51 PW_LOG_WARN("This log statement should be at WARN level; no args");
52 for (int i = 0; i < n; ++i) {
53 PW_LOG_WARN("Counting: %d", i);
54 }
55 PW_LOG_WARN("Here is a string: %s; with another string %s", "foo", "bar");
56
57 // Error level.
58 PW_LOG_ERROR("This log statement should be at ERROR level; no args");
59 for (int i = 0; i < n; ++i) {
60 PW_LOG_ERROR("Counting: %d", i);
61 }
62 PW_LOG_ERROR("Here is a string: %s; with another string %s", "foo", "bar");
63
64 // Critical level.
65 PW_LOG_CRITICAL("This log is the last one; device should reboot");
66
67 // Core log macro, with manually specified level and flags.
68 PW_LOG(PW_LOG_LEVEL_DEBUG, 0, "A manual DEBUG-level message");
69 PW_LOG(PW_LOG_LEVEL_DEBUG, 1, "A manual DEBUG-level message; with a flag");
70
71 PW_LOG(PW_LOG_LEVEL_INFO, 0, "A manual INFO-level message");
72 PW_LOG(PW_LOG_LEVEL_INFO, 1, "A manual INFO-level message; with a flag");
73
74 PW_LOG(PW_LOG_LEVEL_WARN, 0, "A manual WARN-level message");
75 PW_LOG(PW_LOG_LEVEL_WARN, 1, "A manual WARN-level message; with a flag");
76
77 PW_LOG(PW_LOG_LEVEL_ERROR, 0, "A manual ERROR-level message");
78 PW_LOG(PW_LOG_LEVEL_ERROR, 1, "A manual ERROR-level message; with a flag");
79
80 PW_LOG(PW_LOG_LEVEL_CRITICAL, 0, "A manual CRITICAL-level message");
81 PW_LOG(
82 PW_LOG_LEVEL_CRITICAL, 1, "A manual CRITICAL-level message; with a flag");
83
84 // Log levels other than the standard ones work; what each backend does is
85 // implementation defined.
86 PW_LOG(0, PW_LOG_NO_FLAGS, "Custom log level: 0");
87 PW_LOG(1, PW_LOG_NO_FLAGS, "Custom log level: 1");
88 PW_LOG(2, PW_LOG_NO_FLAGS, "Custom log level: 2");
89 PW_LOG(3, PW_LOG_NO_FLAGS, "Custom log level: 3");
90 PW_LOG(100, PW_LOG_NO_FLAGS, "Custom log level: 100");
91
92 // Logging from a function.
93 LoggingFromFunctionPlainC();
94
95 // Changing the module name mid-file.
96#undef PW_LOG_MODULE_NAME
97#define PW_LOG_MODULE_NAME "XYZ"
98 PW_LOG_INFO("This has a custom module name");
99 PW_LOG_INFO("So does this");
100}