Visual Servoing Platform  version 3.6.1 under development (2024-04-19)
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 = nullptr)
156  : currentFile(file), currentFunc(func), currentLine(line), err(error), header(s)
157  { }
158 
164  void operator()(int level, const char *format, ...)
165  {
166  // if the level is inferior to VP_DEBUG_MODE
167  if (VP_DEBUG_MODE >= level) {
168  // gets the variable list of arguments
169  va_list args;
170  va_start(args, format);
171 
172  if (err)
173  std::cerr << "(L" << level << ") ";
174  else
175  std::cout << "(L" << level << ") ";
176 
177  // calls display with it
178  display(format, args);
179 
180  va_end(args);
181  }
182  }
183 
188  void operator()(const char *format, ...)
189  {
190  // gets the variable list of arguments
191  va_list args;
192  va_start(args, format);
193 
194 #ifdef VP_DEBUG
195  std::cout << "(L0) ";
196 #endif
197 
198  // calls display with it
199  display(format, args);
200 
201  va_end(args);
202  }
203 
213  void display(const char *format, va_list args)
214  {
215  // if we want to write to std::cerr/stderr
216  if (err) {
217  // first writes the header if there is one
218  if (header != nullptr) {
219  std::cerr << header;
220  }
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  }
229  else {
230  // first writes the header if there is one
231  if (header != nullptr) {
232  std::cout << header;
233  }
234  // then writes the recorded namefile, function and line
235  std::cout << currentFile << ": " << currentFunc << "(#" << currentLine << ") : ";
236  // and finally writes the message passed to () operator.
237  vprintf(format, args);
238  printf("\n");
239  // flushes the buffer
240  fflush(stdout);
241  }
242  }
243 };
244 
245 /* ------------------------------------------------------------------------- */
246 /* --- vpTRACE IN/OUT FONCTION --------------------------------------------- */
247 /* ------------------------------------------------------------------------- */
248 
249 #ifdef VP_TRACE // Activate the trace mode
250 
269 #define vpIN_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "begin "))
270 
289 #define vpOUT_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "end "))
290 
291 #else // #ifdef VP_TRACE
292 
293 inline void vpIN_FCT(const char * /* a */, ...) { }
294 inline void vpOUT_FCT(const char * /* a */, ...) { }
295 
296 #endif // #ifdef VP_TRACE
297 
298 /* -------------------------------------------------------------------------- */
299 /* --- vpTRACE -------------------------------------------------------------- */
300 /* -------------------------------------------------------------------------- */
301 
302 #ifdef VP_TRACE
303 
329 #define vpCTRACE std::cout << "(L0) " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
330 
356 #define vpCERROR std::cerr << "(L0) " << "!!\t" << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
357 
382 #define vpERROR_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, true))
383 
405 #define vpTRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false))
406 
407 #else // #ifdef VP_TRACE
408 
409 #define vpCTRACE \
410  if (false) \
411  std::cout // Warning C4127
412 #define vpCERROR \
413  if (false) \
414  std::cerr // Warning C4127
415 
416 inline void vpERROR_TRACE(const char * /* a */, ...) { }
417 inline void vpERROR_TRACE(int /* level */, const char * /* a */, ...) { }
418 inline void vpTRACE(const char * /* a */, ...) { }
419 inline void vpTRACE(int /* level */, const char * /* a */, ...) { }
420 
421 #endif // #ifdef VP_TRACE
422 
423 /* ------------------------------------------------------------------------- */
424 /* --- VP_DEBUG ------------------------------------------------------------ */
425 /* ------------------------------------------------------------------------- */
426 
427 #ifdef VP_DEBUG
428 
450 #define vpDERROR_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, true))
451 
473 #define vpDEBUG_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false))
474 
497 #define vpCDEBUG(level) \
498  if (VP_DEBUG_MODE < level) \
499  ; \
500  else \
501  std::cout << "(L" << level << ") " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
502 
524 #define vpDEBUG_ENABLE(level) (VP_DEBUG_MODE >= level)
525 
526 #else // #ifdef VP_DEBUG
527 
528 inline void vpDERROR_TRACE(const char * /* a */, ...) { }
529 inline void vpDEBUG_TRACE(const char * /* a */, ...) { }
530 inline void vpDERROR_TRACE(int /* level */, const char * /* a */, ...) { }
531 inline void vpDEBUG_TRACE(int /* level */, const char * /* a */, ...) { }
532 
533 #define vpCDEBUG(level) \
534  if (false) \
535  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 #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=nullptr)
Definition: vpDebug.h:155
void operator()(const char *format,...)
Definition: vpDebug.h:188
void operator()(int level, const char *format,...)
Definition: vpDebug.h:164
void display(const char *format, va_list args)
Definition: vpDebug.h:213
#define vpIN_FCT
Definition: vpDebug.h:269
#define vpTRACE
Definition: vpDebug.h:405
#define vpOUT_FCT
Definition: vpDebug.h:289
#define vpDEBUG_TRACE
Definition: vpDebug.h:473
#define vpDERROR_TRACE
Definition: vpDebug.h:450
#define vpERROR_TRACE
Definition: vpDebug.h:382