-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.h
executable file
·130 lines (110 loc) · 4.25 KB
/
debug.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
/**
* @file debug.h
*
* @date 29.06.2019
* @author twatorowski ([email protected])
*
* @brief Debugging macros
*/
#ifndef DEBUG_H
#define DEBUG_H
#include <stdint.h>
#include <stddef.h>
#include "config.h"
#include "compiler.h"
#include "dev/usart.h"
#include "dev/usart_dev.h"
#include "util/concatstr.h"
#include "util/stdio.h"
#include "sys/yield.h"
#include "sys/time.h"
/* disables debug globally */
#if !(DEVELOPMENT)
#undef DEBUG
#endif
/** levels of debug */
#define DLVL_DEBUG 1
#define DLVL_INFO 10
#define DLVL_WARN 20
#define DLVL_ERROR 30
/* get debug level name */
/**
* @brief returns the string name of given debug level
*
* @param lvl level value (from enum)
* @return const char* pointer to the string name
*/
const char * Debug_GetLevelName(int lvl);
/**
* @brief set global debug level
*
* @param lvl level to be set
* @return err_t error code
*/
err_t Debug_SetGlobalLevel(int lvl);
/**
* @brief send message over debug interfaces
*
* @param ptr pointer to the message
* @param size message length
*
* @return err_t error code
*/
err_t Debug_Send(const void *ptr, size_t size);
/* debug enabled? */
#ifdef DEBUG
/* debug buffer */
extern char debug_buf[];
/** #brief length of the data in debug buffer */
extern size_t debug_buf_len;
/* global level of debug */
extern int debug_global_lvl;
/* this is a fix for debug being defined with no value */
#if ~(~DEBUG + 0) == 0 && ~(~DEBUG + 1) == 1
/* we want to override the value of the debug */
#undef DEBUG
/* here we asume that no value means user wants everything */
#define DEBUG debug_global_lvl
#endif
/* debug message prefix */
#define DBG_MSG_PRFX \
"+D: [" __FILE__ ":" CONCATSTR(__LINE__)":%d:%s]"
/**
* @brief debug printf-like function
*/
#define dprintf(__lvl, fmt, ...) \
/* encapsulated in a loop, to make it compiler-proof :) */ \
do { \
if ((__lvl) < (DEBUG)) \
break; \
/* debug buffer is occupied? */ \
while (debug_buf_len) \
Yield(); \
/* produce string */ \
debug_buf_len = snprintf(debug_buf, DEBUG_MAX_LINE_LEN, \
DBG_MSG_PRFX fmt, time(0), Debug_GetLevelName(__lvl), ## __VA_ARGS__); \
/* try to send debug over the tp */ \
Debug_Send(debug_buf, debug_buf_len); \
/* release the buffer */ \
debug_buf_len = 0; \
} while (0)
// /** helper macros */
// #define dprintf_d(fmt, ...) dprintf(DLVL_DEBUG, fmt, __VA_ARGS__)
// #define dprintf_i(fmt, ...) dprintf(DLVL_INFO, fmt, __VA_ARGS__)
// #define dprintf_w(fmt, ...) dprintf(DLVL_WARN, fmt, __VA_ARGS__)
// #define dprintf_e(fmt, ...) dprintf(DLVL_ERROR, fmt, __VA_ARGS__)
#else
/**
* @brief debug printf-like function
*/
#define dprintf(__lvl, fmt, ...) \
/* encapsulated in a loop, to make it compiler-proof :) */ \
do { \
} while (0)
#endif
/** helper macros */
#define dprintf_d(fmt, ...) dprintf(DLVL_DEBUG, fmt, __VA_ARGS__)
#define dprintf_i(fmt, ...) dprintf(DLVL_INFO, fmt, __VA_ARGS__)
#define dprintf_w(fmt, ...) dprintf(DLVL_WARN, fmt, __VA_ARGS__)
#define dprintf_e(fmt, ...) dprintf(DLVL_ERROR, fmt, __VA_ARGS__)
#endif /* DEBUG_H */