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_AUTO_TEST_CASE(testDebugTrace) {
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;
}
entity.h
dynamicgraph::CustomEntity::getClassName
virtual const std::string & getClassName() const
Definition: command-test.cpp:47
dynamicgraph
dynamicgraph::CustomEntity::~CustomEntity
~CustomEntity()
Definition: command-test.cpp:100
dynamicgraph::DebugTrace::closeFile
static void closeFile(const char *filename=DEBUG_FILENAME_DEFAULT)
Definition: debug.cpp:54
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(command_test)
Definition: command-test.cpp:134
dgDEBUG
#define dgDEBUG(level)
Definition: debug.h:157
dynamicgraph::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity")
dynamicgraph::CustomEntity
Definition: command-test.cpp:36
dynamicgraph::CustomEntity::CustomEntity
CustomEntity(const std::string &n)
Definition: command-test.cpp:48
CustomEntity
Definition: custom-entity.cpp:22
dynamicgraph::CustomEntity::testDebugTrace
void testDebugTrace()
Definition: debug-logger-winit.cpp:47
dynamicgraph::dgDEBUGFLOW
DYNAMIC_GRAPH_DLLAPI DebugTrace dgDEBUGFLOW
dgDEBUGOUT
#define dgDEBUGOUT(level)
Definition: debug.h:204
dgDEBUGIN
#define dgDEBUGIN(level)
VP_DEBUG.
Definition: debug.h:202
dynamicgraph::Entity::Entity
Entity(const std::string &name)
Definition: src/dgraph/entity.cpp:33
exception-factory.h
factory.h
dynamicgraph::CustomEntity::CLASS_NAME
static const std::string CLASS_NAME
Definition: command-test.cpp:38
dynamicgraph::DebugTrace::DEBUG_FILENAME_DEFAULT
static const char * DEBUG_FILENAME_DEFAULT
Definition: debug.h:86
dynamicgraph::DebugTrace::openFile
static void openFile(const char *filename=DEBUG_FILENAME_DEFAULT)
Definition: debug.cpp:48
dgDEBUGINOUT
#define dgDEBUGINOUT(level)
Definition: debug.h:206
debug.h
dynamicgraph::FactoryStorage::newEntity
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
Definition: src/dgraph/factory.cpp:79
pool.h
dynamicgraph::FactoryStorage::getInstance
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
Definition: src/dgraph/factory.cpp:15


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Oct 22 2023 02:27:08