Imre Kis | 2ccd8e8 | 2021-10-08 11:21:14 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 | /* |
| 3 | * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #ifndef TRACE_H_ |
| 7 | #define TRACE_H_ |
| 8 | |
| 9 | #include "compiler.h" |
| 10 | |
| 11 | #define TRACE_LEVEL_NONE (0) |
| 12 | #define TRACE_LEVEL_ERROR (1) |
| 13 | #define TRACE_LEVEL_INFO (2) |
| 14 | #define TRACE_LEVEL_DEBUG (3) |
| 15 | |
| 16 | #ifndef TRACE_LEVEL |
| 17 | #define TRACE_LEVEL TRACE_LEVEL_ERROR |
| 18 | #endif /* TRACE_LEVEL */ |
| 19 | |
| 20 | /** |
| 21 | * no_trace_printf will be optimized out becase of the 'if (0)' but all the |
| 22 | * checks will still run against the format string and the parameters. |
| 23 | */ |
| 24 | #define no_trace_printf(func, line, level, fmt, ...) \ |
| 25 | do { \ |
| 26 | if (0) { \ |
| 27 | trace_printf(func, line, level, fmt, ##__VA_ARGS__); \ |
| 28 | } \ |
| 29 | } while (0) |
| 30 | |
| 31 | void trace_puts(const char *str); |
| 32 | void trace_printf(const char *func, int line, int level, const char *fmt, ...) __printf(4, 5); |
| 33 | |
| 34 | #if TRACE_LEVEL >= TRACE_LEVEL_ERROR |
| 35 | #define EMSG(...) trace_printf(__func__, __LINE__, TRACE_LEVEL_ERROR, __VA_ARGS__) |
| 36 | #else |
| 37 | #define EMSG(...) no_trace_printf(__func__, __LINE__, TRACE_LEVEL_ERROR, __VA_ARGS__) |
| 38 | #endif /* TRACE_LEVEL >= TRACE_LEVEL_ERROR */ |
| 39 | |
| 40 | #if TRACE_LEVEL >= TRACE_LEVEL_INFO |
| 41 | #define IMSG(...) trace_printf(__func__, __LINE__, TRACE_LEVEL_INFO, __VA_ARGS__) |
| 42 | #else |
| 43 | #define IMSG(...) no_trace_printf(__func__, __LINE__, TRACE_LEVEL_INFO, __VA_ARGS__) |
| 44 | #endif /* TRACE_LEVEL >= TRACE_LEVEL_INFO */ |
| 45 | |
| 46 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
| 47 | #define DMSG(...) trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__) |
| 48 | #else |
| 49 | #define DMSG(...) no_trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__) |
| 50 | #endif /* TRACE_LEVEL >= TRACE_LEVEL_DEBUG */ |
| 51 | |
| 52 | #endif /* TRACE_H_ */ |