1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 // This file describes Pigweed's public user-facing logging API.
17 // THIS PUBLIC API IS NOT STABLE OR COMPLETE!
19 // Key functionality is still missing:
21 // - API for controlling verbosity at run time
22 // - API for querying if logging is enabled for the given level or flags
26 #include "pw_log/levels.h"
27 #include "pw_log/options.h"
29 // log_backend.h must ultimately resolve to a header that implements the macros
30 // required by the logging facade, as described below.
32 // Inputs: Macros the downstream user provides to control the logging system:
35 // - The module name the backend should use
38 // - General log level setting. By default, logs below this level are
39 // excluded from the build.
41 // Outputs: Macros log_backend.h is expected to provide:
43 // PW_LOG(level, flags, fmt, ...)
45 // Level - An integer level as defined by pw_log/levels.h
46 // Flags - Arbitrary flags the backend can leverage; user-defined.
47 // Example: HAS_PII - A log has personally-identifying data
48 // Example: HAS_DII - A log has device-identifying data
49 // Example: RELIABLE_DELIVERY - Ask backend to ensure the
50 // log is delivered; this may entail blocking other logs.
51 // Example: BEST_EFFORT - Don't deliver this log if it
52 // would mean blocking or dropping important-flagged logs
54 // PW_LOG_DEBUG(fmt, ...)
55 // PW_LOG_INFO(fmt, ...)
56 // PW_LOG_WARN(fmt, ...)
57 // PW_LOG_ERROR(fmt, ...)
58 // PW_LOG_CRITICAL(fmt, ...)
59 // - Optional. If not defined by the backend, the facade's default
60 // implementation defines these in terms of PW_LOG().
62 #include "pw_log_backend/log_backend.h"
65 #define PW_LOG(level, flags, message, ...) \
67 if (PW_LOG_ENABLE_IF(level, flags)) { \
68 PW_HANDLE_LOG(level, flags, message, __VA_ARGS__); \
73 // For backends that elect to only provide the general PW_LOG() macro and not
74 // specialized versions, define the standard PW_LOG_<level>() macros in terms
75 // of the general PW_LOG().
77 #define PW_LOG_DEBUG(message, ...) \
78 PW_LOG(PW_LOG_LEVEL_DEBUG, PW_LOG_DEFAULT_FLAGS, message, __VA_ARGS__)
79 #endif // PW_LOG_DEBUG
82 #define PW_LOG_INFO(message, ...) \
83 PW_LOG(PW_LOG_LEVEL_INFO, PW_LOG_DEFAULT_FLAGS, message, __VA_ARGS__)
87 #define PW_LOG_WARN(message, ...) \
88 PW_LOG(PW_LOG_LEVEL_WARN, PW_LOG_DEFAULT_FLAGS, message, __VA_ARGS__)
92 #define PW_LOG_ERROR(message, ...) \
93 PW_LOG(PW_LOG_LEVEL_ERROR, PW_LOG_DEFAULT_FLAGS, message, __VA_ARGS__)
94 #endif // PW_LOG_ERROR
96 #ifndef PW_LOG_CRITICAL
97 #define PW_LOG_CRITICAL(message, ...) \
98 PW_LOG(PW_LOG_LEVEL_CRITICAL, PW_LOG_DEFAULT_FLAGS, message, __VA_ARGS__)
99 #endif // PW_LOG_CRITICAL
101 // Default: Number of bits available for the log level
103 // All log statements have a level, and this define is the number of bits
104 // available for the level. Some backends restrict this for better efficiency.
105 // By default, pick a restricted but large enough value to work for most cases.
106 #ifndef PW_LOG_LEVEL_BITS
107 #define PW_LOG_LEVEL_BITS 6
108 #endif // PW_LOG_LEVEL_BITS
110 // Default: Number of bits available for the log flags
112 // All log statements have a flags field, and this define is the number of bits
113 // available for the flags. Some backends restrict this for better efficiency.
114 // By default, pick a restricted but large enough value to work for most cases.
115 #ifndef PW_LOG_FLAG_BITS
116 #define PW_LOG_FLAG_BITS 10
117 #endif // PW_LOG_FLAG_BITS
119 // Define short, usable names if requested.
120 // TODO(pwbug/17): Convert this to the config system when available.
121 #ifndef PW_LOG_USE_SHORT_NAMES
122 #define PW_LOG_USE_SHORT_NAMES 0
125 // Define ultra short, usable names if requested.
126 #ifndef PW_LOG_USE_ULTRA_SHORT_NAMES
127 #define PW_LOG_USE_ULTRA_SHORT_NAMES 0
128 #endif // PW_LOG_USE_SHORT_NAMES
131 #if PW_LOG_USE_SHORT_NAMES
133 #define LOG_DEBUG PW_LOG_DEBUG
134 #define LOG_INFO PW_LOG_INFO
135 #define LOG_WARN PW_LOG_WARN
136 #define LOG_ERROR PW_LOG_ERROR
137 #define LOG_CRITICAL PW_LOG_CRITICAL
138 #endif // PW_LOG_USE_SHORT_NAMES
142 #if PW_LOG_USE_ULTRA_SHORT_NAMES
143 #if !PW_LOG_USE_SHORT_NAMES
146 #define DBG PW_LOG_DEBUG
147 #define INF PW_LOG_INFO
148 #define WRN PW_LOG_WARN
149 #define ERR PW_LOG_ERROR
150 #define CRT PW_LOG_CRITICAL
151 #endif // PW_LOG_USE_ULTRA_SHORT_NAMES