Debugging with macros and level

Introduction

The idea of this class and collection of MACROS is to specify a level for a message, and record the message in a stream according to this level. In addition there are mechanisms allowing that no time is wasted in condition test. You can therefore let the debugging messages inside the code without impact on performances.

Setting up dgDEBUG macros

To allow message display the entity must be compiled with the macro

#define VP_DEBUG 1

Commenting or removing this macro disable all the messages specified by the following macros.

The level up to which the messages are accepted for display is specified by:

#define VP_DEBUG_MODE 50

In the constructor of the entity, the file where all the messages are written is specified by:

dynamicgraph::dgDEBUGFLOW.openFile("/tmp/dynamic-graph-traces.txt");

Using dgDEBUG macros

To print that the beginning of a method is being executed use the following macros:

5 here specifies the minimum level that you be specified by VP_DEBUG_MODE for this message to be displayed.

It will generate the following output:

/path_to/dynamic-graph/tests/debug-trace.cpp: testDebugTrace(#46) :# In {

The output displays the name of the source file, the name of the method, the line where is the macro, and the message itself.

When going out of the method:

This generates the following output:

/path_to/dynamic-graph/tests/debug-trace.cpp: testDebugTrace(#54) :# Out }

A message inside the code is written through:

dgDEBUG(5) << "Here is a test" << std::endl;

This generates the following output:

/path_to/dynamic-graph/tests/debug-trace.cpp: testDebugTrace(#52) :Here is a
test

Working example

A full working example is given here:

/* Copyright 2019, LAAS-CNRS
*
* Olivier Stasse
*
*/
#include <iostream>
#include <sstream>
#define VP_DEBUG 1
#define VP_DEBUG_MODE 50
#define VP_TEMPLATE_DEBUG_MODE 50
#define BOOST_TEST_MODULE debug - trace
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
using boost::test_tools::output_test_stream;
namespace dynamicgraph {
class CustomEntity : public Entity {
public:
static const std::string CLASS_NAME;
virtual const std::string &getClassName() const { return CLASS_NAME; }
explicit CustomEntity(const std::string &n) : Entity(n) {
dynamicgraph::dgDEBUGFLOW.openFile("/tmp/dynamic-graph-traces.txt");
}
dynamicgraph::dgDEBUGFLOW.closeFile("/tmp/dynamic-graph-traces.txt");
}
void testDebugTrace() {
dgDEBUG(5) << "Here is a test" << std::endl;
}
};
} // namespace dynamicgraph
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
(dynamic_cast<dynamicgraph::CustomEntity *>(
"my-entity")));
dynamicgraph::CustomEntity &entity = *ptr_entity;
entity.testDebugTrace();
output_test_stream output;
std::fstream the_debug_file;
std::ios::in);
// Extract the filename and this source file from the output
std::string astr;
std::ostringstream oss_debug_file;
while (std::getline(the_debug_file, astr)) {
std::size_t found = astr.find(":");
std::string asubstr = astr.substr(found + 1, astr.length());
found = asubstr.find(":");
std::string asubstr2 = asubstr.substr(found + 1, astr.length());
oss_debug_file << asubstr2;
}
the_debug_file.close();
// Compare with the strings put inside this source file
std::string str_to_test =
"# In {"
"# In/Out { }"
"Here is a test"
"# Out }";
bool two_sub_string_identical;
// Make comparisons.
two_sub_string_identical = str_to_test == oss_debug_file.str();
BOOST_CHECK(two_sub_string_identical);
delete ptr_entity;
}


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03