Program listing for file numerics/src/tools/siconos_debug.h

Program listing for file numerics/src/tools/siconos_debug.h#

  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#ifdef DEBUG_STDOUT
 31
 32#ifdef DEBUG_NOCOLOR
 33#define DEBUG_INTERNAL_PRINTF(...) printf(__VA_ARGS__);
 34#else
 35#define DEBUG_INTERNAL_PRINTF(...) \
 36  printf(ANSI_COLOR_RED);          \
 37  printf(__VA_ARGS__);             \
 38  printf(ANSI_COLOR_RESET);
 39#endif
 40
 41#else
 42
 43#ifdef DEBUG_NOCOLOR
 44#define DEBUG_INTERNAL_PRINTF(...) fprintf(stderr, __VA_ARGS__);
 45#else
 46#define DEBUG_INTERNAL_PRINTF(...) \
 47  fprintf(stderr, ANSI_COLOR_RED); \
 48  fprintf(stderr, __VA_ARGS__);    \
 49  fprintf(stderr, ANSI_COLOR_RESET);
 50#endif
 51
 52#endif
 53
 54
 55
 56#define DEBUG_PREFIX                                                    \
 57  ((debug_counter > 0) ? ({                                             \
 58    for (unsigned int i = 0; i < debug_counter - 1; i++) printf("|  "); \
 59  })                                                                    \
 60                       : ({ (void)0; }))
 61
 62#define DEBUG_BEGIN(M)                     \
 63  DEBUG_PREFIX;                            \
 64  if (debug_counter < 24) debug_counter++; \
 65  DEBUG_INTERNAL_PRINTF("-= BEGIN ==== %s", M)
 66#define DEBUG_END(M)                      \
 67  if (debug_counter > 0) debug_counter--; \
 68  DEBUG_PREFIX;                           \
 69  DEBUG_INTERNAL_PRINTF("-= END   ==== %s", M)
 70
 71#ifdef DEBUG_BEGIN_END_ONLY
 72#define DEBUG_PRINTF(_fmt, ...)
 73#define DEBUG_PRINT(M)
 74#define DEBUG_EXPR(E)
 75#define DEBUG_EXPR_WE(E)
 76#define DEBUG_GLOBAL_VAR_DECL(D)
 77#else
 78#define DEBUG_PRINTF(_fmt, ...)                                              \
 79  ({                                                                         \
 80    DEBUG_PREFIX;                                                            \
 81    DEBUG_INTERNAL_PRINTF(DEBUG_WHERESTR _fmt, DEBUG_WHEREARG, __VA_ARGS__); \
 82  })
 83#define DEBUG_PRINT(M) DEBUG_PRINTF("%s", M)
 84#define DEBUG_EXPR(E)       \
 85  DEBUG_PRINTF("%s: ", #E); \
 86  do {                      \
 87    E;                      \
 88  } while (0)
 89#define DEBUG_EXPR_WE(E) \
 90  do {                   \
 91    E;                   \
 92  } while (0)
 93#define DEBUG_GLOBAL_VAR_DECL(D) D
 94#endif
 95
 96#else
 97
 98#define DEBUG_BEGIN(M)
 99#define DEBUG_END(M)
100#define DEBUG_PRINTF(_fmt, ...)
101#define DEBUG_PRINT(M)
102#define DEBUG_EXPR(E)
103#define DEBUG_EXPR_WE(E)
104#define DEBUG_GLOBAL_VAR_DECL(D)
105
106#endif
107
108#define DEBUG_PRINT_MAT_STR(NAME, M, nrows, ncols)                                \
109  DEBUG_PRINT(#NAME " matrix\n");                                                 \
110  DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) {                            \
111    for (unsigned j = 0; j < ncols; ++j) {                                        \
112      DEBUG_PRINTF(ANSI_COLOR_BLUE " % 2.2e " ANSI_COLOR_RESET, M[i + j * nrows]) \
113    }                                                                             \
114    DEBUG_PRINT("\n")                                                             \
115  });
116
117#define DEBUG_PRINT_MAT(M, nrows, ncols) DEBUG_PRINT_MAT_STR(#M, M, nrows, ncols)
118
119#define DEBUG_PRINT_MAT_SMALL_STR(NAME, M, nrows, ncols, ...)                           \
120  DEBUG_PRINT(#NAME " matrix\n");                                                       \
121  DEBUG_EXPR_WE(double* _arr_[] = {__VA_ARGS__}; for (unsigned i = 0; i < nrows; ++i) { \
122    for (unsigned k = 0; k < sizeof(_arr_) / sizeof(double*); ++k) {                    \
123      DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, _arr_[k][i])           \
124    }                                                                                   \
125    for (unsigned j = 0; j < ncols; ++j) {                                              \
126      if (fabs(M[i + j * nrows]) > 2.2e-16) {                                           \
127        DEBUG_PRINTF(ANSI_COLOR_YELLOW " % 2.f " ANSI_COLOR_RESET, M[i + j * nrows])    \
128      } else {                                                                          \
129        DEBUG_PRINT(ANSI_COLOR_BLUE " . " ANSI_COLOR_RESET)                             \
130      }                                                                                 \
131    }                                                                                   \
132    DEBUG_PRINT("\n")                                                                   \
133  });
134
135#define DEBUG_PRINT_SMALL_MAT(M, nrows, ncols) DEBUG_PRINT_MAT_SMALL_STR(#M, M, nrows, ncols)
136
137#define DEBUG_PRINT_MAT_ROW_MAJOR_STR(NAME, M, nrows, ncols)                     \
138  DEBUG_PRINT(#NAME " matrix\n");                                                \
139  DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) {                           \
140    for (unsigned j = 0; j < ncols; ++j) {                                       \
141      DEBUG_PRINTF(ANSI_COLOR_BLUE "% 2.2e " ANSI_COLOR_RESET, M[i * ncols + j]) \
142    }                                                                            \
143    DEBUG_PRINT("\n")                                                            \
144  });
145
146#define DEBUG_PRINT_MAT_ROW_MAJOR(M, nrows, ncols) \
147  DEBUG_PRINT_MAT_ROW_MAJOR_STR(#M, M, nrows, ncols)
148
149#define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_STR(NAME, M, nrows, ncols, ncols_to_display) \
150  DEBUG_PRINT(#NAME " matrix\n");                                                    \
151  DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) {                               \
152    for (unsigned j = 0; j < ncols_to_display; ++j) {                                \
153      DEBUG_PRINTF(ANSI_COLOR_BLUE "% 2.2e " ANSI_COLOR_RESET, M[i * ncols + j])     \
154    }                                                                                \
155    DEBUG_PRINT("\n")                                                                \
156  });
157
158#define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS(M, nrows, ncols, ncols_to_display) \
159  DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_STR(#M, M, nrows, ncols, ncols_to_display)
160
161#define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_SMALL_STR(NAME, M, nrows, ncols, ncols_to_display) \
162  DEBUG_PRINT(#NAME " matrix\n");                                                          \
163  DEBUG_EXPR_WE(for (unsigned i = 0; i < nrows; ++i) {                                     \
164    for (unsigned j = 0; j < ncols_to_display; ++j) {                                      \
165      if (fabs(M[i * ncols + j]) > 2.2e-16) {                                              \
166        DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, M[i * ncols + j])       \
167      } else {                                                                             \
168        DEBUG_PRINT(ANSI_COLOR_BLUE " 0     " ANSI_COLOR_RESET)                            \
169      }                                                                                    \
170    }                                                                                      \
171    DEBUG_PRINT("\n")                                                                      \
172  });
173
174#define DEBUG_PRINT_MAT_ROW_MAJOR_NCOLS_SMALL2_STR(NAME, M, nrows, ncols, ncols_to_display, \
175                                                   ...)                                     \
176  DEBUG_PRINT(#NAME " matrix\n");                                                           \
177  DEBUG_EXPR_WE(double* _arr_[] = {__VA_ARGS__}; for (unsigned i = 0; i < nrows; ++i) {     \
178    for (unsigned k = 0; k < sizeof(_arr_) / sizeof(double*); ++k) {                        \
179      DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, _arr_[k][i])               \
180    }                                                                                       \
181    for (unsigned j = 0; j < ncols_to_display; ++j) {                                       \
182      if (fabs(M[i * ncols + j]) > 2.2e-16) {                                               \
183        DEBUG_PRINTF(ANSI_COLOR_YELLOW "% 1.0e " ANSI_COLOR_RESET, M[i * ncols + j])        \
184      } else {                                                                              \
185        DEBUG_PRINT(ANSI_COLOR_BLUE " 0 " ANSI_COLOR_RESET)                                 \
186      }                                                                                     \
187    }                                                                                       \
188    DEBUG_PRINT("\n")                                                                       \
189  });
190
191#define DEBUG_PRINT_MAT_ROW_MAJOR_SMALL_NCOLS(M, nrows, ncols, ncols_to_display) \
192  DEBUG_PRINT_MAT_ROW_MAJOR_SMALL_NCOLS_STR(#M, M, nrows, ncols, ncols_to_display)
193
194#define DEBUG_PRINT_VEC_STR(MSG, V, size)            \
195  DEBUG_PRINT(MSG " vector\n");                      \
196  DEBUG_EXPR_WE(for (unsigned i = 0; i < size; ++i){ \
197      DEBUG_PRINTF(ANSI_COLOR_GREEN "% 2.2e " ANSI_COLOR_RESET, V[i])} DEBUG_PRINT("\n"));
198
199#define DEBUG_PRINT_VEC(V, size) DEBUG_PRINT_VEC_STR(#V, V, size)
200
201#define DEBUG_PRINT_VEC_INT_STR(MSG, V, size)        \
202  DEBUG_PRINT(MSG " vector\n");                      \
203  DEBUG_EXPR_WE(for (unsigned i = 0; i < size; ++i){ \
204      DEBUG_PRINTF(ANSI_COLOR_CYAN "%d " ANSI_COLOR_RESET, V[i])} DEBUG_PRINT("\n"));
205
206#define DEBUG_PRINT_VEC_INT(V, size) DEBUG_PRINT_VEC_INT_STR(#V, V, size)
207
208#define DEBUG_OR_VERBOSE(X) \
209  if (verbose > 0) {        \
210    X;                      \
211  } else {                  \
212    DEBUG_EXPR_WE(X);       \
213  }
214
215#endif