Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
vpDebug.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 VP_DEBUG_H
47 #define VP_DEBUG_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 
64 BEGIN_VISP_NAMESPACE
138 {
139 public:
150  vpTraceOutput(const char *file, int line, const char *func, bool error = false, const char *s = nullptr)
151  : currentFile(file), currentFunc(func), currentLine(line), err(error), header(s)
152  { }
153 
159  void operator()(int level, const char *format, ...)
160  {
161  // if the level is inferior to VP_DEBUG_MODE
162  if (VP_DEBUG_MODE >= level) {
163  // gets the variable list of arguments
164  va_list args;
165  va_start(args, format);
166 
167  if (err) {
168  std::cerr << "(L" << level << ") ";
169  }
170  else {
171  std::cout << "(L" << level << ") ";
172  }
173 
174  // calls display with it
175  display(format, args);
176 
177  va_end(args);
178  }
179  }
180 
185  void operator()(const char *format, ...)
186  {
187  // gets the variable list of arguments
188  va_list args;
189  va_start(args, format);
190 
191 #ifdef VP_DEBUG
192  std::cout << "(L0) ";
193 #endif
194 
195  // calls display with it
196  display(format, args);
197 
198  va_end(args);
199  }
200 
210  void display(const char *format, va_list args)
211  {
212  // if we want to write to std::cerr/stderr
213  if (err) {
214  // first writes the header if there is one
215  if (header != nullptr) {
216  std::cerr << header;
217  }
218  // then writes the recorded namefile, function and line
219  std::cerr << "!!\t" << currentFile << ": " << currentFunc << "(#" << currentLine << ") : ";
220  // and finally writes the message passed to () operator.
221  vfprintf(stderr, format, args);
222  fprintf(stderr, "\n");
223  // flushes the buffer
224  fflush(stderr);
225  }
226  else {
227  // first writes the header if there is one
228  if (header != nullptr) {
229  std::cout << header;
230  }
231  // then writes the recorded namefile, function and line
232  std::cout << currentFile << ": " << currentFunc << "(#" << currentLine << ") : ";
233  // and finally writes the message passed to () operator.
234  vprintf(format, args);
235  printf("\n");
236  // flushes the buffer
237  fflush(stdout);
238  }
239  }
240 private:
241  const char *currentFile; // Name of the file to use in the displays
242  const char *currentFunc; // Name of the function to use in the displays
243  int currentLine; // Line to use in the displays
244 
245  // if true, output to std::cerr/stderr else std::cout/stdout
246  bool err;
247  // string to display before anything else
248  const char *header;
249 
250 };
251 
252 /* ------------------------------------------------------------------------- */
253 /* --- vpTRACE IN/OUT FONCTION --------------------------------------------- */
254 /* ------------------------------------------------------------------------- */
255 
256 #ifdef VP_TRACE // Activate the trace mode
257 
280 #define vpIN_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "begin "))
281 
304 #define vpOUT_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "end "))
305 
306 #else // #ifdef VP_TRACE
307 
308 inline void vpIN_FCT(const char * /* a */, ...) { }
309 inline void vpOUT_FCT(const char * /* a */, ...) { }
310 
311 #endif // #ifdef VP_TRACE
312 
313 /* -------------------------------------------------------------------------- */
314 /* --- vpTRACE -------------------------------------------------------------- */
315 /* -------------------------------------------------------------------------- */
316 
317 #ifdef VP_TRACE
318 
348 #define vpCTRACE std::cout << "(L0) " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
349 
379 #define vpCERROR std::cerr << "(L0) " << "!!\t" << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
380 
409 #define vpERROR_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, true))
410 
436 #define vpTRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false))
437 
438 #else // #ifdef VP_TRACE
439 
440 #define vpCTRACE \
441  if (false) \
442  std::cout // Warning C4127
443 #define vpCERROR \
444  if (false) \
445  std::cerr // Warning C4127
446 
447 inline void vpERROR_TRACE(const char * /* a */, ...) { }
448 inline void vpERROR_TRACE(int /* level */, const char * /* a */, ...) { }
449 inline void vpTRACE(const char * /* a */, ...) { }
450 inline void vpTRACE(int /* level */, const char * /* a */, ...) { }
451 
452 #endif // #ifdef VP_TRACE
453 
454 /* ------------------------------------------------------------------------- */
455 /* --- VP_DEBUG ------------------------------------------------------------ */
456 /* ------------------------------------------------------------------------- */
457 
458 #ifdef VP_DEBUG
459 
485 #define vpDERROR_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, true))
486 
512 #define vpDEBUG_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false))
513 
540 #define vpCDEBUG(level) \
541  if (VP_DEBUG_MODE < level) \
542  ; \
543  else \
544  std::cout << "(L" << level << ") " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
545 
571 #define vpDEBUG_ENABLE(level) (VP_DEBUG_MODE >= level)
572 
573 #else // #ifdef VP_DEBUG
574 
575 inline void vpDERROR_TRACE(const char * /* a */, ...) { }
576 inline void vpDEBUG_TRACE(const char * /* a */, ...) { }
577 inline void vpDERROR_TRACE(int /* level */, const char * /* a */, ...) { }
578 inline void vpDEBUG_TRACE(int /* level */, const char * /* a */, ...) { }
579 
580 #define vpCDEBUG(level) \
581  if (false) \
582  std::cout // Warning C4127
583 #define vpDEBUG_ENABLE(level) (false) // Warning C4127
584 
585 #endif // #ifdef VP_DEBUG
586 
587 /* -------------------------------------------------------------------------- */
588 /* --- DEFENSIF ------------------------------------------------------------- */
589 /* -------------------------------------------------------------------------- */
590 #ifdef VP_DEFENSIF
591 #define DEFENSIF(a) (a)
592 #else
593 #define DEFENSIF(a) (0)
594 #endif /*#ifdef DEFENSIF*/
595 END_VISP_NAMESPACE
596 #endif /* #ifdef __DEBUG_HH */
This class is used to display debug or error messages.
Definition: vpDebug.h:138
vpTraceOutput(const char *file, int line, const char *func, bool error=false, const char *s=nullptr)
Definition: vpDebug.h:150
void operator()(const char *format,...)
Definition: vpDebug.h:185
void operator()(int level, const char *format,...)
Definition: vpDebug.h:159
void display(const char *format, va_list args)
Definition: vpDebug.h:210