-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.h
142 lines (98 loc) · 5.3 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
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef DEBUG_H
#define DEBUG_H
/**********************************************************************************
** ENVIROMENT VARIABILE
**********************************************************************************/
//#define ENABLE_DEBUG
/**********************************************************************************
** GLOBAL INCLUDE
**********************************************************************************/
/**********************************************************************************
** DEFINE
**********************************************************************************/
#define _DEBUG_MAX_INDENT_LEVEL 12
/**********************************************************************************
** MACRO
**********************************************************************************/
#ifdef ENABLE_DEBUG
///----------------------------------------------------------------
/// GLOBAL VARIABLES
///----------------------------------------------------------------
//Variable prototypes for debug
#define DEBUG_VARS_PROTOTYPES() \
extern FILE *_debug_file; \
extern int _debug_indent_level, _debug_show_level
//Global variables for debug
#define DEBUG_VARS() \
FILE *_debug_file = NULL; \
int _debug_indent_level = 0, _debug_show_level = 0
//Change the show level
#define DSHOW( level ) \
_debug_show_level = (level)
///----------------------------------------------------------------
/// DEBUG FILE MACROS
///----------------------------------------------------------------
//Open file and start debugging
#define DSTART( user__debug_show_level ) \
_debug_file = fopen( "debug.log", "w+"), _debug_indent_level = 0, _debug_show_level = user__debug_show_level , fprintf(_debug_file, "Start Debug!\n")
//Stop debugging and close file
#define DSTOP() \
((_debug_file != NULL)?fprintf(_debug_file, "\nDebug has Ended!\n"), fflush(_debug_file), fclose(_debug_file), _debug_file = NULL:(0))
///----------------------------------------------------------------
/// DEBUG PRINT MACROS
///----------------------------------------------------------------
//Print a given number of tab characters
#define DTAB(n) \
(((_debug_file != NULL) && (_debug_indent_level >= _debug_show_level))?(fprintf(_debug_file,"%.*s", n, "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")):(0));
//Print a number of tab equal to indent level than print user defined string
#define DPRINT( ... ) \
(((_debug_file != NULL) && (_debug_indent_level >= _debug_show_level))?(fprintf(_debug_file,"%.*s", _debug_indent_level, "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), fprintf(_debug_file, __VA_ARGS__ )):(0))
//print user defined string
#define DPRINT_NOTAB( ... ) \
(((_debug_file != NULL) && (_debug_indent_level >= _debug_show_level))?(fprintf(_debug_file, __VA_ARGS__ )):(0))
///----------------------------------------------------------------
/// FUNCTION TRACE MACROS
///----------------------------------------------------------------
//Enter Function and increase indent level. No argument print version
#define DENTER() \
DPRINT( "-->> \"%s\" |\n", __FUNCTION__), (_debug_indent_level<_DEBUG_MAX_INDENT_LEVEL)?(++_debug_indent_level):(_DEBUG_MAX_INDENT_LEVEL)
//Return from function and decrease indent level. No argument version
#define DRETURN() \
(_debug_indent_level>0)?(--_debug_indent_level):(0), DPRINT( "<<-- \"%s\" |\n", __FUNCTION__)
//Enter Function and increase indent level. No argument print version
#define DENTER_ARG( ... ) \
DPRINT( "-->> \"%s\" | ", __FUNCTION__), DPRINT_NOTAB( __VA_ARGS__ ), (_debug_indent_level<_DEBUG_MAX_INDENT_LEVEL)?(++_debug_indent_level):(_DEBUG_MAX_INDENT_LEVEL)
//Return from function and decrease indent level. No argument version
#define DRETURN_ARG( ... ) \
(_debug_indent_level>0)?(--_debug_indent_level):(0), DPRINT( "<<-- \"%s\" | ", __FUNCTION__), DPRINT_NOTAB( __VA_ARGS__ )
#else
#define DEBUG_VARS_PROTOTYPES()
#define DEBUG_VARS()
#define DSHOW( ... )
#define DSTART( ... )
#define DSTOP()
#define DTAB( ... )
#define DPRINT( ... )
#define DPRINT_NOTAB( ... )
#define DENTER( ... )
#define DRETURN( ... )
#define DENTER_ARG( ... )
#define DRETURN_ARG( ... )
#endif
/**********************************************************************************
** TYPEDEF
**********************************************************************************/
/**********************************************************************************
** PROTOTYPE: STRUCTURE
**********************************************************************************/
/**********************************************************************************
** PROTOTYPE: GLOBAL VARIABILE
**********************************************************************************/
//Global variables prototype
DEBUG_VARS_PROTOTYPES();
/**********************************************************************************
** PROTOTYPE: FUNCTION
**********************************************************************************/
#else
#warning "multiple inclusion of the header file"
#endif