ViSP  2.6.2
vpDebug.h
1 /****************************************************************************
2  *
3  * $Id: vpDebug.h 3530 2012-01-03 10:52:12Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Debug and trace macro.
36  *
37  * - TRACING: vpTRACE and vpERROR_TRACE work like printf with carreer return at the end of the string.
38  * vpCERROR et vpCTRACE work like the C++ output streams std::cout and std::cerr.
39  * - DEBUGING: vpDEBUG_TRACE(niv) and vpDERROR_TRACE(niv), work like printf, but print only if the
40  * tracing level niv is greater than the debug level VP_DEBUG_MODE.
41  * vpCDEBUG(niv) work like the C++ output stream std::cout.
42  * vpDEBUG_ENABLE(niv) is equal to 1 if the debug level niv is greater than the debug mode
43  * VP_DEBUG_MODE, 0 else.
44  * - PROG DEFENSIVE: DEFENSIF(a) is equal to a if defensive mode is active, 0 else.
45  *
46  * Authors:
47  * Nicolas Mansard, Bruno Renier
48  *
49  *****************************************************************************/
50 
51 #ifndef __VP_DEBUG_HH
52 #define __VP_DEBUG_HH
53 
54 #include <visp/vpConfig.h>
55 #include <stdio.h>
56 #include <stdarg.h>
57 #include <iostream>
58 
59 
60 #ifdef WIN32
61 # ifndef __FUNCTION__
62 # define __FUNCTION__ " "
63 # endif
64 #endif
65 
66 #ifndef VP_DEBUG_MODE
67 # define VP_DEBUG_MODE 0
68 #endif
69 
70 
138 {
139 private:
140  const char* currentFile; //Name of the file to use in the displays
141  const char* currentFunc; //Name of the function to use in the displays
142  int currentLine; //Line to use in the displays
143 
144  //if true, output to std::cerr/stderr else std::cout/stdout
145  bool err;
146  //string to display before anything else
147  const char* header;
148 
149 public:
159  vpTraceOutput(const char* file, int line, const char* func, bool error=false, const char * s=NULL) :
160  currentFile(file),
161  currentFunc(func),
162  currentLine(line),
163  err(error),
164  header(s)
165  {}
166 
172  void operator()(int niv, const char* format, ...)
173  {
174  //if the niv level is inferior to VP_DEBUG_MODE
175  if(VP_DEBUG_MODE >= niv)
176  {
177  //gets the variable list of arguments
178  va_list args;
179  va_start(args, format);
180 
181  if (err)
182  std::cerr << "(N" << niv << ") " ;
183  else
184  std::cout << "(N" << niv << ") " ;
185 
186  //calls display with it
187  display(format, args);
188 
189  va_end(args);
190  }
191  }
192 
197  void operator()(const char* format, ...)
198  {
199  //gets the variable list of arguments
200  va_list args;
201  va_start(args, format);
202 
203 #ifdef VP_DEBUG
204  std::cout<<"(N0) ";
205 #endif
206 
207  //calls display with it
208  display(format, args);
209 
210  va_end(args);
211  }
212 
222  void display(const char* format, va_list args)
223  {
224  //if we want to write to std::cerr/stderr
225  if(err)
226  {
227  //first writes the header if there is one
228  if(header != NULL) std::cerr<<header;
229  //then writes the recorded namefile, function and line
230  std::cerr << "!!\t" << currentFile << ": " <<currentFunc << "(#" << currentLine << ") :" ;
231  //and finally writes the message passed to () operator.
232  vfprintf (stderr, format, args);
233  fprintf (stderr, "\n");
234  //flushes the buffer
235  fflush (stderr);
236  }
237  else
238  {
239  //first writes the header if there is one
240  if(header != NULL) std::cout<<header;
241  //then writes the recorded namefile, function and line
242  std::cout <<currentFile << ": " << currentFunc << "(#" << currentLine << ") :" ;
243  //and finally writes the message passed to () operator.
244  vprintf (format, args);
245  printf ("\n");
246  //flushes the buffer
247  fflush (stdout);
248  }
249  }
250 
251 };
252 
253 
254 /* ------------------------------------------------------------------------- */
255 /* --- vpTRACE IN/OUT FONCTION --------------------------------------------- */
256 /* ------------------------------------------------------------------------- */
257 
275 #define vpIN_FCT (vpTraceOutput(__FILE__,__LINE__, __FUNCTION__, false, "begin "))
276 
277 
295 #define vpOUT_FCT (vpTraceOutput(__FILE__,__LINE__, __FUNCTION__, false, "end "))
296 
297 
298 
299 /* -------------------------------------------------------------------------- */
300 /* --- vpTRACE -------------------------------------------------------------- */
301 /* -------------------------------------------------------------------------- */
302 
327 #define vpCTRACE std::cout << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
328 
329 
354 #define vpCERROR std::cerr << "!!\t" << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
355 
379 #define vpERROR_TRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, true))
380 
401 #define vpTRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, false))
402 
403 
404 /* ------------------------------------------------------------------------- */
405 /* --- VP_DEBUG ------------------------------------------------------------ */
406 /* ------------------------------------------------------------------------- */
407 
408 #ifdef VP_DEBUG
409 
431 #define vpDERROR_TRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, true))
432 
454 #define vpDEBUG_TRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, false))
455 
478 #define vpCDEBUG(niv) if (VP_DEBUG_MODE < niv) ; else \
479  std::cout << "(N" << niv << ") "<< __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
480 
502 #define vpDEBUG_ENABLE(niv) (VP_DEBUG_MODE >= niv)
503 
504 #else
505 
506 inline void vpDERROR_TRACE(int /* niv */, const char * /* a */, ...){}
507 inline void vpDEBUG_TRACE(int /* niv */, const char * /* a */, ...){}
508 
509 #define vpCDEBUG(niv) if(false) std::cout // Warning C4127
510 #define vpDEBUG_ENABLE(niv) (false) // Warning C4127
511 
512 #endif
513 
514 /* -------------------------------------------------------------------------- */
515 /* --- DEFENSIF ------------------------------------------------------------- */
516 /* -------------------------------------------------------------------------- */
517 #ifdef VP_DEFENSIF
518 #define DEFENSIF(a) (a)
519 #else
520 #define DEFENSIF(a) (0)
521 #endif /*#ifdef DEFENSIF*/
522 
523 
524 #endif /* #ifdef __DEBUG_HH */
525 
526 /*
527  * Local variables:
528  * c-basic-offset: 4
529  * End:
530  */
#define vpDEBUG_TRACE
Definition: vpDebug.h:454
#define vpDERROR_TRACE
Definition: vpDebug.h:431
This class is used to display debug or error messages.
Definition: vpDebug.h:137
vpTraceOutput(const char *file, int line, const char *func, bool error=false, const char *s=NULL)
Definition: vpDebug.h:159
void operator()(const char *format,...)
Definition: vpDebug.h:197
void display(const char *format, va_list args)
Definition: vpDebug.h:222
void operator()(int niv, const char *format,...)
Definition: vpDebug.h:172