C/C++ API Reference
Loading...
Searching...
No Matches
log.h
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 file describes Pigweed's public user-facing logging API.
16//
17// THIS PUBLIC API IS NOT STABLE OR COMPLETE!
18//
19// Key functionality is still missing:
20//
21// - API for controlling verbosity at run time
22// - API for querying if logging is enabled for the given level or flags
23//
24#pragma once
25
26#include "pw_log/config.h"
27#include "pw_log/levels.h"
28#include "pw_log/options.h"
29
30// log_backend.h must ultimately resolve to a header that implements the macros
31// required by the logging facade, as described below.
32//
33// Inputs: Macros the downstream user provides to control the logging system:
34//
35// PW_LOG_MODULE_NAME
36// - The module name the backend should use
37//
38// PW_LOG_LEVEL
39// - General log level setting. By default, logs below this level are
40// excluded from the build.
41//
42// Outputs: Macros log_backend.h is expected to provide:
43//
44// PW_LOG(level, verbosity, module, flags, fmt, ...)
45// - Required.
46// Level - An integer level as defined by pw_log/levels.h for this log.
47// Verbosity - An integer level as defined by pw_log/levels.h which is the
48// minimum level which is enabled.
49// Module - A string literal for the module name.
50// Flags - Arbitrary flags the backend can leverage; user-defined.
51// Example: HAS_PII - A log has personally-identifying data
52// Example: HAS_DII - A log has device-identifying data
53// Example: RELIABLE_DELIVERY - Ask backend to ensure the
54// log is delivered; this may entail blocking other logs.
55// Example: BEST_EFFORT - Don't deliver this log if it
56// would mean blocking or dropping important-flagged logs
57//
58// PW_LOG_DEBUG(fmt, ...)
59// PW_LOG_INFO(fmt, ...)
60// PW_LOG_WARN(fmt, ...)
61// PW_LOG_ERROR(fmt, ...)
62// PW_LOG_CRITICAL(fmt, ...)
63// - Optional. If not defined by the backend, the facade's default
64// implementation defines these in terms of PW_LOG().
65//
66#include "pw_log_backend/log_backend.h"
67
69
73#define PW_LOG( \
74 level, verbosity, module, flags, /* format string and arguments */...) \
75 do { \
76 if (PW_LOG_ENABLE_IF(level, verbosity, module, flags)) { \
77 PW_HANDLE_LOG(level, module, flags, __VA_ARGS__); \
78 } \
79 } while (0)
80
82
83// For backends that elect to only provide the general PW_LOG() macro and not
84// specialized versions, define the standard PW_LOG_<level>() macros in terms
85// of the general PW_LOG().
86#ifndef PW_LOG_DEBUG
87#define PW_LOG_DEBUG(...) \
88 PW_LOG(PW_LOG_LEVEL_DEBUG, \
89 PW_LOG_LEVEL, \
90 PW_LOG_MODULE_NAME, \
91 PW_LOG_FLAGS, \
92 __VA_ARGS__)
93#endif // PW_LOG_DEBUG
94
95#ifndef PW_LOG_INFO
96#define PW_LOG_INFO(...) \
97 PW_LOG(PW_LOG_LEVEL_INFO, \
98 PW_LOG_LEVEL, \
99 PW_LOG_MODULE_NAME, \
100 PW_LOG_FLAGS, \
101 __VA_ARGS__)
102#endif // PW_LOG_INFO
103
104#ifndef PW_LOG_WARN
105#define PW_LOG_WARN(...) \
106 PW_LOG(PW_LOG_LEVEL_WARN, \
107 PW_LOG_LEVEL, \
108 PW_LOG_MODULE_NAME, \
109 PW_LOG_FLAGS, \
110 __VA_ARGS__)
111#endif // PW_LOG_WARN
112
113#ifndef PW_LOG_ERROR
114#define PW_LOG_ERROR(...) \
115 PW_LOG(PW_LOG_LEVEL_ERROR, \
116 PW_LOG_LEVEL, \
117 PW_LOG_MODULE_NAME, \
118 PW_LOG_FLAGS, \
119 __VA_ARGS__)
120#endif // PW_LOG_ERROR
121
122#ifndef PW_LOG_CRITICAL
123#define PW_LOG_CRITICAL(...) \
124 PW_LOG(PW_LOG_LEVEL_CRITICAL, \
125 PW_LOG_LEVEL, \
126 PW_LOG_MODULE_NAME, \
127 PW_LOG_FLAGS, \
128 __VA_ARGS__)
129#endif // PW_LOG_CRITICAL
130
131#ifndef PW_LOG_EVERY_N
132#define PW_LOG_EVERY_N(level, rate, ...) \
133 do { \
134 static uint32_t _pw_log_suppressor##__LINE__ = 0; \
135 if (_pw_log_suppressor##__LINE__ == 0) { \
136 PW_LOG( \
137 level, PW_LOG_LEVEL, PW_LOG_MODULE_NAME, PW_LOG_FLAGS, __VA_ARGS__); \
138 _pw_log_suppressor##__LINE__ = rate; \
139 } else { \
140 _pw_log_suppressor##__LINE__--; \
141 } \
142 } while (0)
143#endif // PW_LOG_EVERY_N
144
146
152#ifndef PW_LOG_FLAG_BITS
153#define PW_LOG_FLAG_BITS 2
154#endif // PW_LOG_FLAG_BITS
155