HPM APP
HPMicro Application solution
monitor_log.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2024 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef MONITOR_LOG_H
9 #define MONITOR_LOG_H
10 
11 #include <stdio.h>
12 #include "monitor_kconfig.h"
13 
14 /* DEBUG level */
15 #define MONITOR_DBG_ERROR 0
16 #define MONITOR_DBG_WARNING 1
17 #define MONITOR_DBG_INFO 2
18 #define MONITOR_DBG_LOG 3
19 
20 #ifndef MONITOR_DBG_TAG
21 #define MONITOR_DBG_TAG "MONITOR"
22 #endif
23 /*
24  * The color for terminal (foreground)
25  * BLACK 30
26  * RED 31
27  * GREEN 32
28  * YELLOW 33
29  * BLUE 34
30  * PURPLE 35
31  * CYAN 36
32  * WHITE 37
33  */
34 
35 #ifdef CONFIG_MONITOR_PRINTF_COLOR_ENABLE
36 #define _MONITOR_DBG_COLOR(n) CONFIG_MONITOR_PRINTF("\033[" #n "m")
37 #define _MONITOR_DBG_LOG_HDR(lvl_name, color_n) \
38  CONFIG_MONITOR_PRINTF("\033[" #color_n "m[" lvl_name "/" MONITOR_DBG_TAG "] ")
39 #define _MONITOR_DBG_LOG_X_END \
40  CONFIG_MONITOR_PRINTF("\033[0m")
41 #else
42 #define _MONITOR_DBG_COLOR(n)
43 #define _MONITOR_DBG_LOG_HDR(lvl_name, color_n) \
44  CONFIG_MONITOR_PRINTF("[" lvl_name "/" MONITOR_DBG_TAG "] ")
45 #define _MONITOR_DBG_LOG_X_END
46 #endif
47 
48 #define monitor_dbg_log_line(lvl, color_n, fmt, ...) \
49  do { \
50  _MONITOR_DBG_LOG_HDR(lvl, color_n); \
51  CONFIG_MONITOR_PRINTF(fmt, ##__VA_ARGS__); \
52  _MONITOR_DBG_LOG_X_END; \
53  } while (0)
54 
55 #if (CONFIG_MONITOR_DBG_LEVEL >= MONITOR_DBG_LOG)
56 #define MONITOR_LOG_DBG(fmt, ...) monitor_dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
57 #else
58 #define MONITOR_LOG_DBG(...)
59 #endif
60 
61 #if (CONFIG_MONITOR_DBG_LEVEL >= MONITOR_DBG_INFO)
62 #define MONITOR_LOG_INFO(fmt, ...) monitor_dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
63 #else
64 #define MONITOR_LOG_INFO(...)
65 #endif
66 
67 #if (CONFIG_MONITOR_DBG_LEVEL >= MONITOR_DBG_WARNING)
68 #define MONITOR_LOG_WRN(fmt, ...) monitor_dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
69 #else
70 #define MONITOR_LOG_WRN(...)
71 #endif
72 
73 #if (CONFIG_MONITOR_DBG_LEVEL >= MONITOR_DBG_ERROR)
74 #define MONITOR_LOG_ERR(fmt, ...) monitor_dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
75 #else
76 #define MONITOR_LOG_ERR(...)
77 #endif
78 
79 #if (CONFIG_MONITOR_DBG_LEVEL >= MONITOR_DBG_LOG)
80 #define MONITOR_LOG_RAW(...) CONFIG_MONITOR_PRINTF(__VA_ARGS__)
81 #else
82 #define MONITOR_LOG_RAW(...)
83 #endif
84 
85 #endif /* MONITOR_LOG_H */