Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpDebug.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Debug and trace macro.
33  *
34  * - TRACING: vpTRACE and vpERROR_TRACE work like printf with carreer
35  *return at the end of the string. vpCERROR et vpCTRACE work like the C++
36  *output streams std::cout and std::cerr.
37  * - DEBUGING: vpDEBUG_TRACE(niv) and vpDERROR_TRACE(niv), work like
38  *printf, but print only if the tracing level niv is greater than the debug
39  *level VP_DEBUG_MODE. vpCDEBUG(niv) work like the C++ output
40  *stream std::cout. vpDEBUG_ENABLE(niv) is equal to 1 if the
41  *debug level niv is greater than the debug mode
42  * VP_DEBUG_MODE, 0 else.
43  * - PROG DEFENSIVE: DEFENSIF(a) is equal to a if defensive mode is active,
44  *0 else.
45  *
46  * Authors:
47  * Nicolas Mansard, Bruno Renier
48  *
49  *****************************************************************************/
50 
51 #ifndef _vpDebug_h_
52 #define _vpDebug_h_
53 
54 #include <iostream>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <visp3/core/vpConfig.h>
58 
59 #if defined(_WIN32)
60 #ifndef __FUNCTION__
61 #define __FUNCTION__ " "
62 #endif
63 #endif
64 
65 #ifndef VP_DEBUG_MODE
66 #define VP_DEBUG_MODE 0
67 #endif
68 
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:
160  vpTraceOutput(const char *file, int line, const char *func, bool error = false, const char *s = NULL)
161  : currentFile(file), currentFunc(func), currentLine(line), err(error), header(s)
162  {
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  // gets the variable list of arguments
175  va_list args;
176  va_start(args, format);
177 
178  if (err)
179  std::cerr << "(L" << level << ") ";
180  else
181  std::cout << "(L" << level << ") ";
182 
183  // calls display with it
184  display(format, args);
185 
186  va_end(args);
187  }
188  }
189 
194  void operator()(const char *format, ...)
195  {
196  // gets the variable list of arguments
197  va_list args;
198  va_start(args, format);
199 
200 #ifdef VP_DEBUG
201  std::cout << "(L0) ";
202 #endif
203 
204  // calls display with it
205  display(format, args);
206 
207  va_end(args);
208  }
209 
219  void display(const char *format, va_list args)
220  {
221  // if we want to write to std::cerr/stderr
222  if (err) {
223  // first writes the header if there is one
224  if (header != NULL)
225  std::cerr << header;
226  // then writes the recorded namefile, function and line
227  std::cerr << "!!\t" << currentFile << ": " << currentFunc << "(#" << currentLine << ") : ";
228  // and finally writes the message passed to () operator.
229  vfprintf(stderr, format, args);
230  fprintf(stderr, "\n");
231  // flushes the buffer
232  fflush(stderr);
233  } else {
234  // first writes the header if there is one
235  if (header != NULL)
236  std::cout << header;
237  // then writes the recorded namefile, function and line
238  std::cout << currentFile << ": " << currentFunc << "(#" << currentLine << ") : ";
239  // and finally writes the message passed to () operator.
240  vprintf(format, args);
241  printf("\n");
242  // flushes the buffer
243  fflush(stdout);
244  }
245  }
246 };
247 
248 /* -------------------------------------------------------------------------
249  */
250 /* --- vpTRACE IN/OUT FONCTION ---------------------------------------------
251  */
252 /* -------------------------------------------------------------------------
253  */
254 
255 #ifdef VP_TRACE // Activate the trace mode
256 
275 #define vpIN_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "begin "))
276 
295 #define vpOUT_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "end "))
296 
297 #else // #ifdef VP_TRACE
298 
299 inline void vpIN_FCT(const char * /* a */, ...) {}
300 inline void vpOUT_FCT(const char * /* a */, ...) {}
301 
302 #endif // #ifdef VP_TRACE
303 
304 /* --------------------------------------------------------------------------
305  */
306 /* --- vpTRACE --------------------------------------------------------------
307  */
308 /* --------------------------------------------------------------------------
309  */
310 
311 #ifdef VP_TRACE
312 
338 #define vpCTRACE std::cout << "(L0) " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
339 
365 #define vpCERROR \
366  std::cerr << "(L0) " \
367  << "!!\t" << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
368 
393 #define vpERROR_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, true))
394 
416 #define vpTRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false))
417 
418 #else // #ifdef VP_TRACE
419 
420 #define vpCTRACE \
421  if (false) \
422  std::cout // Warning C4127
423 #define vpCERROR \
424  if (false) \
425  std::cerr // Warning C4127
426 
427 inline void vpERROR_TRACE(const char * /* a */, ...) {}
428 inline void vpERROR_TRACE(int /* level */, const char * /* a */, ...) {}
429 inline void vpTRACE(const char * /* a */, ...) {}
430 inline void vpTRACE(int /* level */, const char * /* a */, ...) {}
431 
432 #endif // #ifdef VP_TRACE
433 
434 /* -------------------------------------------------------------------------
435  */
436 /* --- VP_DEBUG ------------------------------------------------------------
437  */
438 /* -------------------------------------------------------------------------
439  */
440 
441 #ifdef VP_DEBUG
442 
464 #define vpDERROR_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, true))
465 
487 #define vpDEBUG_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false))
488 
511 #define vpCDEBUG(level) \
512  if (VP_DEBUG_MODE < level) \
513  ; \
514  else \
515  std::cout << "(L" << level << ") " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
516 
538 #define vpDEBUG_ENABLE(level) (VP_DEBUG_MODE >= level)
539 
540 #else // #ifdef VP_DEBUG
541 
542 inline void vpDERROR_TRACE(const char * /* a */, ...) {}
543 inline void vpDEBUG_TRACE(const char * /* a */, ...) {}
544 inline void vpDERROR_TRACE(int /* level */, const char * /* a */, ...) {}
545 inline void vpDEBUG_TRACE(int /* level */, const char * /* a */, ...) {}
546 
547 #define vpCDEBUG(level) \
548  if (false) \
549  std::cout // Warning C4127
550 #define vpDEBUG_ENABLE(level) (false) // Warning C4127
551 
552 #endif // #ifdef VP_DEBUG
553 
554 /* --------------------------------------------------------------------------
555  */
556 /* --- DEFENSIF -------------------------------------------------------------
557  */
558 /* --------------------------------------------------------------------------
559  */
560 #ifdef VP_DEFENSIF
561 #define DEFENSIF(a) (a)
562 #else
563 #define DEFENSIF(a) (0)
564 #endif /*#ifdef DEFENSIF*/
565 
566 #endif /* #ifdef __DEBUG_HH */
#define vpERROR_TRACE
Definition: vpDebug.h:393
#define vpTRACE
Definition: vpDebug.h:416
#define vpOUT_FCT
Definition: vpDebug.h:295
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:160
void operator()(const char *format,...)
Definition: vpDebug.h:194
void operator()(int level, const char *format,...)
Definition: vpDebug.h:170
#define vpDERROR_TRACE
Definition: vpDebug.h:464
void display(const char *format, va_list args)
Definition: vpDebug.h:219
#define vpDEBUG_TRACE
Definition: vpDebug.h:487
#define vpIN_FCT
Definition: vpDebug.h:275