|
| 1 | +/* |
| 2 | + * Copyright IBM Corp. All Rights Reserved. |
| 3 | + * Copyright 2020 Intel Corporation |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef LOG_DEFINES |
| 9 | +#define LOG_DEFINES |
| 10 | + |
| 11 | +#ifndef TAG |
| 12 | +#define TAG "" |
| 13 | +#endif |
| 14 | + |
| 15 | +#define LOC_FMT " (%s:%d) " |
| 16 | + |
| 17 | +#define NRM "\x1B[0m" |
| 18 | +#define CYN "\x1B[36m" |
| 19 | +#define YEL "\x1B[33m" |
| 20 | +#define RED "\x1B[31m" |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | + |
| 24 | +/* |
| 25 | + * Note: `DO_DEBUG` is set to `false` by default, so no `LOG_DEBUG` is displayed. |
| 26 | + * At compile time, this behaviour can be changed by defining `-DDO_DEBUG=true` before the header is |
| 27 | + * included. In SGX deployments, such define should be set "only" when the `SGX_BUILD` environment |
| 28 | + * variable is set to `DEBUG`. Finally, notice that `DO_INFO`, `DO_WARNING` and `DO_ERROR` are set |
| 29 | + * to `true` by default. So, unless they are explictly disabled at compile time, the respective logs |
| 30 | + * will be displayed. |
| 31 | + */ |
| 32 | + |
| 33 | +#ifndef DO_DEBUG |
| 34 | +#define DO_DEBUG false |
| 35 | +#endif |
| 36 | + |
| 37 | +#ifndef DO_INFO |
| 38 | +#define DO_INFO true |
| 39 | +#endif |
| 40 | + |
| 41 | +#ifndef DO_WARNING |
| 42 | +#define DO_WARNING true |
| 43 | +#endif |
| 44 | + |
| 45 | +#ifndef DO_ERROR |
| 46 | +#define DO_ERROR true |
| 47 | +#endif |
| 48 | + |
| 49 | +#ifdef __cplusplus |
| 50 | +extern "C" { |
| 51 | +#endif |
| 52 | +int printf(const char* fmt, ...); |
| 53 | +#ifdef __cplusplus |
| 54 | +} |
| 55 | +#endif |
| 56 | + |
| 57 | +#if DO_DEBUG == true |
| 58 | +#define LOG_DEBUG(fmt, ...) \ |
| 59 | + printf(CYN "DEBUG " LOC_FMT TAG YEL fmt NRM "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
| 60 | +#else // DO_DEBUG |
| 61 | +#define LOG_DEBUG(fmt, ...) |
| 62 | +#endif // DO_DEBUG |
| 63 | + |
| 64 | +#if DO_INFO == true |
| 65 | +#define LOG_INFO(fmt, ...) \ |
| 66 | + printf(CYN "INFO " LOC_FMT TAG NRM fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
| 67 | +#else // DO_INFO |
| 68 | +#define LOG_INFO(fmt, ...) |
| 69 | +#endif // DO_INFO |
| 70 | + |
| 71 | +#if DO_WARNING == true |
| 72 | +#define LOG_WARNING(fmt, ...) \ |
| 73 | + printf(CYN "WARNING " LOC_FMT TAG RED fmt NRM "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
| 74 | +#else // DO_WARNING |
| 75 | +#define LOG_WARNING(fmt, ...) |
| 76 | +#endif // DO_WARNING |
| 77 | + |
| 78 | +#if DO_ERROR == true |
| 79 | +#define LOG_ERROR(fmt, ...) \ |
| 80 | + printf(CYN "ERROR " LOC_FMT TAG RED fmt NRM "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
| 81 | +#else // DO_ERROR |
| 82 | +#define LOG_ERROR(fmt, ...) |
| 83 | +#endif // DO_ERROR |
| 84 | + |
| 85 | +#define ERROR_LOG_STRING "error log - omitted" |
| 86 | + |
| 87 | +#endif // LOG_DEFINES |
0 commit comments