log_module.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
21 #ifndef LOG_MODULE_H
22 #define LOG_MODULE_H
23 
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 
28 #include "log.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
39 #ifndef LOG_ERROR_ANSI_COLOR_CODE
40 #define LOG_ERROR_ANSI_COLOR_CODE ("\033[1;31m")
41 #endif
42 
48 #ifndef LOG_WARNING_ANSI_COLOR_CODE
49 #define LOG_WARNING_ANSI_COLOR_CODE ("\033[1;33m")
50 #endif
51 
57 #ifndef LOG_INFO_ANSI_COLOR_CODE
58 #define LOG_INFO_ANSI_COLOR_CODE ("\033[1m")
59 #endif
60 
66 #ifndef LOG_DEBUG_ANSI_COLOR_CODE
67 #define LOG_DEBUG_ANSI_COLOR_CODE ("\033[0;32m")
68 #endif
69 
73 #define LOG_RESET_ANSI_COLOR_CODE ("\033[0m")
74 
80 static const char * const _ansi_codes[] =
81 {
86 };
87 
94 static inline void log_write(unsigned level, const char *format, ...)
95 {
96  assert(level > 0);
97 
98  printf("%s", _ansi_codes[level]);
99  va_list args;
100  va_start(args, format);
101  /* Temporarily disable clang format-nonliteral warning */
102 #ifdef __clang__
103 #pragma clang diagnostic push
104 #pragma clang diagnostic ignored "-Wformat-nonliteral"
105 #endif /* clang */
106  vprintf(format, args);
107 #ifdef __clang__
108 #pragma clang diagnostic pop
109 #endif /* clang */
110  va_end(args);
112 
113 #ifdef MODULE_NEWLIB
114  /* no fflush on msp430 */
115  fflush(stdout);
116 #endif
117 }
118 
119 #ifdef __cplusplus
120 }
121 #endif
122 
123 #endif /* LOG_MODULE_H */
Informational log level, will print purely informational messages like successful system bootup...
Definition: log.h:58
#define LOG_DEBUG_ANSI_COLOR_CODE
Default ANSI color escape code for debug logs.
Definition: log_module.h:67
int printf(const char *format,...)
System standard printf function.
Error log level, will print only critical, non-recoverable errors like hardware initialization failur...
Definition: log.h:53
#define LOG_RESET_ANSI_COLOR_CODE
ANSI color escape code used for resetting color.
Definition: log_module.h:73
Warning log level, will print warning messages for temporary errors.
Definition: log.h:56
POSIX.1-2008 compliant version of the assert macro.
#define LOG_ERROR_ANSI_COLOR_CODE
Default ANSI color escape code for error logs.
Definition: log_module.h:40
static const char *const _ansi_codes[]
ANSI color escape codes array.
Definition: log_module.h:80
#define LOG_WARNING_ANSI_COLOR_CODE
Default ANSI color escape code for warning logs.
Definition: log_module.h:49
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
void log_write(unsigned level, const char *format,...)
log_write overridden function
#define LOG_INFO_ANSI_COLOR_CODE
Default ANSI color escape code for info logs.
Definition: log_module.h:58
System logging header.
Debug log level, printing developer stuff considered too verbose for production use.
Definition: log.h:61