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