NAMD
Debug.h
Go to the documentation of this file.
1 
7 #ifndef DEBUG_H
8 #define DEBUG_H
9 
10 // assert enabled by compiling with USE_ASSERT
11 #undef ASSERT
12 #ifdef USE_ASSERT
13 #define ASSERT(expr) \
14  do { \
15  if ( !(expr) ) { \
16  char msg[256]; \
17  snprintf(msg, sizeof(msg), "ASSERT: \"%s\" " \
18  "(%s, %d)\n", #expr, __FILE__, __LINE__); \
19  NAMD_die(msg); \
20  } \
21  } while (0) // requires terminating semi-colon
22 #else
23 #define ASSERT(expr)
24 #endif // USE_ASSERT
25 
26 #ifndef MIN_DEBUG_LEVEL
27  #define MIN_DEBUG_LEVEL 0
28 #endif
29 #ifndef MAX_DEBUG_LEVEL
30  #define MAX_DEBUG_LEVEL 10
31 #endif
32 #ifndef STDERR_LEVEL
33  /* anything >= this error level goes to stderr */
34  #define STDERR_LEVEL 5
35 #endif
36 
37 
38 /*****************************************************************
39  * DebugM(): function to display a debug message.
40  * Messages have different levels. The low numbers are low severity
41  * while the high numbers are really important. Very high numbers
42  * are sent to stderr rather than stdout.
43  * The default severity scale is from 0 to 10.
44  * 0 = plain message
45  * 4 = important message
46  * 5 = warning (stderr)
47  * 10 = CRASH BANG BOOM error (stderr)
48  * The remaining args are like printf: a format string and some args.
49  * This function can be turned off by compiling without the DEBUGM flag
50  * No parameters to this function should have a side effect!
51  * No functions should be passed as parameters! (including inline)
52  *****************************************************************/
53 
54  #ifdef DEBUGM
55 
56 #include "InfoStream.h"
57 
58  #define Debug(x) (x)
59  #define DebugM(level,format) \
60  { \
61  if ((level >= MIN_DEBUG_LEVEL) && (level <= MAX_DEBUG_LEVEL)) \
62  { \
63  infostream Dout; \
64  if (level >= STDERR_LEVEL) Dout << "[ERROR " << level << "] "; \
65  else if (level > 0) Dout << "[Debug " << level << "] "; \
66  Dout << iPE << ' ' << iFILE; \
67  Dout << format << endi; \
68  } \
69  }
70 
71  #else
72  /* make a void function. */
73  /* parameters with side effects will be removed! */
74  #define Debug(x) ;
75  #define DebugM(x,y) ;
76 
77  #endif /* DEBUGM */
78 
79 #ifdef PROCTRACE_DEBUG
80 #include "charm++.h"
81 #include <stdarg.h>
82 #include "ProcessorPrivate.h"
83 class DebugFileTrace{
84 private:
85  char *fname;
86  FILE *fp;
87 public:
88  inline static DebugFileTrace *Instance(char *fn){
89  if(CkpvAccess(DebugFileTrace_instance)==0){
90  CkpvAccess(DebugFileTrace_instance) = new DebugFileTrace(fn);
91  }
92  return CkpvAccess(DebugFileTrace_instance);
93  }
94  inline static DebugFileTrace *Object(){
95  return CkpvAccess(DebugFileTrace_instance);
96  }
97  DebugFileTrace(char *fn){
98  if(fn==NULL) {
99  fname = NULL;
100  fp = stdout;
101  return;
102  }else{
103  char tmp[128];
104  memset(tmp, 0, 128*sizeof(char));
105  sprintf(tmp, "%s.%d", fn, CkMyPe());
106  fname = new char[strlen(tmp)+1];
107  memcpy(fname, tmp, strlen(tmp)+1);
108  fp = fopen(fname, "w");
109  fclose(fp);
110  }
111  }
112  ~DebugFileTrace(){
113  delete [] fname;
114  }
115  inline void writeTrace(const char *msg, ...){
116  va_list argList;
117  va_start(argList, msg);
118  vfprintf(fp, msg, argList);
119  va_end(argList);
120  }
121  inline int openTrace(){
122  if(fname==NULL) return 0;
123  fp = fopen(fname, "a");
124  if(fp==NULL)
125  return 1;
126  else
127  return 0;
128  }
129  inline int closeTrace(){
130  if(fname==NULL) return 0;
131  return fclose(fp);
132  }
133  inline int flushTrace(){
134  return fflush(fp);
135  }
136 };
137 #endif
138 
139 
140 #undef D_MSG
141 #undef D_STR
142 #undef D_INT
143 #undef D_REAL
144 #undef D_REXP
145 #undef D_VEC
146 
147 #ifdef NL_DEBUG
148 
149 #define D_MSG(t) \
150  fprintf(stderr, "DEBUG: %s:%d: %s\n", __FILE__, __LINE__, t)
151 #define D_STR(t) \
152  fprintf(stderr, "DEBUG: %s:%d: %s=\"%s\"\n", __FILE__, __LINE__, #t, t)
153 #define D_INT(n) \
154  fprintf(stderr, "DEBUG: %s:%d: %s=%d\n", __FILE__, __LINE__, #n, n)
155 #define D_REAL(r) \
156  fprintf(stderr, "DEBUG: %s:%d: %s=%g\n", __FILE__, __LINE__, #r, double(r))
157 #define D_REXP(r) \
158  fprintf(stderr, "DEBUG: %s:%d: %s=%e\n", __FILE__, __LINE__, #r, double(r))
159 #define D_VEC(v) \
160  fprintf(stderr, "DEBUG: %s:%d: %s=%g %g %g\n", __FILE__, __LINE__, \
161  #v, double(v.x), double(v.y), double(v.z))
162 
163 #else
164 
165 #define D_MSG(t)
166 #define D_STR(t)
167 #define D_INT(t)
168 #define D_REAL(t)
169 #define D_REXP(t)
170 #define D_VEC(t)
171 
172 #endif // NL_DEBUG
173 
174 
175 #endif /* DEBUG_H */
176