Zero  0.1.0
w_debug.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-MT -- Multi-threaded port of the SHORE storage manager
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
24 /*<std-header orig-src='shore' incl-file-exclusion='W_DEBUG_H'>
25 
26  $Id: w_debug.h,v 1.1 2010/12/09 15:29:05 nhall Exp $
27 
28 SHORE -- Scalable Heterogeneous Object REpository
29 
30 Copyright (c) 1994-99 Computer Sciences Department, University of
31  Wisconsin -- Madison
32 All Rights Reserved.
33 
34 Permission to use, copy, modify and distribute this software and its
35 documentation is hereby granted, provided that both the copyright
36 notice and this permission notice appear in all copies of the
37 software, derivative works or modified versions, and any portions
38 thereof, and that both notices appear in supporting documentation.
39 
40 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
41 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
42 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
43 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
44 
45 This software was developed with support by the Advanced Research
46 Project Agency, ARPA order number 018 (formerly 8230), monitored by
47 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
48 Further funding for this work was provided by DARPA through
49 Rome Research Laboratory Contract No. F30602-97-2-0247.
50 
51 */
52 
53 #ifndef __W_DEBUG_H
54 #define __W_DEBUG_H
55 
56 #include "w_defines.h"
57 
58 /* -- do not edit anything above this line -- </std-header>*/
59 
60 #ifndef __W_BASE_H
61 /* NB: DO NOT make this include w.h -- not yet */
62 #include "w_base.h"
63 #endif // __W_BASE_H
64 
80 #include <cassert>
81 #include <pthread.h>
82 #include <sstream>
83 
84 #undef USE_REGEX
85 
86 #ifdef USE_REGEX
87 #include "regex_posix.h"
88 #endif /* USE_REGEX */
89 
90 /* XXX missing type in vc++, hack around it here too, don't pollute
91  global namespace too badly. */
93 
94 #ifdef W_TRACE
95 
96 // Turns full path from __FILE__ macro into just name of the file
97 // CS: This was not necessary in Shore-MT because gcc was invoked
98 // not on the full path, but on the file directly
99 #define _strip_filename(f) \
100  (strrchr(f, '/') ? strrchr(f, '/') + 1 : f)
101 
102 #endif /* W_TRACE*/
103 
104 /* ************************************************************************ */
105 
106 /* ************************************************************************
107  *
108  * Class w_debug, macros DBG, DBG_NONL, DBG1, DBG1_NONL:
109  */
110 
111 
122 class w_debug {
123 private:
124  char* _flags;
125 
126  enum {
127  _all = 0x1,
128  _none = 0x2
129  };
130 
131  unsigned int mask;
132 
134 
135 #ifdef USE_REGEX
136  static regex_t re_posix_re;
137  static bool re_ready;
138  static char* re_error_str;
139  static char* re_comp_debug(const char* pattern);
140  static int re_exec_debug(const char* string);
141 #endif /* USE_REGEX */
142 
143  int all(void) {
144  return (mask & _all) ? 1 : 0;
145  }
146 
147  int none(void) {
148  return (mask & _none) ? 1 : 0;
149  }
150 
151 public:
152  w_debug(const char* n, const char* f);
153 
154  ~w_debug();
155 
156  int flag_on(const char* fn, const char* file);
157 
158  const char* flags() {
159  return _flags;
160  }
161 
162  void setflags(const char* newflags);
163 
164  void memdump(void* p, int len); // hex dump of memory
165  int trace_level() {
166  return _trace_level;
167  }
168 };
169 
170 extern w_debug _w_debug;
171 
172 
173 // I wanted to use google-logging (glog), but changing all of the existing code
174 // takes time. So, currently it's just std::cout.
175 #define ERROUT(a) std::cerr << "[" << hex << pthread_self() << dec << "] " << __FILE__ << " (" << __LINE__ << ") " a << endl;
176 //#define DBGOUT(a) std::cout << "[" << pthread_self() << "] " << __FILE__ << " (" << __LINE__ << ") " a << endl;
177 
178 // CS: reverted back to shore's old debug mechanism, which allows us
179 // to select only output from certain source files. The current mechanism
180 // dumps way to much debug information, which makes it hard to perform
181 // actual debugging focused only on certain components.
182 #define DBGPRINT(a, file, line) \
183  std::stringstream ss; \
184  ss << "[" << hex << pthread_self() << dec << "] " \
185  << _strip_filename(file) << " (" << line << ") " a; \
186  std::cerr << ss.str() << endl;
187 
188 #define DBGOUT(a) do { \
189  if(_w_debug.flag_on(__func__,_strip_filename(__FILE__))) { \
190  DBGPRINT(a, __FILE__, __LINE__); \
191  } \
192  } while (0);
193 
194 #define DBGOUT0(a) DBGOUT(a)
195 
196 #if 1
197 #if W_DEBUG_LEVEL >= 1
198 #define DBGOUT1(a) DBGOUT(a)
199 #else
200 #define DBGOUT1(a)
201 #endif
202 
203 #if W_DEBUG_LEVEL >= 2
204 #define DBGOUT2(a) DBGOUT(a)
205 #else
206 #define DBGOUT2(a)
207 #endif
208 
209 #if W_DEBUG_LEVEL >= 3
210 #define DBGOUT3(a) DBGOUT(a)
211 #else
212 #define DBGOUT3(a)
213 #endif
214 
215 #if W_DEBUG_LEVEL >= 4
216 #define DBGOUT4(a) DBGOUT(a)
217 #else
218 #define DBGOUT4(a)
219 #endif
220 
221 #if W_DEBUG_LEVEL >= 5
222 #define DBGOUT5(a) DBGOUT(a)
223 #else
224 #define DBGOUT5(a)
225 #endif
226 
227 #if W_DEBUG_LEVEL >= 6
228 #define DBGOUT6(a) DBGOUT(a)
229 #else
230 #define DBGOUT6(a)
231 #endif
232 
233 #if W_DEBUG_LEVEL >= 7
234 #define DBGOUT7(a) DBGOUT(a)
235 #else
236 #define DBGOUT7(a)
237 #endif
238 
239 #if W_DEBUG_LEVEL >= 8
240 #define DBGOUT8(a) DBGOUT(a)
241 #else
242 #define DBGOUT8(a)
243 #endif
244 
245 #if W_DEBUG_LEVEL >= 9
246 #define DBGOUT9(a) DBGOUT(a)
247 #else
248 #define DBGOUT9(a)
249 #endif
250 
251 #define DBG1(a) DBGOUT1(a)
252 #define DBG2(a) DBGOUT2(a)
253 #define DBG3(a) DBGOUT3(a)
254 #define DBG5(a) DBGOUT5(a)
255 
256 #else
257 
258 #define DBGOUT1(a)
259 #define DBGOUT2(a)
260 #define DBGOUT3(a)
261 #define DBGOUT4(a)
262 #define DBGOUT5(a)
263 #define DBGOUT6(a)
264 #define DBGOUT7(a)
265 #define DBGOUT8(a)
266 #define DBGOUT9(a)
267 
268 #endif
269 
270 // the old "DBG" idiom is level=3
271 #define DBG(a) DBGOUT3(a)
272 /*
273 #if defined(W_TRACE)
274 
275 # define DBG2(a,file,line) \
276  w_dbg_fmtflags old = _w_debug.clog.setf(ios::dec, ios::basefield); \
277  _w_debug.clog << _strip_filename(file) << ":" << line << ":" ; \
278  _w_debug.clog.setf(old, ios::basefield); \
279  _w_debug.clog a << endl;
280 
281 # define DBG1(a) do {\
282  if(_w_debug.flag_on(__func__,__FILE__)) { \
283  DBG2(a,__FILE__,__LINE__) \
284  } } while(0)
285 
286 # define DBG(a) DBG1(a)
287 
288 #else
289 # define DBG(a)
290 #endif *//* defined(W_TRACE) */
291 /* ************************************************************************ */
292 
293 // #define DBG2(a,f,l) DBGPRINT(a,f,l) // used by smthread.h
294 
295 #include <thread>
296 
297 #define DBGTHRD(arg) DBG(<<" th."<< std::this_thread::get_id() << " " arg)
298 
299 /*<std-footer incl-file-exclusion='W_DEBUG_H'> -- do not edit anything below this line -- */
300 
301 #endif // __W_DEBUG_H /*</std-footer>*/
~w_debug()
Definition: w_debug.cpp:111
const char * flags()
Definition: w_debug.h:158
ios::fmtflags w_dbg_fmtflags
Definition: w_debug.h:92
w_debug(const char *n, const char *f)
Definition: w_debug.cpp:72
int all(void)
Definition: w_debug.h:143
int _trace_level
Definition: w_debug.h:133
void setflags(const char *newflags)
Definition: w_debug.cpp:119
ios::fmtflags fmtflags
Definition: w_base.cpp:141
char * _flags
Definition: w_debug.h:124
int none(void)
Definition: w_debug.h:147
Definition: w_debug.h:127
Definition: w_debug.h:128
void memdump(void *p, int len)
w_debug _w_debug
int flag_on(const char *fn, const char *file)
Definition: w_debug.cpp:181
An ErrLog used for tracing (configure –enable-trace)
Definition: w_debug.h:122
unsigned int mask
Definition: w_debug.h:131
int trace_level()
Definition: w_debug.h:165