Virgil IoT KIT
logger.h
Go to the documentation of this file.
1 // Copyright (C) 2015-2020 Virgil Security, Inc.
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // (1) Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // (2) Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in
14 // the documentation and/or other materials provided with the
15 // distribution.
16 //
17 // (3) Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
22 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 // DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 // IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
34 
68 #ifndef AP_SECURITY_SDK_LOGGER_H
69 #define AP_SECURITY_SDK_LOGGER_H
70 
71 #include <logger-config.h>
72 #include <stdarg.h>
73 #include <string.h>
74 
75 #ifdef __cplusplus
76 namespace VirgilIoTKit {
77 extern "C" {
78 #endif
79 
82 typedef enum {
86  VS_LOGLEV_INFO = 0x00,
87  VS_LOGLEV_FATAL = 0x10,
88  VS_LOGLEV_ALERT = 0x20,
90  VS_LOGLEV_ERROR = 0x40,
93  VS_LOGLEV_TRACE = 0x70,
95  VS_LOGLEV_DEBUG = 0xFD,
97 
98 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
99 
101 #define VS_LOG_GET_LOGLEVEL(LOGLEV_VARIABLE) (LOGLEV_VARIABLE) = vs_logger_get_loglev()
102 
103 #define VS_LOG_IS_LOGLEVEL(LOGLEV_VALUE) vs_logger_is_loglev(LOGLEV_VALUE)
104 
105 #define VS_LOG_SET_LOGLEVEL(LOGLEV_VALUE) vs_logger_set_loglev(LOGLEV_VALUE)
106 
107 
108 #if VS_IOT_LOGGER_USE_LIBRARY
109 
110 #include <stdbool.h>
111 #include <stdint.h>
112 #include <stdlib-config.h>
113 
114 
122 #define VS_LOG(LGLVL, FRMT, ...) vs_logger_message((LGLVL), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
123 
133 #define VS_LOG_HEX(LGLVL, PREFIX, BUF, SIZE) \
134  vs_logger_message_hex((LGLVL), __FILENAME__, __LINE__, (PREFIX), (BUF), (SIZE))
135 
145 bool
146 vs_log_thread_descriptor(const char *description);
147 
148 #ifdef __cplusplus
149 #define VS_LOG_MAKE_LEVEL(LEVEL) VirgilIoTKit::LEVEL
150 #else
151 #define VS_LOG_MAKE_LEVEL(LEVEL) LEVEL
152 #endif
153 
155 #define VS_LOG_INFO(FRMT, ...) \
156  vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_INFO), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
157 
158 #define VS_LOG_FATAL(FRMT, ...) \
159  vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_FATAL), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
160 
161 #define VS_LOG_ALERT(FRMT, ...) \
162  vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_ALERT), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
163 
164 #define VS_LOG_CRITICAL(FRMT, ...) \
165  vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_CRITICAL), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
166 
167 #define VS_LOG_ERROR(FRMT, ...) \
168  vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_ERROR), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
169 
170 #define VS_LOG_WARNING(FRMT, ...) \
171  vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_WARNING), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
172 
173 #define VS_LOG_NOTICE(FRMT, ...) \
174  vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_NOTICE), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
175 
176 #define VS_LOG_TRACE(FRMT, ...) \
177  vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_TRACE), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
178 
179 #define VS_LOG_DEBUG(FRMT, ...) \
180  vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_DEBUG), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
181 
182 // Functions
183 
191 bool
193 
206 void
207 vs_logger_message(vs_log_level_t level, const char *cur_filename, uint32_t line_num, const char *log_format, ...);
208 
216 void
217 vs_logger_init(vs_log_level_t log_level);
218 
228 
236 
244 bool
246 
259 void
261  const char *cur_filename,
262  uint32_t line_num,
263  const char *prefix,
264  const void *data_buf,
265  const uint16_t data_size);
266 
267 #elif VS_IOT_LOGGER_USE_LIBRARY != 1 && defined(VS_IOT_LOGGER_FUNCTION)
268 
269 #define vs_logger_last_result() true
270 #define vs_logger_init(log_level) (void)log_level;
271 #define vs_logger_set_loglev(log_level) (void)log_level;
272 #define vs_logger_get_loglev() VS_LOGLEV_NO_LOGGER
273 #define vs_logger_is_loglev(level) ((level) == VS_LOGLEV_NO_LOGGER)
274 
275 #define vs_logger_message_hex(level, cur_filename, line_num, prefix, data_buf, data_size) \
276  do { \
277  (void)level; \
278  (void)cur_filename; \
279  (void)line_num; \
280  (void)prefix; \
281  (void)data_buf; \
282  (void)data_size; \
283  } while (0)
284 
285 #define vs_logger_message(level, cur_filename, line_num, log_format, ...) \
286  do { \
287  (void)level; \
288  (void)cur_filename; \
289  (void)line_num; \
290  VS_IOT_LOGGER_FUNCTION((log_format), ##__VA_ARGS__); \
291  VS_IOT_LOGGER_FUNCTION(VS_IOT_LOGGER_EOL); \
292  } while (0)
293 
294 #else // VS_IOT_LOGGER_USE_LIBRARY != 1 && !defined(VS_IOT_LOGGER_FUNCTION)
295 
296 #include "macro_va_args.h"
297 
298 #define vs_logger_last_result() true
299 #define vs_logger_init(log_level) (void)log_level;
300 #define vs_logger_set_loglev(log_level) (void)log_level;
301 #define vs_logger_get_loglev() VS_LOGLEV_NO_LOGGER
302 #define vs_logger_is_loglev(level) ((level) == VS_LOGLEV_NO_LOGGER)
303 #define vs_log_thread_descriptor() (true)
304 
305 #define VS_IOT_LOGGER_VOID(a) (void)a;
306 
307 #define VS_LOG(LGLVL, FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
308 #define VS_LOG_HEX(LGLVL, PREFIX, BUF, SIZE) VS_IOT_MAP(VS_IOT_LOGGER_VOID, PREFIX, BUF, SIZE)
309 #define VS_LOG_INFO(FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
310 #define VS_LOG_FATAL(FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
311 #define VS_LOG_ALERT(FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
312 #define VS_LOG_CRITICAL(FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
313 #define VS_LOG_ERROR(FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
314 #define VS_LOG_WARNING(FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
315 #define VS_LOG_NOTICE(FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
316 #define VS_LOG_TRACE(FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
317 #define VS_LOG_DEBUG(FRMT, ...) VS_IOT_MAP(VS_IOT_LOGGER_VOID, FRMT, ##__VA_ARGS__)
318 
319 #endif // VS_IOT_LOGGER_USE_LIBRARY
320 
321 #ifdef __cplusplus
322 } // extern "C"
323 } // namespace VirgilIoTKit
324 #endif
325 
326 #endif // AP_SECURITY_SDK_LOGGER_H
VS_LOGLEV_WARNING
@ VS_LOGLEV_WARNING
Warning messages.
Definition: logger.h:91
VS_LOGLEV_DEBUG
@ VS_LOGLEV_DEBUG
Debug messages.
Definition: logger.h:95
VS_LOGLEV_ALERT
@ VS_LOGLEV_ALERT
Alert messages.
Definition: logger.h:88
vs_logger_message
void vs_logger_message(vs_log_level_t level, const char *cur_filename, uint32_t line_num, const char *log_format,...)
Log text message.
vs_logger_get_loglev
vs_log_level_t vs_logger_get_loglev(void)
Get current logging level.
VS_LOGLEV_FATAL
@ VS_LOGLEV_FATAL
Fatal messages.
Definition: logger.h:87
vs_logger_set_loglev
vs_log_level_t vs_logger_set_loglev(vs_log_level_t new_level)
Set current logging level.
VS_LOGLEV_TRACE
@ VS_LOGLEV_TRACE
Trace messages.
Definition: logger.h:93
vs_log_level_t
vs_log_level_t
Logging level.
Definition: logger.h:82
vs_logger_init
void vs_logger_init(vs_log_level_t log_level)
Initialize logging level.
VS_LOGLEV_ERROR
@ VS_LOGLEV_ERROR
Error messages.
Definition: logger.h:90
vs_logger_last_result
bool vs_logger_last_result(void)
Last result.
vs_log_thread_descriptor
bool vs_log_thread_descriptor(const char *description)
Set current thread description.
vs_logger_message_hex
void vs_logger_message_hex(vs_log_level_t level, const char *cur_filename, uint32_t line_num, const char *prefix, const void *data_buf, const uint16_t data_size)
Log HEX buffer.
VS_LOGLEV_NOTICE
@ VS_LOGLEV_NOTICE
Notifications.
Definition: logger.h:92
VS_LOGLEV_CRITICAL
@ VS_LOGLEV_CRITICAL
Critical messages.
Definition: logger.h:89
VS_LOGLEV_NO_LOGGER
@ VS_LOGLEV_NO_LOGGER
Logging is disabled.
Definition: logger.h:84
VS_LOGLEV_UNKNOWN
@ VS_LOGLEV_UNKNOWN
Invalid logging level.
Definition: logger.h:83
vs_logger_is_loglev
bool vs_logger_is_loglev(vs_log_level_t level)
Check that specified logging level is enabled.
VS_LOGLEV_INFO
@ VS_LOGLEV_INFO
Information messages.
Definition: logger.h:86