Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | /* * Copyright (c) 2018 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #ifndef ZEPHYR_INCLUDE_LOGGING_LOG_H_ #define ZEPHYR_INCLUDE_LOGGING_LOG_H_ #include <logging/log_instance.h> #include <logging/log_core.h> #ifdef __cplusplus extern "C" { #endif /** * @brief Logging * @defgroup logging Logging * @{ * @} */ /** * @brief Logger API * @defgroup log_api Logging API * @ingroup logger * @{ */ #define LOG_LEVEL_NONE 0 #define LOG_LEVEL_ERR 1 #define LOG_LEVEL_WRN 2 #define LOG_LEVEL_INF 3 #define LOG_LEVEL_DBG 4 /** * @brief Writes an ERROR level message to the log. * * @details It's meant to report severe errors, such as those from which it's * not possible to recover. * * @param ... A string optionally containing printk valid conversion specifier, * followed by as many values as specifiers. */ #define LOG_ERR(...) Z_LOG(LOG_LEVEL_ERR, __VA_ARGS__) /** * @brief Writes a WARNING level message to the log. * * @details It's meant to register messages related to unusual situations that * are not necessarily errors. * * @param ... A string optionally containing printk valid conversion specifier, * followed by as many values as specifiers. */ #define LOG_WRN(...) Z_LOG(LOG_LEVEL_WRN, __VA_ARGS__) /** * @brief Writes an INFO level message to the log. * * @details It's meant to write generic user oriented messages. * * @param ... A string optionally containing printk valid conversion specifier, * followed by as many values as specifiers. */ #define LOG_INF(...) Z_LOG(LOG_LEVEL_INF, __VA_ARGS__) /** * @brief Writes a DEBUG level message to the log. * * @details It's meant to write developer oriented information. * * @param ... A string optionally containing printk valid conversion specifier, * followed by as many values as specifiers. */ #define LOG_DBG(...) Z_LOG(LOG_LEVEL_DBG, __VA_ARGS__) /** * @brief Writes an ERROR level message associated with the instance to the log. * * Message is associated with specific instance of the module which has * independent filtering settings (if runtime filtering is enabled) and * message prefix (\<module_name\>.\<instance_name\>). It's meant to report * severe errors, such as those from which it's not possible to recover. * * @param _log_inst Pointer to the log structure associated with the instance. * @param ... A string optionally containing printk valid conversion specifier, * followed by as many values as specifiers. */ #define LOG_INST_ERR(_log_inst, ...) \ Z_LOG_INSTANCE(LOG_LEVEL_ERR, _log_inst, __VA_ARGS__) /** * @brief Writes a WARNING level message associated with the instance to the * log. * * Message is associated with specific instance of the module which has * independent filtering settings (if runtime filtering is enabled) and * message prefix (\<module_name\>.\<instance_name\>). It's meant to register * messages related to unusual situations that are not necessarily errors. * * @param _log_inst Pointer to the log structure associated with the instance. * @param ... A string optionally containing printk valid conversion * specifier, followed by as many values as specifiers. */ #define LOG_INST_WRN(_log_inst, ...) \ Z_LOG_INSTANCE(LOG_LEVEL_WRN, _log_inst, __VA_ARGS__) /** * @brief Writes an INFO level message associated with the instance to the log. * * Message is associated with specific instance of the module which has * independent filtering settings (if runtime filtering is enabled) and * message prefix (\<module_name\>.\<instance_name\>). It's meant to write * generic user oriented messages. * * @param _log_inst Pointer to the log structure associated with the instance. * @param ... A string optionally containing printk valid conversion specifier, * followed by as many values as specifiers. */ #define LOG_INST_INF(_log_inst, ...) \ Z_LOG_INSTANCE(LOG_LEVEL_INF, _log_inst, __VA_ARGS__) /** * @brief Writes a DEBUG level message associated with the instance to the log. * * Message is associated with specific instance of the module which has * independent filtering settings (if runtime filtering is enabled) and * message prefix (\<module_name\>.\<instance_name\>). It's meant to write * developer oriented information. * * @param _log_inst Pointer to the log structure associated with the instance. * @param ... A string optionally containing printk valid conversion specifier, * followed by as many values as specifiers. */ #define LOG_INST_DBG(_log_inst, ...) \ Z_LOG_INSTANCE(LOG_LEVEL_DBG, _log_inst, __VA_ARGS__) /** * @brief Writes an ERROR level hexdump message to the log. * * @details It's meant to report severe errors, such as those from which it's * not possible to recover. * * @param _data Pointer to the data to be logged. * @param _length Length of data (in bytes). * @param _str Persistent, raw string. */ #define LOG_HEXDUMP_ERR(_data, _length, _str) \ Z_LOG_HEXDUMP(LOG_LEVEL_ERR, _data, _length, _str) /** * @brief Writes a WARNING level message to the log. * * @details It's meant to register messages related to unusual situations that * are not necessarily errors. * * @param _data Pointer to the data to be logged. * @param _length Length of data (in bytes). * @param _str Persistent, raw string. */ #define LOG_HEXDUMP_WRN(_data, _length, _str) \ Z_LOG_HEXDUMP(LOG_LEVEL_WRN, _data, _length, _str) /** * @brief Writes an INFO level message to the log. * * @details It's meant to write generic user oriented messages. * * @param _data Pointer to the data to be logged. * @param _length Length of data (in bytes). * @param _str Persistent, raw string. */ #define LOG_HEXDUMP_INF(_data, _length, _str) \ Z_LOG_HEXDUMP(LOG_LEVEL_INF, _data, _length, _str) /** * @brief Writes a DEBUG level message to the log. * * @details It's meant to write developer oriented information. * * @param _data Pointer to the data to be logged. * @param _length Length of data (in bytes). * @param _str Persistent, raw string. */ #define LOG_HEXDUMP_DBG(_data, _length, _str) \ Z_LOG_HEXDUMP(LOG_LEVEL_DBG, _data, _length, _str) /** * @brief Writes an ERROR hexdump message associated with the instance to the * log. * * Message is associated with specific instance of the module which has * independent filtering settings (if runtime filtering is enabled) and * message prefix (\<module_name\>.\<instance_name\>). It's meant to report * severe errors, such as those from which it's not possible to recover. * * @param _log_inst Pointer to the log structure associated with the instance. * @param _data Pointer to the data to be logged. * @param _length Length of data (in bytes). * @param _str Persistent, raw string. */ #define LOG_INST_HEXDUMP_ERR(_log_inst, _data, _length, _str) \ Z_LOG_HEXDUMP_INSTANCE(LOG_LEVEL_ERR, _log_inst, _data, _length, _str) /** * @brief Writes a WARNING level hexdump message associated with the instance to * the log. * * @details It's meant to register messages related to unusual situations that * are not necessarily errors. * * @param _log_inst Pointer to the log structure associated with the instance. * @param _data Pointer to the data to be logged. * @param _length Length of data (in bytes). * @param _str Persistent, raw string. */ #define LOG_INST_HEXDUMP_WRN(_log_inst, _data, _length, _str) \ Z_LOG_HEXDUMP_INSTANCE(LOG_LEVEL_WRN, _log_inst, _data, _length, _str) /** * @brief Writes an INFO level hexdump message associated with the instance to * the log. * * @details It's meant to write generic user oriented messages. * * @param _log_inst Pointer to the log structure associated with the instance. * @param _data Pointer to the data to be logged. * @param _length Length of data (in bytes). * @param _str Persistent, raw string. */ #define LOG_INST_HEXDUMP_INF(_log_inst, _data, _length, _str) \ Z_LOG_HEXDUMP_INSTANCE(LOG_LEVEL_INF, _log_inst, _data, _length, _str) /** * @brief Writes a DEBUG level hexdump message associated with the instance to * the log. * * @details It's meant to write developer oriented information. * * @param _log_inst Pointer to the log structure associated with the instance. * @param _data Pointer to the data to be logged. * @param _length Length of data (in bytes). * @param _str Persistent, raw string. */ #define LOG_INST_HEXDUMP_DBG(_log_inst, _data, _length, _str) \ Z_LOG_HEXDUMP_INSTANCE(LOG_LEVEL_DBG, _log_inst, _data, _length, _str) /** * @brief Writes an formatted string to the log. * * @details Conditionally compiled (see CONFIG_LOG_PRINTK). Function provides * printk functionality. It is inefficient compared to standard logging * because string formatting is performed in the call context and not deferred * to the log processing context (@ref log_process). * * @param fmt Formatted string to output. * @param ap Variable parameters. * * return Number of bytes written. */ int log_printk(const char *fmt, va_list ap); /** @brief Copy transient string to a buffer from internal, logger pool. * * Function should be used when transient string is intended to be logged. * Logger allocates a buffer and copies input string returning a pointer to the * copy. Logger ensures that buffer is freed when logger message is freed. * * @param str Transient string. * * @return Copy of the string or default string if buffer could not be * allocated. String may be truncated if input string does not fit in * a buffer from the pool (see CONFIG_LOG_STRDUP_MAX_STRING). */ char *log_strdup(const char *str); #ifdef __cplusplus } #define LOG_IN_CPLUSPLUS 1 #endif /* Macro expects that optionally on second argument local log level is provided. * If provided it is returned, otherwise default log level is returned or * LOG_LEVEL, if it was locally defined. */ #if !defined(CONFIG_LOG) #define _LOG_LEVEL_RESOLVE(...) LOG_LEVEL_NONE #else #define _LOG_LEVEL_RESOLVE(...) \ Z_LOG_EVAL(LOG_LEVEL, \ (GET_ARG2(__VA_ARGS__, LOG_LEVEL)), \ (GET_ARG2(__VA_ARGS__, CONFIG_LOG_DEFAULT_LEVEL))) #endif /* Return first argument */ #define _LOG_ARG1(arg1, ...) arg1 #define _LOG_MODULE_CONST_DATA_CREATE(_name, _level) \ COND_CODE_1(LOG_IN_CPLUSPLUS, (extern), ()) \ const struct log_source_const_data LOG_ITEM_CONST_DATA(_name) \ __attribute__ ((section("." STRINGIFY(LOG_ITEM_CONST_DATA(_name))))) \ __attribute__((used)) = { \ .name = STRINGIFY(_name), \ .level = _level \ } #define _LOG_MODULE_DYNAMIC_DATA_CREATE(_name) \ struct log_source_dynamic_data LOG_ITEM_DYNAMIC_DATA(_name) \ __attribute__ ((section("." STRINGIFY( \ LOG_ITEM_DYNAMIC_DATA(_name)))) \ ) \ __attribute__((used)) #define _LOG_MODULE_DYNAMIC_DATA_COND_CREATE(_name) \ COND_CODE_1( \ CONFIG_LOG_RUNTIME_FILTERING, \ (_LOG_MODULE_DYNAMIC_DATA_CREATE(_name);), \ () \ ) #define _LOG_MODULE_DATA_CREATE(_name, _level) \ _LOG_MODULE_CONST_DATA_CREATE(_name, _level); \ _LOG_MODULE_DYNAMIC_DATA_COND_CREATE(_name) /** * @brief Create module-specific state and register the module with Logger. * * This macro normally must be used after including <logging/log.h> to * complete the initialization of the module. * * Module registration can be skipped in two cases: * * - The module consists of more than one file, and another file * invokes this macro. (LOG_MODULE_DECLARE() should be used instead * in all of the module's other files.) * - Instance logging is used and there is no need to create module entry. In * that case LOG_LEVEL_SET() should be used to set log level used within the * file. * * Macro accepts one or two parameters: * - module name * - optional log level. If not provided then default log level is used in * the file. * * Example usage: * - LOG_MODULE_REGISTER(foo, CONFIG_FOO_LOG_LEVEL) * - LOG_MODULE_REGISTER(foo) * * * @note The module's state is defined, and the module is registered, * only if LOG_LEVEL for the current source file is non-zero or * it is not defined and CONFIG_LOG_DEFAULT_LEVEL is non-zero. * In other cases, this macro has no effect. * @see LOG_MODULE_DECLARE */ #define LOG_MODULE_REGISTER(...) \ Z_LOG_EVAL( \ _LOG_LEVEL_RESOLVE(__VA_ARGS__), \ (_LOG_MODULE_DATA_CREATE(GET_ARG1(__VA_ARGS__), \ _LOG_LEVEL_RESOLVE(__VA_ARGS__))),\ ()/*Empty*/ \ ) \ LOG_MODULE_DECLARE(__VA_ARGS__) /** * @brief Macro for declaring a log module (not registering it). * * Modules which are split up over multiple files must have exactly * one file use LOG_MODULE_REGISTER() to create module-specific state * and register the module with the logger core. * * The other files in the module should use this macro instead to * declare that same state. (Otherwise, LOG_INF() etc. will not be * able to refer to module-specific state variables.) * * Macro accepts one or two parameters: * - module name * - optional log level. If not provided then default log level is used in * the file. * * Example usage: * - LOG_MODULE_DECLARE(foo, CONFIG_FOO_LOG_LEVEL) * - LOG_MODULE_DECLARE(foo) * * @note The module's state is declared only if LOG_LEVEL for the * current source file is non-zero or it is not defined and * CONFIG_LOG_DEFAULT_LEVEL is non-zero. In other cases, * this macro has no effect. * @see LOG_MODULE_REGISTER */ #define LOG_MODULE_DECLARE(...) \ extern const struct log_source_const_data \ LOG_ITEM_CONST_DATA(GET_ARG1(__VA_ARGS__)); \ extern struct log_source_dynamic_data \ LOG_ITEM_DYNAMIC_DATA(GET_ARG1(__VA_ARGS__)); \ \ static const struct log_source_const_data * \ __log_current_const_data __attribute__((unused)) = \ _LOG_LEVEL_RESOLVE(__VA_ARGS__) ? \ &LOG_ITEM_CONST_DATA(GET_ARG1(__VA_ARGS__)) : NULL; \ \ static struct log_source_dynamic_data * \ __log_current_dynamic_data __attribute__((unused)) = \ (_LOG_LEVEL_RESOLVE(__VA_ARGS__) && \ IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) ? \ &LOG_ITEM_DYNAMIC_DATA(GET_ARG1(__VA_ARGS__)) : NULL; \ \ static const u32_t __log_level __attribute__((unused)) = \ _LOG_LEVEL_RESOLVE(__VA_ARGS__) /** * @brief Macro for setting log level in the file or function where instance * logging API is used. * * @param level Level used in file or in function. * */ #define LOG_LEVEL_SET(level) \ static const u32_t __log_level __attribute__((unused)) = \ _LOG_LEVEL_RESOLVE(level) /** * @} */ #endif /* ZEPHYR_INCLUDE_LOGGING_LOG_H_ */ |