Skip to content
Snippets Groups Projects
debug.hh 7.49 KiB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
/*
 * Copyright 2010,
 * François Bleibel,
 * Olivier Stasse,
Thomas Moulard's avatar
Thomas Moulard committed
 * CNRS/AIST
Thomas Moulard's avatar
Thomas Moulard committed
 */
Thomas Moulard's avatar
Thomas Moulard committed
#ifndef SOT_CORE_DEBUG_HH
Guilhem Saurel's avatar
Guilhem Saurel committed
#define SOT_CORE_DEBUG_HH
#include <cstdarg>
#include <cstdio>
#include <fstream>
#include <sstream>

Guilhem Saurel's avatar
Guilhem Saurel committed
#include "sot/core/api.hh"

Guilhem Saurel's avatar
Guilhem Saurel committed
#ifndef VP_DEBUG_MODE
#define VP_DEBUG_MODE 0
Guilhem Saurel's avatar
Guilhem Saurel committed
#endif  //! VP_DEBUG_MODE
Guilhem Saurel's avatar
Guilhem Saurel committed

#ifndef VP_TEMPLATE_DEBUG_MODE
#define VP_TEMPLATE_DEBUG_MODE 0
Guilhem Saurel's avatar
Guilhem Saurel committed
#endif  //! VP_TEMPLATE_DEBUG_MODE

#define SOT_COMMON_TRACES                                       \
  do {                                                          \
    va_list arg;                                                \
    va_start(arg, format);                                      \
    vsnprintf(charbuffer, SIZE, format, arg);                   \
    va_end(arg);                                                \
    outputbuffer << tmpbuffer.str() << charbuffer << std::endl; \
Guilhem Saurel's avatar
Guilhem Saurel committed
  } while (0)

namespace dynamicgraph {
namespace sot {
class SOT_CORE_EXPORT DebugTrace {
Guilhem Saurel's avatar
Guilhem Saurel committed
 public:
Guilhem Saurel's avatar
Guilhem Saurel committed
  static const int SIZE = 512;

  std::stringstream tmpbuffer;
  std::ostream &outputbuffer;
  char charbuffer[SIZE + 1];
  int traceLevel;
  int traceLevelTemplate;

  DebugTrace(std::ostream &os) : outputbuffer(os) {}

  inline void trace(const int level, const char *format, ...) {
Guilhem Saurel's avatar
Guilhem Saurel committed
    if (level <= traceLevel) SOT_COMMON_TRACES;
Guilhem Saurel's avatar
Guilhem Saurel committed
    tmpbuffer.str("");
  }

  inline void trace(const char *format, ...) {
    SOT_COMMON_TRACES;
    tmpbuffer.str("");
  }

  inline void trace(const int level = -1) {
Guilhem Saurel's avatar
Guilhem Saurel committed
    if (level <= traceLevel) outputbuffer << tmpbuffer.str();
Guilhem Saurel's avatar
Guilhem Saurel committed
    tmpbuffer.str("");
  }

  inline void traceTemplate(const int level, const char *format, ...) {
Guilhem Saurel's avatar
Guilhem Saurel committed
    if (level <= traceLevelTemplate) SOT_COMMON_TRACES;
Guilhem Saurel's avatar
Guilhem Saurel committed
    tmpbuffer.str("");
  }

  inline void traceTemplate(const char *format, ...) {
    SOT_COMMON_TRACES;
    tmpbuffer.str("");
  }

  inline DebugTrace &pre(const std::ostream &) { return *this; }

  inline DebugTrace &pre(const std::ostream &, int level) {
    traceLevel = level;
    return *this;
  }

  static const char *DEBUG_FILENAME_DEFAULT;
  static void openFile(const char *filename = DEBUG_FILENAME_DEFAULT);
  static void closeFile(const char *filename = DEBUG_FILENAME_DEFAULT);
};

SOT_CORE_EXPORT extern DebugTrace sotDEBUGFLOW;
SOT_CORE_EXPORT extern DebugTrace sotERRORFLOW;
Guilhem Saurel's avatar
Guilhem Saurel committed
}  // namespace sot
}  // namespace dynamicgraph
Thomas Moulard's avatar
Thomas Moulard committed

Guilhem Saurel's avatar
Guilhem Saurel committed
#ifdef VP_DEBUG
Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotPREDEBUG \
Guilhem Saurel's avatar
Guilhem Saurel committed
  __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"

Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotPREERROR \
Guilhem Saurel's avatar
Guilhem Saurel committed
  "\t!! " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"

Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotDEBUG(level)                                       \
  if ((level > VP_DEBUG_MODE) ||                              \
      (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good())) \
    ;                                                         \
  else                                                        \
Guilhem Saurel's avatar
Guilhem Saurel committed
    dynamicgraph::sot::sotDEBUGFLOW.outputbuffer << sotPREDEBUG

Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotDEBUGMUTE(level)                                   \
  if ((level > VP_DEBUG_MODE) ||                              \
      (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good())) \
    ;                                                         \
  else                                                        \
Guilhem Saurel's avatar
Guilhem Saurel committed
    dynamicgraph::sot::sotDEBUGFLOW.outputbuffer

Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotERROR                                            \
  if (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good()) \
    ;                                                       \
  else                                                      \
