1 /* Siconos is a program dedicated to modeling, simulation and control
2  * of non smooth dynamical systems.
3  *
4  * Copyright 2021 INRIA.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17 */
18 
19 /*! \file debug.h
20   \brief Some debug facilities
21 */
22 
23 #ifndef DEBUG_H
24 #define DEBUG_H
25 
26 #if __cplusplus < 201103L
27 #ifndef DECIMAL_DIG
28 #define DECIMAL_DIG __DECIMAL_DIG__
29 #endif
30 #endif
31 
32 #define ANSI_COLOR_RED     "\x1b[31m"
33 #define ANSI_COLOR_GREEN   "\x1b[32m"
34 #define ANSI_COLOR_YELLOW  "\x1b[33m"
35 #define ANSI_COLOR_BLUE    "\x1b[34m"
36 #define ANSI_COLOR_MAGENTA "\x1b[35m"
37 #define ANSI_COLOR_CYAN    "\x1b[36m"
38 #define ANSI_COLOR_RESET   "\x1b[0m"
39 
40 #ifdef DEBUG_MESSAGES
41 
42 #ifdef DEBUG_WHERE_MESSAGES
43 #define DEBUG_WHERESTR  "%s:%d:\n"
44 #define DEBUG_WHEREARG  __FILE__, __LINE__
45 #else
46 #define DEBUG_WHERESTR  "%s"
47 #define DEBUG_WHEREARG  ""
48 #endif
49 
50 extern unsigned int debug_counter;
51 
52 
53 #ifdef DEBUG_STDOUT
54 
55 #ifdef DEBUG_NOCOLOR
56 #define DEBUG_INTERNAL_PRINTF(...)   printf(__VA_ARGS__);
57 #else
58 #define DEBUG_INTERNAL_PRINTF(...)   printf(ANSI_COLOR_RED); printf(__VA_ARGS__); printf(ANSI_COLOR_RESET);
59 #endif
60 
61 #else
62 
63 #ifdef DEBUG_NOCOLOR
64 #define DEBUG_INTERNAL_PRINTF(...)    fprintf(stderr, __VA_ARGS__);
65 #else
66 #define DEBUG_INTERNAL_PRINTF(...)    fprintf(stderr, ANSI_COLOR_RED); fprintf(stderr, __VA_ARGS__); fprintf(stderr, ANSI_COLOR_RESET);
67 #endif
68 
69 #endif //DEBUG_STDOUT
70 
71 
72 //#define DEBUG_PREFIX   printf("%i",debug_counter); for (unsigned int i =0 ; i < debug_counter; i++) printf(".");
73 #define DEBUG_PREFIX                                                   \
74   if (debug_counter > 0)                                               \
75     for (unsigned int i = 0 ; i < debug_counter-1; i++) printf("|  ");
76 
77 #define DEBUG_BEGIN(M)  DEBUG_PREFIX; if (debug_counter  < 24 ) debug_counter++;   DEBUG_INTERNAL_PRINTF("-= BEGIN ==== %s",M)
78 #define DEBUG_END(M)    if (debug_counter >0)  debug_counter-- ; DEBUG_PREFIX;   DEBUG_INTERNAL_PRINTF("-= END   ==== %s",M)
79 
80 #ifdef DEBUG_BEGIN_END_ONLY
81 #define DEBUG_PRINTF(_fmt, ...)
82 #define DEBUG_PRINT(M)
83 #define DEBUG_EXPR(E)
84 #define DEBUG_EXPR_WE(E)
85 #define DEBUG_GLOBAL_VAR_DECL(D)
86 #else
87 #define DEBUG_PRINTF(_fmt, ...)  DEBUG_PREFIX; DEBUG_INTERNAL_PRINTF(DEBUG_WHERESTR _fmt, DEBUG_WHEREARG, __VA_ARGS__)
88 #define DEBUG_PRINT(M)  DEBUG_PRINTF("%s",M)
89 #define DEBUG_EXPR(E) DEBUG_PRINTF("%s: ", #E) do { E ; } while(0)
90 #define DEBUG_EXPR_WE(E) do { E ; } while(0)
91 #define DEBUG_GLOBAL_VAR_DECL(D) D
92 #endif
93 
94 #else
95 
96 #define DEBUG_BEGIN(M)
97 #define DEBUG_END(M)
98 #define DEBUG_PRINTF(_fmt, ...)
99 #define DEBUG_PRINT(M)
100 #define DEBUG_EXPR(E)
101 #define DEBUG_EXPR_WE(E)
102 #define DEBUG_GLOBAL_VAR_DECL(D)
103 
104 #endif
105 
106 #define DEBUG_PRINT_MAT_STR(NAME, M, nrows, ncols) \
107 DEBUG_PRINT(#NAME " matrix\n"); \
108 DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) \
109   { for(unsigned j = 0 ; j < ncols; ++j) \
110   { DEBUG_PRINTF(ANSI_COLOR_BLUE " % 2.2e " ANSI_COLOR_RESET, M[i + j*nrows]) } \
111    DEBUG_PRINT("\n")});
112 
113 #define DEBUG_PRINT_MAT(M, nrows, ncols) DEBUG_PRINT_MAT_STR(#M, M, nrows, ncols)
114 
115 #define DEBUG_PRINT_MAT_SMALL_STR(NAME, M, nrows, ncols, ...) \
116 DEBUG_PRINT(#NAME " matrix\n"); \
117 DEBUG_EXPR_WE(double* _arr_[] = {__VA_ARGS__}; for (unsigned i = 0; i < nrows; ++i) \
118   { for (unsigned k = 0; k < sizeof(_arr_)/sizeof(double*); ++k) {DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, _arr_[k][i])} \
119     for(unsigned j = 0 ; j < ncols; ++j) \
120   { if (fabs(M[i + j*nrows]) > 2.2e-16) {DEBUG_PRINTF(ANSI_COLOR_YELLOW " % 2.f " ANSI_COLOR_RESET, M[i + j*nrows])} \
121     else { DEBUG_PRINT(ANSI_COLOR_BLUE " . " ANSI_COLOR_RESET) } } \
122    DEBUG_PRINT("\n")});
123 
124 #define DEBUG_PRINT_SMALL_MAT(M, nrows, ncols) DEBUG_PRINT_MAT_SMALL_STR(#M, M, nrows, ncols)
125 
126 #define DEBUG_PRINT_MAT_ROW_MAJOR_STR(NAME, M, nrows, ncols) \
127 DEBUG_PRINT(#NAME " matrix\n"); \
128 DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) \
129   { for(unsigned j = 0 ; j < ncols; ++j) \
130   { DEBUG_PRINTF(ANSI_COLOR_BLUE "% 2.2e " ANSI_COLOR_RESET, M[i*ncols + j]) } \
131    DEBUG_PRINT("\n")});
132 
133 #define DEBUG_PRINT_MAT_ROW_MAJOR(M, nrows, ncols) DEBUG_PRINT_MAT_ROW_MAJOR_STR(#M, M, nrows, ncols)
134 
135 #define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_STR(NAME, M, nrows, ncols, ncols_to_display) \
136 DEBUG_PRINT(#NAME " matrix\n"); \
137 DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) \
138   { for(unsigned j = 0 ; j < ncols_to_display; ++j) \
139   { DEBUG_PRINTF(ANSI_COLOR_BLUE "% 2.2e " ANSI_COLOR_RESET, M[i*ncols + j]) } \
140    DEBUG_PRINT("\n")});
141 
142 #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)
143 
144 #define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_SMALL_STR(NAME, M, nrows, ncols, ncols_to_display) \
145 DEBUG_PRINT(#NAME " matrix\n"); \
146 DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) \
147   { for(unsigned j = 0 ; j < ncols_to_display; ++j) \
148   { if (fabs(M[i*ncols + j]) > 2.2e-16) {DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, M[i*ncols + j])} \
149     else { DEBUG_PRINT(ANSI_COLOR_BLUE " 0     " ANSI_COLOR_RESET) } } \
150    DEBUG_PRINT("\n")});
151 
152 #define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_SMALL2_STR(NAME, M, nrows, ncols, ncols_to_display, ...) \
153 DEBUG_PRINT(#NAME " matrix\n"); \
154 DEBUG_EXPR_WE( double* _arr_[] = {__VA_ARGS__}; for (unsigned i = 0; i < nrows; ++i) \
155   { for (unsigned k = 0; k < sizeof(_arr_)/sizeof(double*); ++k) {DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, _arr_[k][i])} \
156     for(unsigned j = 0 ; j < ncols_to_display; ++j) \
157   { if (fabs(M[i*ncols + j]) > 2.2e-16) {DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, M[i*ncols + j])} \
158     else { DEBUG_PRINT(ANSI_COLOR_BLUE " 0 " ANSI_COLOR_RESET) } } \
159    DEBUG_PRINT("\n")});
160 
161 #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)
162 
163 #define DEBUG_PRINT_VEC_STR(MSG, V, size) \
164 DEBUG_PRINT(MSG " vector\n"); \
165 DEBUG_EXPR_WE(for (unsigned i = 0; i < size; ++i) \
166   { DEBUG_PRINTF(ANSI_COLOR_GREEN "% 2.2e " ANSI_COLOR_RESET, V[i]) }\
167    DEBUG_PRINT("\n"));
168 
169 #define DEBUG_PRINT_VEC(V, size) DEBUG_PRINT_VEC_STR(#V, V, size)
170 
171 #define DEBUG_PRINT_VEC_INT_STR(MSG, V, size) \
172 DEBUG_PRINT(MSG " vector\n"); \
173 DEBUG_EXPR_WE(for (unsigned i = 0; i < size; ++i) \
174   { DEBUG_PRINTF(ANSI_COLOR_CYAN "%d " ANSI_COLOR_RESET, V[i]) }\
175    DEBUG_PRINT("\n"));
176 
177 #define DEBUG_PRINT_VEC_INT(V, size) DEBUG_PRINT_VEC_INT_STR(#V, V, size)
178 
179 
180 #define DEBUG_OR_VERBOSE(X) if (verbose > 0) { X; } else { DEBUG_EXPR_WE(X); }
181 
182 #endif
183