Visual Servoing Platform  version 3.0.0
vpDebug.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Debug and trace macro.
32  *
33  * - TRACING: vpTRACE and vpERROR_TRACE work like printf with carreer return at the end of the string.
34  * vpCERROR et vpCTRACE work like the C++ output streams std::cout and std::cerr.
35  * - DEBUGING: vpDEBUG_TRACE(niv) and vpDERROR_TRACE(niv), work like printf, but print only if the
36  * tracing level niv is greater than the debug level VP_DEBUG_MODE.
37  * vpCDEBUG(niv) work like the C++ output stream std::cout.
38  * vpDEBUG_ENABLE(niv) is equal to 1 if the debug level niv is greater than the debug mode
39  * VP_DEBUG_MODE, 0 else.
40  * - PROG DEFENSIVE: DEFENSIF(a) is equal to a if defensive mode is active, 0 else.
41  *
42  * Authors:
43  * Nicolas Mansard, Bruno Renier
44  *
45  *****************************************************************************/
46 
47 #ifndef __VP_DEBUG_HH
48 #define __VP_DEBUG_HH
49 
50 #include <visp3/core/vpConfig.h>
51 #include <stdio.h>
52 #include <stdarg.h>
53 #include <iostream>
54 
55 
56 #if defined(_WIN32)
57 # ifndef __FUNCTION__
58 # define __FUNCTION__ " "
59 # endif
60 #endif
61 
62 #ifndef VP_DEBUG_MODE
63 # define VP_DEBUG_MODE 0
64 #endif
65 
66 
136 {
137  private:
138  const char* currentFile; //Name of the file to use in the displays
139  const char* currentFunc; //Name of the function to use in the displays
140  int currentLine; //Line to use in the displays
141 
142  //if true, output to std::cerr/stderr else std::cout/stdout
143  bool err;
144  //string to display before anything else
145  const char* header;
146 
147  public:
157  vpTraceOutput(const char* file, int line, const char* func, bool error=false, const char * s=NULL) :
158  currentFile(file),
159  currentFunc(func),
160  currentLine(line),
161  err(error),
162  header(s)
163  {}
164 
170  void operator()(int level, const char* format, ...)
171  {
172  //if the level is inferior to VP_DEBUG_MODE
173  if(VP_DEBUG_MODE >= level)
174  {
175  //gets the variable list of arguments
176  va_list args;
177  va_start(args, format);
178 
179  if (err)
180  std::cerr << "(L" << level << ") " ;
181  else
182  std::cout << "(L" << level << ") " ;
183 
184  //calls display with it
185  display(format, args);
186 
187  va_end(args);
188  }
189  }
190 
195  void operator()(const char* format, ...)
196  {
197  //gets the variable list of arguments
198  va_list args;
199  va_start(args, format);
200 
201 #ifdef VP_DEBUG
202  std::cout<<"(L0) ";
203 #endif
204 
205  //calls display with it
206  display(format, args);
207 
208  va_end(args);
209  }
210 
220  void display(const char* format, va_list args)
221  {
222  //if we want to write to std::cerr/stderr
223  if(err)
224  {
225  //first writes the header if there is one
226  if(header != NULL) std::cerr<<header;
227  //then writes the recorded namefile, function and line
228  std::cerr << "!!\t" << currentFile << ": " <<currentFunc << "(#" << currentLine << ") : " ;
229  //and finally writes the message passed to () operator.
230  vfprintf (stderr, format, args);
231  fprintf (stderr, "\n");
232  //flushes the buffer
233  fflush (stderr);
234  }
235  else
236  {
237  //first writes the header if there is one
238  if(header != NULL) std::cout<<header;
239  //then writes the recorded namefile, function and line
240  std::cout <<currentFile << ": " << currentFunc << "(#" << currentLine << ") : " ;
241  //and finally writes the message passed to () operator.
242  vprintf (format, args);
243  printf ("\n");
244  //flushes the buffer
245  fflush (stdout);
246  }
247  }
248 
249 };
250 
251 
252 /* ------------------------------------------------------------------------- */
253 /* --- vpTRACE IN/OUT FONCTION --------------------------------------------- */
254 /* ------------------------------------------------------------------------- */
255 
256 #ifdef VP_TRACE // Activate the trace mode
257 
276 #define vpIN_FCT (vpTraceOutput(__FILE__,__LINE__, __FUNCTION__, false, "begin "))
277 
278 
297 #define vpOUT_FCT (vpTraceOutput(__FILE__,__LINE__, __FUNCTION__, false, "end "))
298 
299 #else // #ifdef VP_TRACE
300 
301 inline void vpIN_FCT (const char * /* a */, ...){}
302 inline void vpOUT_FCT (const char * /* a */, ...){}
303 
304 #endif // #ifdef VP_TRACE
305 
306 /* -------------------------------------------------------------------------- */
307 /* --- vpTRACE -------------------------------------------------------------- */
308 /* -------------------------------------------------------------------------- */
309 
310 #ifdef VP_TRACE
311 
337 #define vpCTRACE std::cout << "(L0) " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
338 
339 
365 #define vpCERROR std::cerr << "(L0) " << "!!\t" << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
366 
391 #define vpERROR_TRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, true))
392 
414 #define vpTRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, false))
415 
416 #else // #ifdef VP_TRACE
417 
418 #define vpCTRACE if(false) std::cout // Warning C4127
419 #define vpCERROR if(false) std::cerr // Warning C4127
420 
421 inline void vpERROR_TRACE (const char * /* a */, ...){}
422 inline void vpERROR_TRACE (int /* level */, const char * /* a */, ...){}
423 inline void vpTRACE (const char * /* a */, ...){}
424 inline void vpTRACE (int /* level */, const char * /* a */, ...){}
425 
426 #endif // #ifdef VP_TRACE
427 
428 /* ------------------------------------------------------------------------- */
429 /* --- VP_DEBUG ------------------------------------------------------------ */
430 /* ------------------------------------------------------------------------- */
431 
432 #ifdef VP_DEBUG
433 
455 #define vpDERROR_TRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, true))
456 
478 #define vpDEBUG_TRACE (vpTraceOutput( __FILE__,__LINE__, __FUNCTION__, false))
479 
502 #define vpCDEBUG(level) if (VP_DEBUG_MODE < level) ; else \
503  std::cout << "(L" << level << ") "<< __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
504 
526 #define vpDEBUG_ENABLE(level) (VP_DEBUG_MODE >= level)
527 
528 #else // #ifdef VP_DEBUG
529 
530 inline void vpDERROR_TRACE(const char * /* a */, ...){}
531 inline void vpDEBUG_TRACE(const char * /* a */, ...){}
532 inline void vpDERROR_TRACE(int /* level */, const char * /* a */, ...){}
533 inline void vpDEBUG_TRACE(int /* level */, const char * /* a */, ...){}
534 
535 #define vpCDEBUG(level) if(false) std::cout // Warning C4127
536 #define vpDEBUG_ENABLE(level) (false) // Warning C4127
537 
538 #endif // #ifdef VP_DEBUG
539 
540 /* -------------------------------------------------------------------------- */
541 /* --- DEFENSIF ------------------------------------------------------------- */
542 /* -------------------------------------------------------------------------- */
543 #ifdef VP_DEFENSIF
544 #define DEFENSIF(a) (a)
545 #else
546 #define DEFENSIF(a) (0)
547 #endif /*#ifdef DEFENSIF*/
548 
549 
550 #endif /* #ifdef __DEBUG_HH */
#define vpERROR_TRACE
Definition: vpDebug.h:391
#define vpTRACE
Definition: vpDebug.h:414
#define vpOUT_FCT
Definition: vpDebug.h:297
This class is used to display debug or error messages.
Definition: vpDebug.h:135
vpTraceOutput(const char *file, int line, const char *func, bool error=false, const char *s=NULL)
Definition: vpDebug.h:157
void operator()(const char *format,...)
Definition: vpDebug.h:195
void operator()(int level, const char *format,...)
Definition: vpDebug.h:170
#define vpDERROR_TRACE
Definition: vpDebug.h:455
void display(const char *format, va_list args)
Definition: vpDebug.h:220
#define vpDEBUG_TRACE
Definition: vpDebug.h:478
#define vpIN_FCT
Definition: vpDebug.h:276