ViSP  2.9.0
vpDebug.h
1 /****************************************************************************
2  *
3  * $Id: vpDebug.h 4604 2014-01-21 14:15:23Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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 #if defined(_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 
140 {
141  private:
142  const char* currentFile; //Name of the file to use in the displays
143  const char* currentFunc; //Name of the function to use in the displays
144  int currentLine; //Line to use in the displays
145 
146  //if true, output to std::cerr/stderr else std::cout/stdout
147  bool err;
148  //string to display before anything else
149  const char* header;
150 
151  public:
161  vpTraceOutput(const char* file, int line, const char* func, bool error=false, const char * s=NULL) :
162  currentFile(file),
163  currentFunc(func),
164  currentLine(line),
165  err(error),
166  header(s)
167  {}
168 
174  void operator()(int level, const char* format, ...)
175  {
176  //if the level is inferior to VP_DEBUG_MODE
177  if(VP_DEBUG_MODE >= level)
178  {
179  //gets the variable list of arguments
180  va_list args;
181  va_start(args, format);
182 
183  if (err)
184  std::cerr << "(L" << level << ") " ;
185  else
186  std::cout << "(L" << level << ") " ;
187 
188  //calls display with it
189  display(format, args);
190 
191  va_end(args);
192  }
193  }
194 
199  void operator()(const char* format, ...)
200  {
201  //gets the variable list of arguments
202  va_list args;
203  va_start(args, format);
204 
205 #ifdef VP_DEBUG
206  std::cout<<"(L0) ";
207 #endif
208 
209  //calls display with it
210  display(format, args);
211 
212  va_end(args);
213  }
214 
224  void display(const char* format, va_list args)
225  {
226  //if we want to write to std::cerr/stderr
227  if(err)
228  {
229  //first writes the header if there is one
230  if(header != NULL) std::cerr<<header;
231  //then writes the recorded namefile, function and line
232  std::cerr << "!!\t" << currentFile << ": " <<currentFunc << "(#" << currentLine << ") : " ;
233  //and finally writes the message passed to () operator.
234  vfprintf (stderr, format, args);
235  fprintf (stderr, "\n");
236  //flushes the buffer
237  fflush (stderr);
238  }
239  else
240  {
241  //first writes the header if there is one
242  if(header != NULL) std::cout<<header;
243  //then writes the recorded namefile, function and line
244  std::cout <<currentFile << ": " << currentFunc << "(#" << currentLine << ") : " ;
245  //and finally writes the message passed to () operator.
246  vprintf (format, args);
247  printf ("\n");
248  //flushes the buffer
249  fflush (stdout);
250  }
251  }
252 
253 };
254 
255 
256 /* ------------------------------------------------------------------------- */
257 /* --- vpTRACE IN/OUT FONCTION --------------------------------------------- */
258 /* ------------------------------------------------------------------------- */
259 
260 #ifdef VP_TRACE // Activate the trace mode
261 
280 #define vpIN_FCT (vpTraceOutput(__FILE__,__LINE__, __FUNCTION__, false, "begin "))
281 
282 
301 #define vpOUT_FCT (vpTraceOutput(__FILE__,__LINE__, __FUNCTION__, false, "end "))
302 
303 #else // #ifdef VP_TRACE
304 
305 inline void vpIN_FCT (const char * /* a */, ...){}
306 inline void vpOUT_FCT (const char * /* a */, ...){}
307 
308 #endif // #ifdef VP_TRACE
309 
310 /* -------------------------------------------------------------------------- */
311 /* --- vpTRACE -------------------------------------------------------------- */
312 /* -------------------------------------------------------------------------- */
313 
314 #ifdef VP_TRACE
315 
341 #define vpCTRACE std::cout << "(L0) " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
342 
343 
369 #define vpCERROR std::cerr << "(L0) " << "!!\t" << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
370 
395 #define vpERROR_TRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, true))
396 
418 #define vpTRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, false))
419 
420 #else // #ifdef VP_TRACE
421 
422 #define vpCTRACE if(false) std::cout // Warning C4127
423 #define vpCERROR if(false) std::cerr // Warning C4127
424 
425 inline void vpERROR_TRACE (const char * /* a */, ...){}
426 inline void vpERROR_TRACE (int /* level */, const char * /* a */, ...){}
427 inline void vpTRACE (const char * /* a */, ...){}
428 inline void vpTRACE (int /* level */, const char * /* a */, ...){}
429 
430 #endif // #ifdef VP_TRACE
431 
432 /* ------------------------------------------------------------------------- */
433 /* --- VP_DEBUG ------------------------------------------------------------ */
434 /* ------------------------------------------------------------------------- */
435 
436 #ifdef VP_DEBUG
437 
459 #define vpDERROR_TRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, true))
460 
482 #define vpDEBUG_TRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, false))
483 
506 #define vpCDEBUG(level) if (VP_DEBUG_MODE < level) ; else \
507  std::cout << "(L" << level << ") "<< __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
508 
530 #define vpDEBUG_ENABLE(level) (VP_DEBUG_MODE >= level)
531 
532 #else // #ifdef VP_DEBUG
533 
534 inline void vpDERROR_TRACE(const char * /* a */, ...){}
535 inline void vpDEBUG_TRACE(const char * /* a */, ...){}
536 inline void vpDERROR_TRACE(int /* level */, const char * /* a */, ...){}
537 inline void vpDEBUG_TRACE(int /* level */, const char * /* a */, ...){}
538 
539 #define vpCDEBUG(level) if(false) std::cout // Warning C4127
540 #define vpDEBUG_ENABLE(level) (false) // Warning C4127
541 
542 #endif // #ifdef VP_DEBUG
543 
544 /* -------------------------------------------------------------------------- */
545 /* --- DEFENSIF ------------------------------------------------------------- */
546 /* -------------------------------------------------------------------------- */
547 #ifdef VP_DEFENSIF
548 #define DEFENSIF(a) (a)
549 #else
550 #define DEFENSIF(a) (0)
551 #endif /*#ifdef DEFENSIF*/
552 
553 
554 #endif /* #ifdef __DEBUG_HH */
#define vpDEBUG_TRACE
Definition: vpDebug.h:482
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpTRACE
Definition: vpDebug.h:418
#define vpDERROR_TRACE
Definition: vpDebug.h:459
#define vpIN_FCT
Definition: vpDebug.h:280
This class is used to display debug or error messages.
Definition: vpDebug.h:139
vpTraceOutput(const char *file, int line, const char *func, bool error=false, const char *s=NULL)
Definition: vpDebug.h:161
void operator()(const char *format,...)
Definition: vpDebug.h:199
void operator()(int level, const char *format,...)
Definition: vpDebug.h:174
void display(const char *format, va_list args)
Definition: vpDebug.h:224
#define vpOUT_FCT
Definition: vpDebug.h:301