Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
144{
145public:
156 vpTraceOutput(const char *file, int line, const char *func, bool error = false, const char *s = nullptr)
157 : currentFile(file), currentFunc(func), currentLine(line), err(error), header(s)
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 }
176 else {
177 std::cout << "(L" << level << ") ";
178 }
179
180 // calls display with it
181 display(format, args);
182
183 va_end(args);
184 }
185 }
186
191 void operator()(const char *format, ...)
192 {
193 // gets the variable list of arguments
194 va_list args;
195 va_start(args, format);
196
197#ifdef VP_DEBUG
198 std::cout << "(L0) ";
199#endif
200
201 // calls display with it
202 display(format, args);
203
204 va_end(args);
205 }
206
207#if defined(__clang__)
208// Mute warnings:
209// vpDebug.h(221,24): warning : format string is not a string literal [-Wformat-nonliteral]
210// vpDebug.h(234,15): warning : format string is not a string literal [-Wformat-nonliteral]
211# pragma clang diagnostic push
212# pragma clang diagnostic ignored "-Wformat-nonliteral"
213#endif
221 void display(const char *format, va_list args)
222 {
223 // if we want to write to std::cerr/stderr
224 if (err) {
225 // first writes the header if there is one
226 if (header != nullptr) {
227 std::cerr << header;
228 }
229 // then writes the recorded namefile, function and line
230 std::cerr << "!!\t" << currentFile << ": " << currentFunc << "(#" << currentLine << ") : ";
231 // and finally writes the message passed to () operator.
232 vfprintf(stderr, format, args);
233 fprintf(stderr, "\n");
234 // flushes the buffer
235 fflush(stderr);
236 }
237 else {
238 // first writes the header if there is one
239 if (header != nullptr) {
240 std::cout << header;
241 }
242 // then writes the recorded namefile, function and line
243 std::cout << currentFile << ": " << currentFunc << "(#" << currentLine << ") : ";
244 // and finally writes the message passed to () operator.
245 vprintf(format, args);
246 printf("\n");
247 // flushes the buffer
248 fflush(stdout);
249 }
250 }
251#if defined(__clang__)
252# pragma clang diagnostic pop
253#endif
254
255private:
256 const char *currentFile; // Name of the file to use in the displays
257 const char *currentFunc; // Name of the function to use in the displays
258 int currentLine; // Line to use in the displays
259
260 // if true, output to std::cerr/stderr else std::cout/stdout
261 bool err;
262 // string to display before anything else
263 const char *header;
264};
265
266/* ------------------------------------------------------------------------- */
267/* --- vpTRACE IN/OUT FONCTION --------------------------------------------- */
268/* ------------------------------------------------------------------------- */
269
270#ifdef VP_TRACE // Activate the trace mode
271
294#define vpIN_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "begin "))
295
318#define vpOUT_FCT (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false, "end "))
319
320#else // #ifdef VP_TRACE
321
322inline void vpIN_FCT(const char * /* a */, ...) { }
323inline void vpOUT_FCT(const char * /* a */, ...) { }
324
325#endif // #ifdef VP_TRACE
326
327/* -------------------------------------------------------------------------- */
328/* --- vpTRACE -------------------------------------------------------------- */
329/* -------------------------------------------------------------------------- */
330
331#ifdef VP_TRACE
332
362#define vpCTRACE std::cout << "(L0) " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
363
393#define vpCERROR std::cerr << "(L0) " << "!!\t" << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
394
423#define vpERROR_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, true))
424
450#define vpTRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false))
451
452#else // #ifdef VP_TRACE
453
454#define vpCTRACE \
455 if (false) \
456 std::cout // Warning C4127
457#define vpCERROR \
458 if (false) \
459 std::cerr // Warning C4127
460
461inline void vpERROR_TRACE(const char * /* a */, ...) { }
462inline void vpERROR_TRACE(int /* level */, const char * /* a */, ...) { }
463inline void vpTRACE(const char * /* a */, ...) { }
464inline void vpTRACE(int /* level */, const char * /* a */, ...) { }
465
466#endif // #ifdef VP_TRACE
467
468/* ------------------------------------------------------------------------- */
469/* --- VP_DEBUG ------------------------------------------------------------ */
470/* ------------------------------------------------------------------------- */
471
472#ifdef VP_DEBUG
473
499#define vpDERROR_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, true))
500
526#define vpDEBUG_TRACE (vpTraceOutput(__FILE__, __LINE__, __FUNCTION__, false))
527
554#define vpCDEBUG(level) \
555 if (VP_DEBUG_MODE < level) \
556 ; \
557 else \
558 std::cout << "(L" << level << ") " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") : "
559
585#define vpDEBUG_ENABLE(level) (VP_DEBUG_MODE >= level)
586
587#else // #ifdef VP_DEBUG
588
589inline void vpDERROR_TRACE(const char * /* a */, ...) { }
590inline void vpDEBUG_TRACE(const char * /* a */, ...) { }
591inline void vpDERROR_TRACE(int /* level */, const char * /* a */, ...) { }
592inline void vpDEBUG_TRACE(int /* level */, const char * /* a */, ...) { }
593
594#define vpCDEBUG(level) \
595 if (false) \
596 std::cout // Warning C4127
597#define vpDEBUG_ENABLE(level) (false) // Warning C4127
598
599#endif // #ifdef VP_DEBUG
600
601/* -------------------------------------------------------------------------- */
602/* --- DEFENSIF ------------------------------------------------------------- */
603/* -------------------------------------------------------------------------- */
604#ifdef VP_DEFENSIF
605#define DEFENSIF(a) (a)
606#else
607#define DEFENSIF(a) (0)
608#endif /*#ifdef DEFENSIF*/
609END_VISP_NAMESPACE
610#endif /* #ifdef __DEBUG_HH */
vpTraceOutput(const char *file, int line, const char *func, bool error=false, const char *s=nullptr)
Definition vpDebug.h:156
void operator()(const char *format,...)
Definition vpDebug.h:191
void operator()(int level, const char *format,...)
Definition vpDebug.h:165
void display(const char *format, va_list args)
Definition vpDebug.h:221
#define vpIN_FCT
Definition vpDebug.h:294
#define vpTRACE
Definition vpDebug.h:450
#define vpOUT_FCT
Definition vpDebug.h:318
#define vpDEBUG_TRACE
Definition vpDebug.h:526
#define vpDERROR_TRACE
Definition vpDebug.h:499
#define vpERROR_TRACE
Definition vpDebug.h:423