NUGU SDK Linux  1.7.6
nugu_log.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 SK Telecom Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __NUGU_LOG_H__
18 #define __NUGU_LOG_H__
19 
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <nugu.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
48 #ifndef NUGU_ANSI_COLOR_NORMAL
49 #ifdef NUGU_LOG_USE_ANSICOLOR
50 #define NUGU_ANSI_COLOR_NORMAL "\x1b[0m"
51 #else
52 #define NUGU_ANSI_COLOR_NORMAL ""
53 #endif
54 #endif
55 
56 #ifndef NUGU_ANSI_COLOR_BLACK
57 #define NUGU_ANSI_COLOR_BLACK "\x1b[0;30m"
58 #endif
59 
60 #ifndef NUGU_ANSI_COLOR_RED
61 #define NUGU_ANSI_COLOR_RED "\x1b[0;31m"
62 #endif
63 
64 #ifndef NUGU_ANSI_COLOR_GREEN
65 #define NUGU_ANSI_COLOR_GREEN "\x1b[0;32m"
66 #endif
67 
68 #ifndef NUGU_ANSI_COLOR_BROWN
69 #define NUGU_ANSI_COLOR_BROWN "\x1b[0;33m"
70 #endif
71 
72 #ifndef NUGU_ANSI_COLOR_BLUE
73 #define NUGU_ANSI_COLOR_BLUE "\x1b[0;34m"
74 #endif
75 
76 #ifndef NUGU_ANSI_COLOR_MAGENTA
77 #define NUGU_ANSI_COLOR_MAGENTA "\x1b[0;35m"
78 #endif
79 
80 #ifndef NUGU_ANSI_COLOR_CYAN
81 #define NUGU_ANSI_COLOR_CYAN "\x1b[0;36m"
82 #endif
83 
84 #ifndef NUGU_ANSI_COLOR_LIGHTGRAY
85 #define NUGU_ANSI_COLOR_LIGHTGRAY "\x1b[0;37m"
86 #endif
87 
88 #ifndef NUGU_ANSI_COLOR_DARKGRAY
89 #define NUGU_ANSI_COLOR_DARKGRAY "\x1b[1;30m"
90 #endif
91 
92 #ifndef NUGU_ANSI_COLOR_LIGHTRED
93 #define NUGU_ANSI_COLOR_LIGHTRED "\x1b[1;31m"
94 #endif
95 
96 #ifndef NUGU_ANSI_COLOR_LIGHTGREEN
97 #define NUGU_ANSI_COLOR_LIGHTGREEN "\x1b[1;32m"
98 #endif
99 
100 #ifndef NUGU_ANSI_COLOR_YELLOW
101 #define NUGU_ANSI_COLOR_YELLOW "\x1b[1;33m"
102 #endif
103 
104 #ifndef NUGU_ANSI_COLOR_LIGHTBLUE
105 #define NUGU_ANSI_COLOR_LIGHTBLUE "\x1b[1;34m"
106 #endif
107 
108 #ifndef NUGU_ANSI_COLOR_LIGHTMAGENTA
109 #define NUGU_ANSI_COLOR_LIGHTMAGENTA "\x1b[1;35m"
110 #endif
111 
112 #ifndef NUGU_ANSI_COLOR_LIGHTCYAN
113 #define NUGU_ANSI_COLOR_LIGHTCYAN "\x1b[1;36m"
114 #endif
115 
116 #ifndef NUGU_ANSI_COLOR_WHITE
117 #define NUGU_ANSI_COLOR_WHITE "\x1b[1;37m"
118 #endif
119 
133 };
134 
146 };
147 
155  NUGU_LOG_PREFIX_PID = (1 << 1),
156  NUGU_LOG_PREFIX_TID = (1 << 2),
161  NUGU_LOG_PREFIX_LINE = (1 << 7),
170 };
171 
196  NUGU_LOG_MODULE_ALL = 0xFF
197 };
198 
199 #ifndef NUGU_LOG_MODULE
206 #define NUGU_LOG_MODULE NUGU_LOG_MODULE_DEFAULT
207 #endif
208 
222 NUGU_API void nugu_log_print(enum nugu_log_module module,
223  enum nugu_log_level level, const char *filename,
224  const char *funcname, int line, const char *format,
225  ...);
226 
238 typedef void (*nugu_log_handler)(enum nugu_log_module module,
239  enum nugu_log_level level, const char *prefix,
240  const char *msg, void *user_data);
241 
246 NUGU_API int nugu_log_set_system(enum nugu_log_system log_system);
247 
253 
260 NUGU_API int nugu_log_set_handler(nugu_log_handler handler, void *user_data);
261 
267 NUGU_API void nugu_log_set_prefix_fields(enum nugu_log_prefix field_set);
268 
275 
281 NUGU_API void nugu_log_set_modules(unsigned int bitset);
282 
288 NUGU_API unsigned int nugu_log_get_modules(void);
289 
296 NUGU_API void nugu_log_set_level(enum nugu_log_level level);
297 
302 NUGU_API enum nugu_log_level nugu_log_get_level(void);
303 
308 NUGU_API void nugu_log_set_protocol_line_limit(int length);
309 
315 
325 NUGU_API void nugu_hexdump(enum nugu_log_module module, const uint8_t *data,
326  size_t data_size, const char *header,
327  const char *footer, const char *lineindent);
328 
329 #ifdef __cplusplus
330 }
331 #endif
332 
333 #ifndef __FILENAME__
334 #define __FILENAME__ __FILE__
335 #endif
336 
345 #ifdef _WIN32
346 #define nugu_log(module, level, fmt, ...) \
347  nugu_log_print(module, level, __FILENAME__, __FUNCSIG__, __LINE__, \
348  fmt, ##__VA_ARGS__)
349 #else
350 #define nugu_log(module, level, fmt, ...) \
351  nugu_log_print(module, level, __FILENAME__, __PRETTY_FUNCTION__, \
352  __LINE__, fmt, ##__VA_ARGS__)
353 #endif
354 
355 #ifdef CONFIG_RELEASE
356 #define nugu_dbg(fmt, ...)
357 #define nugu_info(fmt, ...)
358 #define nugu_warn(fmt, ...)
359 #else
360 
366 #define nugu_dbg(fmt, ...) \
367  nugu_log(NUGU_LOG_MODULE, NUGU_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
368 
374 #define nugu_info(fmt, ...) \
375  nugu_log(NUGU_LOG_MODULE, NUGU_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
376 
382 #define nugu_warn(fmt, ...) \
383  nugu_log(NUGU_LOG_MODULE, NUGU_LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
384 #endif
385 
391 #define nugu_error(fmt, ...) \
392  nugu_log(NUGU_LOG_MODULE, NUGU_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
393 
394 #ifndef NUGU_ANSI_COLOR_SEND
395 #ifdef NUGU_LOG_USE_ANSICOLOR
396 #define NUGU_ANSI_COLOR_SEND NUGU_ANSI_COLOR_BROWN
397 #else
398 #define NUGU_ANSI_COLOR_SEND ""
399 #endif
400 #endif
401 
402 #ifndef NUGU_ANSI_COLOR_RECV
403 #ifdef NUGU_LOG_USE_ANSICOLOR
404 #define NUGU_ANSI_COLOR_RECV NUGU_ANSI_COLOR_GREEN
405 #else
406 #define NUGU_ANSI_COLOR_RECV ""
407 #endif
408 #endif
409 
410 #ifndef NUGU_LOG_MARK_SEND
411 #define NUGU_LOG_MARK_SEND "--> "
412 #endif
413 
414 #ifndef NUGU_LOG_MARK_RECV
415 #define NUGU_LOG_MARK_RECV "<-- "
416 #endif
417 
425 #define nugu_log_protocol_send(level, fmt, ...) \
426  nugu_log_print(NUGU_LOG_MODULE_PROTOCOL, level, NULL, NULL, -1, \
427  NUGU_ANSI_COLOR_SEND NUGU_LOG_MARK_SEND fmt \
428  NUGU_ANSI_COLOR_NORMAL, \
429  ##__VA_ARGS__)
430 
438 #define nugu_log_protocol_recv(level, fmt, ...) \
439  nugu_log_print(NUGU_LOG_MODULE_PROTOCOL, level, NULL, NULL, -1, \
440  NUGU_ANSI_COLOR_RECV NUGU_LOG_MARK_RECV fmt \
441  NUGU_ANSI_COLOR_NORMAL, \
442  ##__VA_ARGS__)
443 
448 #define nugu_error_nomem() nugu_error("Not enough memory")
449 
454 #endif
nugu_log_prefix
logging prefix
Definition: nugu_log.h:152
NUGU_API enum nugu_log_prefix nugu_log_get_prefix_fields(void)
Get the additional information fields.
NUGU_API unsigned int nugu_log_get_modules(void)
Get the bitset of modules.
NUGU_API enum nugu_log_system nugu_log_get_system(void)
Get logging backend system.
NUGU_API enum nugu_log_level nugu_log_get_level(void)
Get log level.
NUGU_API void nugu_hexdump(enum nugu_log_module module, const uint8_t *data, size_t data_size, const char *header, const char *footer, const char *lineindent)
Hexdump the specific data to stderr.
NUGU_API int nugu_log_get_protocol_line_limit(void)
Get the maximum length of each protocol log line.
NUGU_API void nugu_log_set_prefix_fields(enum nugu_log_prefix field_set)
Set the additional information fields.
NUGU_API void nugu_log_print(enum nugu_log_module module, enum nugu_log_level level, const char *filename, const char *funcname, int line, const char *format,...)
logging function
NUGU_API int nugu_log_set_handler(nugu_log_handler handler, void *user_data)
Set custom log handler.
nugu_log_level
log levels.
Definition: nugu_log.h:128
NUGU_API void nugu_log_set_protocol_line_limit(int length)
Set the maximum length of each protocol log line.
nugu_log_system
Back-end system that supports log output.
Definition: nugu_log.h:140
NUGU_API void nugu_log_set_level(enum nugu_log_level level)
Set minimum log level(debug, info, warning, error) For example, if set the level to info,...
void(* nugu_log_handler)(enum nugu_log_module module, enum nugu_log_level level, const char *prefix, const char *msg, void *user_data)
Custom log hook handler.
Definition: nugu_log.h:238
NUGU_API void nugu_log_set_modules(unsigned int bitset)
Set the bitset of modules.
NUGU_API int nugu_log_set_system(enum nugu_log_system log_system)
Set logging backend system.
nugu_log_module
logging modules
Definition: nugu_log.h:177
@ NUGU_LOG_PREFIX_PID
Definition: nugu_log.h:155
@ NUGU_LOG_PREFIX_LEVEL
Definition: nugu_log.h:157
@ NUGU_LOG_PREFIX_FILENAME
Definition: nugu_log.h:159
@ NUGU_LOG_PREFIX_FILEPATH
Definition: nugu_log.h:158
@ NUGU_LOG_PREFIX_ALL
Definition: nugu_log.h:167
@ NUGU_LOG_PREFIX_LINE
Definition: nugu_log.h:161
@ NUGU_LOG_PREFIX_DEFAULT
Definition: nugu_log.h:162
@ NUGU_LOG_PREFIX_TIMESTAMP
Definition: nugu_log.h:154
@ NUGU_LOG_PREFIX_FUNCTION
Definition: nugu_log.h:160
@ NUGU_LOG_PREFIX_NONE
Definition: nugu_log.h:153
@ NUGU_LOG_PREFIX_TID
Definition: nugu_log.h:156
@ NUGU_LOG_LEVEL_ERROR
Definition: nugu_log.h:129
@ NUGU_LOG_LEVEL_WARNING
Definition: nugu_log.h:130
@ NUGU_LOG_LEVEL_DEBUG
Definition: nugu_log.h:132
@ NUGU_LOG_LEVEL_INFO
Definition: nugu_log.h:131
@ NUGU_LOG_SYSTEM_NONE
Definition: nugu_log.h:144
@ NUGU_LOG_SYSTEM_CUSTOM
Definition: nugu_log.h:145
@ NUGU_LOG_SYSTEM_STDERR
Definition: nugu_log.h:141
@ NUGU_LOG_SYSTEM_STDOUT
Definition: nugu_log.h:142
@ NUGU_LOG_SYSTEM_SYSLOG
Definition: nugu_log.h:143
@ NUGU_LOG_MODULE_NETWORK_TRACE
Definition: nugu_log.h:180
@ NUGU_LOG_MODULE_NETWORK
Definition: nugu_log.h:179
@ NUGU_LOG_MODULE_APPLICATION
Definition: nugu_log.h:184
@ NUGU_LOG_MODULE_DEFAULT
Definition: nugu_log.h:178
@ NUGU_LOG_MODULE_PROTOCOL
Definition: nugu_log.h:182
@ NUGU_LOG_MODULE_PRESET_DEFAULT
Definition: nugu_log.h:189
@ NUGU_LOG_MODULE_ALL
Definition: nugu_log.h:196
@ NUGU_LOG_MODULE_PRESET_SDK_DEFAULT
Definition: nugu_log.h:185
@ NUGU_LOG_MODULE_AUDIO
Definition: nugu_log.h:181
@ NUGU_LOG_MODULE_PROFILING
Definition: nugu_log.h:183
@ NUGU_LOG_MODULE_PRESET_NETWORK
Definition: nugu_log.h:192