1 /*
2 * Copyright (c) 2017 TDK Invensense
3 *
4 * SPDX-License-Identifier: BSD 3-Clause
5 */
6
7 /** @defgroup Message Message
8 * @brief Utility functions to display and redirect diagnostic messages
9 *
10 * Use INV_MSG_DISABLE or INV_MSG_ENABLE define before including
11 * this header to enable/disable messages for a compilation unit.
12 *
13 * Under Linux, Windows or Arduino, messages are enabled by default.
14 * Use INV_MSG_DISABLE to disable them.
15 *
16 * Under orther environmment, message are disabled by default.
17 * Use INV_MSG_ENABLE to disable them.
18 *
19 * @ingroup EmbUtils
20 * @{
21 */
22
23 #ifndef _INV_MESSAGE_H_
24 #define _INV_MESSAGE_H_
25
26 #include "InvExport.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 #include <stdarg.h>
33
34 /** @brief For eMD target, disable log by default
35 * If compile switch is set for a compilation unit
36 * messages will be totally disabled by default
37 */
38 #if !defined(__linux) && !defined(_WIN32) && !defined(ARDUINO)
39 #define INV_MSG_DISABLE 1
40 #endif
41
42
43 /** @brief Allow to force enabling messaging using INV_MSG_ENABLE define */
44 #ifdef INV_MSG_ENABLE
45 #undef INV_MSG_DISABLE
46 #endif
47
48
49 /** @brief Helper macro for calling inv_msg()
50 * If INV_MSG_DISABLE compile switch is set for a compilation unit
51 * messages will be totally disabled
52 */
53 #define INV_MSG(level, ...) _INV_MSG(level, __VA_ARGS__)
54
55 /** @brief Helper macro for calling inv_msg_setup()
56 * If INV_MSG_DISABLE compile switch is set for a compilation unit
57 * messages will be totally disabled
58 */
59 #define INV_MSG_SETUP(level, printer) _INV_MSG_SETUP(level, printer)
60
61 /** @brief Helper macro for calling inv_msg_setup_level()
62 * If INV_MSG_DISABLE compile switch is set for a compilation unit
63 * messages will be totally disabled
64 */
65 #define INV_MSG_SETUP_LEVEL(level) _INV_MSG_SETUP_LEVEL(level)
66
67 /** @brief Helper macro for calling inv_msg_setup_default()
68 * If INV_MSG_DISABLE compile switch is set for a compilation unit
69 * messages will be totally disabled
70 */
71 #define INV_MSG_SETUP_DEFAULT() _INV_MSG_SETUP_DEFAULT()
72
73 /** @brief Return current level
74 * @warning This macro may expand as a function call
75 */
76 #define INV_MSG_LEVEL _INV_MSG_LEVEL
77
78 #if defined(INV_MSG_DISABLE)
79 #define _INV_MSG(level, ...) (void)0
80 #define _INV_MSG_SETUP(level, printer) (void)0
81 #define _INV_MSG_SETUP_LEVEL(level) (void)0
82 #define _INV_MSG_LEVEL INV_MSG_LEVEL_OFF
83 #else
84 #define _INV_MSG(level, ...) inv_msg(level, __VA_ARGS__)
85 #define _INV_MSG_SETUP(level, printer) inv_msg_setup(level, printer)
86 #define _INV_MSG_SETUP_LEVEL(level) inv_msg_setup(level, inv_msg_printer_default)
87 #define _INV_MSG_SETUP_DEFAULT() inv_msg_setup_default()
88 #define _INV_MSG_LEVEL inv_msg_get_level()
89 #endif
90
91 /** @brief message level definition
92 */
93 enum inv_msg_level {
94 INV_MSG_LEVEL_OFF = 0,
95 INV_MSG_LEVEL_ERROR,
96 INV_MSG_LEVEL_WARNING,
97 INV_MSG_LEVEL_INFO,
98 INV_MSG_LEVEL_VERBOSE,
99 INV_MSG_LEVEL_DEBUG,
100 INV_MSG_LEVEL_MAX
101 };
102
103
104 /** @brief Prototype for print routine function
105 */
106 typedef void (*inv_msg_printer_t)(int level, const char * str, va_list ap);
107
108
109 /** @brief Set message level and printer function
110 * @param[in] level only message above level will be passed to printer function
111 * @param[in] printer user provided function in charge printing message
112 * @return none
113 */
114 void INV_EXPORT inv_msg_setup(int level, inv_msg_printer_t printer);
115
116
117 /** @brief Default printer function that display messages to stderr
118 * Function uses stdio. Care must be taken on embeded platfrom.
119 * Function does nothing with IAR compiler.
120 * @return none
121 */
122 void INV_EXPORT inv_msg_printer_default(int level, const char * str, va_list ap);
123
124 /** @brief Set message level
125 * Default printer function will be used.
126 * @param[in] level only message above level will be passed to printer function
127 * @return none
128 */
inv_msg_setup_level(int level)129 static inline void inv_msg_setup_level(int level)
130 {
131 inv_msg_setup(level, inv_msg_printer_default);
132 }
133
134
135 /** @brief Set default message level and printer
136 * @return none
137 */
inv_msg_setup_default(void)138 static inline void inv_msg_setup_default(void)
139 {
140 inv_msg_setup(INV_MSG_LEVEL_INFO, inv_msg_printer_default);
141 }
142
143 /** @brief Return current message level
144 * @return current message level
145 */
146 int INV_EXPORT inv_msg_get_level(void);
147
148 /** @brief Display a message (through means of printer function)
149 * @param[in] level for the message
150 * @param[in] str message string
151 * @param[in] ... optional arguments
152 * @return none
153 */
154 void INV_EXPORT inv_msg(int level, const char * str, ...);
155
156
157 #ifdef __cplusplus
158 }
159 #endif
160
161 #endif /* INV_MESSAGE_H_ */
162
163 /** @} */
164