Visual Servoing Platform  version 3.6.0 under development (2023-09-27)
vpDebug.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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
34  * return at the end of the string. vpCERROR et vpCTRACE work like the C++
35  * output streams std::cout and std::cerr.
36  * - DEBUGING: vpDEBUG_TRACE(niv) and vpDERROR_TRACE(niv), work like
37  * printf, but print only if the tracing level niv is greater than the debug
38  * level VP_DEBUG_MODE. vpCDEBUG(niv) work like the C++ output
39  * stream std::cout. vpDEBUG_ENABLE(niv) is equal to 1 if the
40  * debug level niv is greater than the debug mode
41  * VP_DEBUG_MODE, 0 else.
42  * - PROG DEFENSIVE: DEFENSIF(a) is equal to a if defensive mode is active,
43  * 0 else.
44  */
45 
46 #ifndef _vpDebug_h_
47 #define _vpDebug_h_
48 
49 #include <iostream>
50 #include <stdarg.h>
51 #include <stdio.h>
52 #include <visp3/core/vpConfig.h>
53 
54 #if defined(_WIN32)
55 #ifndef __FUNCTION__
56 #define __FUNCTION__ " "
57 #endif
58 #endif
59 
60 #ifndef VP_DEBUG_MODE
61 #define VP_DEBUG_MODE 0
62 #endif
63 
133 {
134 private:
135  const char *currentFile; // Name of the file to use in the displays
136  const char *currentFunc; // Name of the function to use in the displays
137  int currentLine; // Line to use in the displays
138 
139  // if true, output to std::cerr/stderr else std::cout/stdout
140  bool err;
141  // string to display before anything else
142  const char *header;
143 
144 public:
155  vpTraceOutput(const char *file, int line, const char *func, bool error = false, const char *s = NULL)
156  : currentFile(file), currentFunc(func), currentLine(line), err(error), header(s)
157  {
158  }
159 
165  void operator()(int level, const char *format, ...)
166  {
167  // if the level is inferior to VP_DEBUG_MODE
168  if (VP_DEBUG_MODE >= level) {
169  // gets the variable list of arguments
170  va_list args;
171  va_start(args, format);
172 
173  if (err)
174  std::cerr << "(L" << level << ") ";
175  else
176  std::cout << "(L" << level << ") ";
177 
178  // calls display with it
179  display(format, args);
180 
181  va_end(args);
182  }
183  }
184 
189  void operator()(const char *format, ...)
190  {
191  // gets the variable list of arguments
192  va_list args;
193  va_start(args, format);
194 
195 #ifdef VP_DEBUG
196  std::cout << "(L0) ";
197 #endif
198 
199  // calls display with it
200  display(format, args);
201 
202  va_end(args);
203  }
204 
214  void display(const char *format, va_list args)
215  {
216  // if we want to write to std::cerr/stderr
217  if (err) {
218  // first writes the header if there is one
219  if (header != NULL)
220  std::cerr << header;
221  // then writes the recorded namefile, function and line
222  std::cerr << "!!\t" << currentFile << ": " << currentFunc << "(#" << currentLine << ") : ";
223  // and finally writes the message passed to () operator.
224  vfprintf(stderr, format, args);
225  fprintf(stderr, "\n");
226  // flushes the buffer
227  fflush(stderr);
228  } else {
229  // first writes the header if there is one
230  if (header != NULL)
231  std::cout << header;
232  // then writes the recorded namefile, function and line
233  std::cout << currentFile << ": " << currentFunc << "(#" << currentLine << ") : ";
234  // and finally writes the message passed to () operator.
235  vprintf(format, args);
236  printf("\n");
237  // flushes the buffer
238  fflush(stdout);
239  }
240  }
241 };
242 
243 /* -------------------------------------------------------------------------
244  */
245 /* --- vpTRACE IN/OUT FONCTION ---------------------------------------------
246  */
247 /* -------------------------------------------------------------------------
248  */
249 
250 #ifdef VP_TRACE // Activate the trace mode
251 
270 #define vpIN_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "begin "))
271 
290 #define vpOUT_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "end "))
291 
292 #else // #ifdef VP_TRACE
293 
294 inline void vpIN_FCT(const char * /* a */, ...) {}
295 inline void vpOUT_FCT(const char * /* a */, ...) {}
296 
297 #endif // #ifdef VP_TRACE
298 
299 /* --------------------------------------------------------------------------
300  */
301 /* --- vpTRACE --------------------------------------------------------------
302  */
303 /* --------------------------------------------------------------------------
304  */
305 
306 #ifdef VP_TRACE
307 
333 #define vpCTRACE std::cout << "(L0) " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
334 
360 #define vpCERROR \
361  std::cerr << "(L0) " \
362  << "!!\t" << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
363 
388 #define vpERROR_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, true))
389 
411 #define vpTRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false))
412 
413 #else // #ifdef VP_TRACE
414 
415 #define vpCTRACE \
416  if (false) \
417  std::cout // Warning C4127
418 #define vpCERROR \
419  if (false) \
420  std::cerr // Warning C4127
421 
422 inline void vpERROR_TRACE(const char * /* a */, ...) {}
423 inline void vpERROR_TRACE(int /* level */, const char * /* a */, ...) {}
424 inline void vpTRACE(const char * /* a */, ...) {}
425 inline void vpTRACE(int /* level */, const char * /* a */, ...) {}
426 
427 #endif // #ifdef VP_TRACE
428 
429 /* -------------------------------------------------------------------------
430  */
431 /* --- VP_DEBUG ------------------------------------------------------------
432  */
433 /* -------------------------------------------------------------------------
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) \
507  if (VP_DEBUG_MODE < level) \
508  ; \
509  else \
510  std::cout << "(L" << level << ") " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
511 
533 #define vpDEBUG_ENABLE(level) (VP_DEBUG_MODE >= level)
534 
535 #else // #ifdef VP_DEBUG
536 
537 inline void vpDERROR_TRACE(const char * /* a */, ...) {}
538 inline void vpDEBUG_TRACE(const char * /* a */, ...) {}
539 inline void vpDERROR_TRACE(int /* level */, const char * /* a */, ...) {}
540 inline void vpDEBUG_TRACE(int /* level */, const char * /* a */, ...) {}
541 
542 #define vpCDEBUG(level) \
543  if (false) \
544  std::cout // Warning C4127
545 #define vpDEBUG_ENABLE(level) (false) // Warning C4127
546 
547 #endif // #ifdef VP_DEBUG
548 
549 /* --------------------------------------------------------------------------
550  */
551 /* --- DEFENSIF -------------------------------------------------------------
552  */
553 /* --------------------------------------------------------------------------
554  */
555 #ifdef VP_DEFENSIF
556 #define DEFENSIF(a) (a)
557 #else
558 #define DEFENSIF(a) (0)
559 #endif /*#ifdef DEFENSIF*/
560 
561 #endif /* #ifdef __DEBUG_HH */
This class is used to display debug or error messages.
Definition: vpDebug.h:133
vpTraceOutput(const char *file, int line, const char *func, bool error=false, const char *s=NULL)
Definition: vpDebug.h:155
void operator()(const char *format,...)
Definition: vpDebug.h:189
void operator()(int level, const char *format,...)
Definition: vpDebug.h:165
void display(const char *format, va_list args)
Definition: vpDebug.h:214
#define vpIN_FCT
Definition: vpDebug.h:270
#define vpTRACE
Definition: vpDebug.h:411
#define vpOUT_FCT
Definition: vpDebug.h:290
#define vpDEBUG_TRACE
Definition: vpDebug.h:482
#define vpDERROR_TRACE
Definition: vpDebug.h:459
#define vpERROR_TRACE
Definition: vpDebug.h:388