Guilhem Saurel's avatar
Guilhem Saurel committed
    dynamicgraph::sot::sotERRORFLOW.outputbuffer << sotPREERROR

Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotDEBUGF                                                      \
  if (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good())            \
    ;                                                                  \
  else                                                                 \
    dynamicgraph::sot::sotDEBUGFLOW                                    \
        .pre(dynamicgraph::sot::sotDEBUGFLOW.tmpbuffer << sotPREDEBUG, \
             VP_DEBUG_MODE)                                            \
Guilhem Saurel's avatar
Guilhem Saurel committed
        .trace

Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotERRORF                                           \
  if (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good()) \
    ;                                                       \
  else                                                      \
Guilhem Saurel's avatar
Guilhem Saurel committed
    sot::sotERRORFLOW.pre(sot::sotERRORFLOW.tmpbuffer << sotPREERROR).trace
Thomas Moulard's avatar
Thomas Moulard committed

Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotTDEBUG(level)                                      \
  if ((level > VP_TEMPLATE_DEBUG_MODE) ||                     \
      (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good())) \
    ;                                                         \
  else                                                        \
Guilhem Saurel's avatar
Guilhem Saurel committed
    dynamicgraph::sot::sotDEBUGFLOW.outputbuffer << sotPREDEBUG

Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotTDEBUGF                                                     \
  if (!dynamicgraph::sot::sotDEBUGFLOW.outputbuffer.good())            \
    ;                                                                  \
  else                                                                 \
    dynamicgraph::sot::sotDEBUGFLOW                                    \
        .pre(dynamicgraph::sot::sotDEBUGFLOW.tmpbuffer << sotPREDEBUG, \
             VP_TEMPLATE_DEBUG_MODE)                                   \
Guilhem Saurel's avatar
Guilhem Saurel committed
        .trace

namespace dynamicgraph {
Guilhem Saurel's avatar
Guilhem Saurel committed
namespace sot {
inline bool sotDEBUG_ENABLE(const int &level) { return level <= VP_DEBUG_MODE; }

inline bool sotTDEBUG_ENABLE(const int &level) {
  return level <= VP_TEMPLATE_DEBUG_MODE;
}
Guilhem Saurel's avatar
Guilhem Saurel committed
}  // namespace sot
}  // namespace dynamicgraph
Guilhem Saurel's avatar
Guilhem Saurel committed
/* -------------------------------------------------------------------------- */
Guilhem Saurel's avatar
Guilhem Saurel committed
#else  // VP_DEBUG
#define sotPREERROR \
Guilhem Saurel's avatar
Guilhem Saurel committed
  "\t!! " << __FILE__ << ": " << __FUNCTION__ << "(#" << __LINE__ << ") :"
Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotDEBUG(level) \
  if (1)                \
    ;                   \
  else                  \
Guilhem Saurel's avatar
Guilhem Saurel committed
    ::dynamicgraph::sot::__null_stream()
Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotDEBUGMUTE(level) \
  if (1)                    \
    ;                       \
  else                      \
Guilhem Saurel's avatar
Guilhem Saurel committed
    ::dynamicgraph::sot::__null_stream()
#define sotERROR sotERRORFLOW.outputbuffer << sotPREERROR
namespace dynamicgraph {
Guilhem Saurel's avatar
Guilhem Saurel committed
namespace sot {
inline void sotDEBUGF(const int, const char *, ...) {}
inline void sotDEBUGF(const char *, ...) {}
inline void sotERRORF(const int, const char *, ...) {}
inline void sotERRORF(const char *, ...) {}
inline std::ostream &__null_stream() {
  // This function should never be called. With -O3,
  // it should not appear in the generated binary.
  static std::ostream os(NULL);
  return os;
}
Guilhem Saurel's avatar
Guilhem Saurel committed
}  // namespace sot
}  // namespace dynamicgraph
Guilhem Saurel's avatar
Guilhem Saurel committed
// TEMPLATE
Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotTDEBUG(level) \
  if (1)                 \
    ;                    \
  else                   \
Guilhem Saurel's avatar
Guilhem Saurel committed
    ::dynamicgraph::sot::__null_stream()
namespace dynamicgraph {
Guilhem Saurel's avatar
Guilhem Saurel committed
namespace sot {
inline void sotTDEBUGF(const int, const char *, ...) {}
inline void sotTDEBUGF(const char *, ...) {}
Guilhem Saurel's avatar
Guilhem Saurel committed
}  // namespace sot
}  // namespace dynamicgraph
Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotDEBUG_ENABLE(level) false
#define sotTDEBUG_ENABLE(level) false
Thomas Moulard's avatar
Thomas Moulard committed

Guilhem Saurel's avatar
Guilhem Saurel committed
#endif  // VP_DEBUG
Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotDEBUGIN(level) sotDEBUG(level) << "# In {" << std::endl
#define sotDEBUGOUT(level) sotDEBUG(level) << "# Out }" << std::endl
#define sotDEBUGINOUT(level) sotDEBUG(level) << "# In/Out { }" << std::endl
Guilhem Saurel's avatar
Guilhem Saurel committed
#define sotTDEBUGIN(level) sotTDEBUG(level) << "# In {" << std::endl
#define sotTDEBUGOUT(level) sotTDEBUG(level) << "# Out }" << std::endl
#define sotTDEBUGINOUT(level) sotTDEBUG(level) << "# In/Out { }" << std::endl
Guilhem Saurel's avatar
Guilhem Saurel committed
#endif  //! #ifdef SOT_CORE_DEBUG_HH
// Local variables:
// c-basic-offset: 2
// End: