Program listing for file numerics/src/tools/siconos_debug.h#
Return to documentation for this file
1#ifndef DEBUG_H
2#define DEBUG_H
3
4#if __cplusplus < 201103L
5#ifndef DECIMAL_DIG
6#define DECIMAL_DIG __DECIMAL_DIG__
7#endif
8#endif
9
10#define ANSI_COLOR_RED "\x1b[31m"
11#define ANSI_COLOR_GREEN "\x1b[32m"
12#define ANSI_COLOR_YELLOW "\x1b[33m"
13#define ANSI_COLOR_BLUE "\x1b[34m"
14#define ANSI_COLOR_MAGENTA "\x1b[35m"
15#define ANSI_COLOR_CYAN "\x1b[36m"
16#define ANSI_COLOR_RESET "\x1b[0m"
17
18#ifdef DEBUG_MESSAGES
19
20#ifdef DEBUG_WHERE_MESSAGES
21#define DEBUG_WHERESTR "%s:%d:"
22#define DEBUG_WHEREARG __FILE__, __LINE__
23#else
24#define DEBUG_WHERESTR "%s"
25#define DEBUG_WHEREARG ""
26#endif
27
28extern unsigned int debug_counter;
29
30
31#ifdef DEBUG_STDOUT
32
33#ifdef DEBUG_NOCOLOR
34#define DEBUG_INTERNAL_PRINTF(...) printf(__VA_ARGS__);
35#else
36#define DEBUG_INTERNAL_PRINTF(...) printf(ANSI_COLOR_RED); printf(__VA_ARGS__); printf(ANSI_COLOR_RESET);
37#endif
38
39#else
40
41#ifdef DEBUG_NOCOLOR
42#define DEBUG_INTERNAL_PRINTF(...) fprintf(stderr, __VA_ARGS__);
43#else
44#define DEBUG_INTERNAL_PRINTF(...) fprintf(stderr, ANSI_COLOR_RED); fprintf(stderr, __VA_ARGS__); fprintf(stderr, ANSI_COLOR_RESET);
45#endif
46
47#endif
48
49
50
51#define DEBUG_PREFIX \
52 ((debug_counter > 0) ? \
53 ({ for (unsigned int i = 0 ; i < debug_counter-1; i++) printf("| ");}) : ({(void)0;}))
54
55#define DEBUG_BEGIN(M) DEBUG_PREFIX; if (debug_counter < 24 ) debug_counter++; DEBUG_INTERNAL_PRINTF("-= BEGIN ==== %s",M)
56#define DEBUG_END(M) if (debug_counter >0) debug_counter-- ; DEBUG_PREFIX; DEBUG_INTERNAL_PRINTF("-= END ==== %s",M)
57
58#ifdef DEBUG_BEGIN_END_ONLY
59#define DEBUG_PRINTF(_fmt, ...)
60#define DEBUG_PRINT(M)
61#define DEBUG_EXPR(E)
62#define DEBUG_EXPR_WE(E)
63#define DEBUG_GLOBAL_VAR_DECL(D)
64#else
65#define DEBUG_PRINTF(_fmt, ...) ({DEBUG_PREFIX; DEBUG_INTERNAL_PRINTF(DEBUG_WHERESTR _fmt, DEBUG_WHEREARG, __VA_ARGS__);})
66#define DEBUG_PRINT(M) DEBUG_PRINTF("%s",M)
67#define DEBUG_EXPR(E) DEBUG_PRINTF("%s: ", #E); do { E ; } while(0)
68#define DEBUG_EXPR_WE(E) do { E ; } while(0)
69#define DEBUG_GLOBAL_VAR_DECL(D) D
70#endif
71
72#else
73
74#define DEBUG_BEGIN(M)
75#define DEBUG_END(M)
76#define DEBUG_PRINTF(_fmt, ...)
77#define DEBUG_PRINT(M)
78#define DEBUG_EXPR(E)
79#define DEBUG_EXPR_WE(E)
80#define DEBUG_GLOBAL_VAR_DECL(D)
81
82#endif
83
84#define DEBUG_PRINT_MAT_STR(NAME, M, nrows, ncols) \
85DEBUG_PRINT(#NAME " matrix\n"); \
86DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) \
87 { for(unsigned j = 0 ; j < ncols; ++j) \
88 { DEBUG_PRINTF(ANSI_COLOR_BLUE " % 2.2e " ANSI_COLOR_RESET, M[i + j*nrows]) } \
89 DEBUG_PRINT("\n")});
90
91#define DEBUG_PRINT_MAT(M, nrows, ncols) DEBUG_PRINT_MAT_STR(#M, M, nrows, ncols)
92
93#define DEBUG_PRINT_MAT_SMALL_STR(NAME, M, nrows, ncols, ...) \
94DEBUG_PRINT(#NAME " matrix\n"); \
95DEBUG_EXPR_WE(double* _arr_[] = {__VA_ARGS__}; for (unsigned i = 0; i < nrows; ++i) \
96 { for (unsigned k = 0; k < sizeof(_arr_)/sizeof(double*); ++k) {DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, _arr_[k][i])} \
97 for(unsigned j = 0 ; j < ncols; ++j) \
98 { if (fabs(M[i + j*nrows]) > 2.2e-16) {DEBUG_PRINTF(ANSI_COLOR_YELLOW " % 2.f " ANSI_COLOR_RESET, M[i + j*nrows])} \
99 else { DEBUG_PRINT(ANSI_COLOR_BLUE " . " ANSI_COLOR_RESET) } } \
100 DEBUG_PRINT("\n")});
101
102#define DEBUG_PRINT_SMALL_MAT(M, nrows, ncols) DEBUG_PRINT_MAT_SMALL_STR(#M, M, nrows, ncols)
103
104#define DEBUG_PRINT_MAT_ROW_MAJOR_STR(NAME, M, nrows, ncols) \
105DEBUG_PRINT(#NAME " matrix\n"); \
106DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) \
107 { for(unsigned j = 0 ; j < ncols; ++j) \
108 { DEBUG_PRINTF(ANSI_COLOR_BLUE "% 2.2e " ANSI_COLOR_RESET, M[i*ncols + j]) } \
109 DEBUG_PRINT("\n")});
110
111#define DEBUG_PRINT_MAT_ROW_MAJOR(M, nrows, ncols) DEBUG_PRINT_MAT_ROW_MAJOR_STR(#M, M, nrows, ncols)
112
113#define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_STR(NAME, M, nrows, ncols, ncols_to_display) \
114DEBUG_PRINT(#NAME " matrix\n"); \
115DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) \
116 { for(unsigned j = 0 ; j < ncols_to_display; ++j) \
117 { DEBUG_PRINTF(ANSI_COLOR_BLUE "% 2.2e " ANSI_COLOR_RESET, M[i*ncols + j]) } \
118 DEBUG_PRINT("\n")});
119
120#define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS(M, nrows, ncols, ncols_to_display) DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_STR(#M, M, nrows, ncols, ncols_to_display)
121
122#define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_SMALL_STR(NAME, M, nrows, ncols, ncols_to_display) \
123DEBUG_PRINT(#NAME " matrix\n"); \
124DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) \
125 { for(unsigned j = 0 ; j < ncols_to_display; ++j) \
126 { if (fabs(M[i*ncols + j]) > 2.2e-16) {DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, M[i*ncols + j])} \
127 else { DEBUG_PRINT(ANSI_COLOR_BLUE " 0 " ANSI_COLOR_RESET) } } \
128 DEBUG_PRINT("\n")});
129
130#define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_SMALL2_STR(NAME, M, nrows, ncols, ncols_to_display, ...) \
131DEBUG_PRINT(#NAME " matrix\n"); \
132DEBUG_EXPR_WE( double* _arr_[] = {__VA_ARGS__}; for (unsigned i = 0; i < nrows; ++i) \
133 { for (unsigned k = 0; k < sizeof(_arr_)/sizeof(double*); ++k) {DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, _arr_[k][i])} \
134 for(unsigned j = 0 ; j < ncols_to_display; ++j) \
135 { if (fabs(M[i*ncols + j]) > 2.2e-16) {DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, M[i*ncols + j])} \
136 else { DEBUG_PRINT(ANSI_COLOR_BLUE " 0 " ANSI_COLOR_RESET) } } \
137 DEBUG_PRINT("\n")});
138
139#define DEBUG_PRINT_MAT_ROW_MAJOR_SMALL_NCOLS(M, nrows, ncols, ncols_to_display) DEBUG_PRINT_MAT_ROW_MAJOR_SMALL_NCOLS_STR(#M, M, nrows, ncols, ncols_to_display)
140
141#define DEBUG_PRINT_VEC_STR(MSG, V, size) \
142DEBUG_PRINT(MSG " vector\n"); \
143DEBUG_EXPR_WE(for (unsigned i = 0; i < size; ++i) \
144 { DEBUG_PRINTF(ANSI_COLOR_GREEN "% 2.2e " ANSI_COLOR_RESET, V[i]) }\
145 DEBUG_PRINT("\n"));
146
147#define DEBUG_PRINT_VEC(V, size) DEBUG_PRINT_VEC_STR(#V, V, size)
148
149#define DEBUG_PRINT_VEC_INT_STR(MSG, V, size) \
150DEBUG_PRINT(MSG " vector\n"); \
151DEBUG_EXPR_WE(for (unsigned i = 0; i < size; ++i) \
152 { DEBUG_PRINTF(ANSI_COLOR_CYAN "%d " ANSI_COLOR_RESET, V[i]) }\
153 DEBUG_PRINT("\n"));
154
155#define DEBUG_PRINT_VEC_INT(V, size) DEBUG_PRINT_VEC_INT_STR(#V, V, size)
156
157
158#define DEBUG_OR_VERBOSE(X) if (verbose > 0) { X; } else { DEBUG_EXPR_WE(X); }
159
160#endif