-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlogger.h
141 lines (129 loc) · 3.25 KB
/
logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Wirepas Oy licensed under Apache License, Version 2.0
*
* See file LICENSE for full license details.
*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
/**
* Simple logger lib to print only relevant log messages.
*
* Usage is as followed:
*
* #define LOG_MODULE_NAME "module_name"
* #define MAX_LOG_LEVEL INFO_LOG_LEVEL
* //#define PRINT_BUFFERS // Un-comment to print buffers
* #include "logger.h"
*
* ...
* LOGD("This is a debug message not printed with current MAX_LOG_LEVEL n=%d",
* n);
* ...
* LOGI("This is an info message printed with current MAX_LOG_LEVEL n=%d", n);
* ...
*
*/
/*
* Both following functions are declared extern instead of a dedicated
* header file to avoid multiple inclusion headers for that module.
* This Logger lib header is not a real lib as depending on parameter
* defined before including this file: MAX_LOG_LEVEL and LOG_MODULE_NAME
*/
/**
* \brief Implemented in platform specific part to print a LOG
* \param level
* Log level as a letter: D, I, W, E
* \param moule
* Module nam
* \param format
* Message to print
* \param args
* Argument for the string format
*/
extern void Platform_LOG(char level, char * module, char * format, va_list args);
/**
* \brief Implemented in platform specific part to print a buffer
* \param buffer
* the buffer to print
* \param size
* the size of the buffer to print
*/
extern void Platform_print_buffer(uint8_t * buffer, int size);
/**
* Macros to define several level of Log: Debug(3), Info(2), Warning(1),
* Error(0)
*/
#define DEBUG_LOG_LEVEL 3
#define INFO_LOG_LEVEL 2
#define WARNING_LOG_LEVEL 1
#define ERROR_LOG_LEVEL 0
#define NO_LOG_LEVEL -1
/**
* Only logs with a level lower or equal to MAX_LOG_LEVEL will be printed
*
* \note this constant can be defined in each file including this file
*/
#ifndef MAX_LOG_LEVEL
/* By default only errors are displayed */
# define MAX_LOG_LEVEL ERROR_LOG_LEVEL
#endif
#ifndef LOG_MODULE_NAME
/* Name of the module */
# error "No module name set for logger"
#endif
/**
* Helpers macros to print logs
*/
#if MAX_LOG_LEVEL >= DEBUG_LOG_LEVEL
static inline void LOGD(char * format, ...)
{
va_list arg;
va_start(arg, format);
Platform_LOG('D', LOG_MODULE_NAME, format, arg);
va_end(arg);
}
#else
# define LOGD(__log__, ...)
#endif
#if MAX_LOG_LEVEL >= INFO_LOG_LEVEL
static inline void LOGI(char * format, ...)
{
va_list arg;
va_start(arg, format);
Platform_LOG('I', LOG_MODULE_NAME, format, arg);
va_end(arg);
}
#else
# define LOGI(__log__, ...)
#endif
#if MAX_LOG_LEVEL >= WARNING_LOG_LEVEL
static inline void LOGW(char * format, ...)
{
va_list arg;
va_start(arg, format);
Platform_LOG('W', LOG_MODULE_NAME, format, arg);
va_end(arg);
}
#else
# define LOGW(__log__, ...)
#endif
#if MAX_LOG_LEVEL >= ERROR_LOG_LEVEL
static inline void LOGE(char * format, ...)
{
va_list arg;
va_start(arg, format);
Platform_LOG('E', LOG_MODULE_NAME, format, arg);
va_end(arg);
}
#else
# define LOGE(__log__, ...)
#endif
#ifdef PRINT_BUFFERS
static inline void LOG_PRINT_BUFFER(uint8_t * buffer, int size)
{
Platform_print_buffer(buffer, size);
}
#else
# define LOG_PRINT_BUFFER(buffer, size)
#endif