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
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:
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:
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <iostream>
#include <sstream>
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#define VP_DEBUG 1
#define VP_DEBUG_MODE 50
#define VP_TEMPLATE_DEBUG_MODE 50
#include <dynamic-graph/debug.h>
#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;
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");
}
~CustomEntity() {
dynamicgraph::dgDEBUGFLOW.closeFile("/tmp/dynamic-graph-traces.txt");
}
void testDebugTrace() {
dgDEBUGIN(5);
dgDEBUGINOUT(5);
dgDEBUG(5) << "Here is a test" << std::endl;
dgDEBUGOUT(5);
}
};
}
BOOST_AUTO_TEST_CASE(testDebugTrace) {
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
dynamicgraph::CustomEntity *ptr_entity =
(dynamic_cast<dynamicgraph::CustomEntity *>(
"my-entity")));
dynamicgraph::CustomEntity &entity = *ptr_entity;
entity.testDebugTrace();
output_test_stream output;
std::fstream the_debug_file;
the_debug_file.open(dynamicgraph::DebugTrace::DEBUG_FILENAME_DEFAULT,
std::ios::in);
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();
std::string str_to_test =
"# In {"
"# In/Out { }"
"Here is a test"
"# Out }";
bool two_sub_string_identical;
two_sub_string_identical = str_to_test == oss_debug_file.str();
BOOST_CHECK(two_sub_string_identical);
delete ptr_entity;
}