Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cberge/dynamic-graph
  • ostasse/dynamic-graph
  • gsaurel/dynamic-graph
  • stack-of-tasks/dynamic-graph
4 results
Show changes
Showing
with 444 additions and 508 deletions
......@@ -13,21 +13,21 @@
namespace dynamicgraph {
RealTimeLogger::RealTimeLogger(const std::size_t &bufferSize)
: buffer_(bufferSize, NULL), frontIdx_(0), backIdx_(0), oss_(NULL),
: buffer_(bufferSize, NULL),
frontIdx_(0),
backIdx_(0),
oss_(NULL),
nbDiscarded_(0) {
for (std::size_t i = 0; i < buffer_.size(); ++i)
buffer_[i] = new Data;
for (std::size_t i = 0; i < buffer_.size(); ++i) buffer_[i] = new Data;
}
RealTimeLogger::~RealTimeLogger() {
// Check that we are not spinning...
for (std::size_t i = 0; i < buffer_.size(); ++i)
delete buffer_[i];
for (std::size_t i = 0; i < buffer_.size(); ++i) delete buffer_[i];
}
bool RealTimeLogger::spinOnce() {
if (empty())
return false;
if (empty()) return false;
Data *data = buffer_[frontIdx_];
frontIdx_ = (frontIdx_ + 1) % buffer_.size();
std::string str = data->buf.str();
......@@ -59,12 +59,6 @@ RTLoggerStream RealTimeLogger::front() {
return RTLoggerStream(this, oss_);
}
RTLoggerStream::~RTLoggerStream() {
os_ << std::ends;
if (logger_ != NULL)
logger_->frontReady();
}
struct RealTimeLogger::thread {
bool requestShutdown_;
int threadPolicy_;
......@@ -72,22 +66,25 @@ struct RealTimeLogger::thread {
bool changedThreadParams;
boost::thread t_;
thread(RealTimeLogger *logger)
: requestShutdown_(false), threadPolicy_(SCHED_OTHER), threadPriority_(0),
changedThreadParams(true), t_(&thread::spin, this, logger) {}
explicit thread(RealTimeLogger *logger)
: requestShutdown_(false),
threadPolicy_(SCHED_OTHER),
threadPriority_(0),
changedThreadParams(true),
t_(&thread::spin, this, logger) {}
void setThreadPolicy(int policy) {
threadPolicy_ = policy;
changedThreadParams = true;
}
// void setThreadPolicy(int policy) {
// threadPolicy_ = policy;
// changedThreadParams = true;
// }
void setPriority(int priority) {
threadPriority_ = priority;
changedThreadParams = true;
}
// void setPriority(int priority) {
// threadPriority_ = priority;
// changedThreadParams = true;
// }
int getThreadPolicy() { return threadPolicy_; }
int getThreadPriority() { return threadPriority_; }
// int getThreadPolicy() { return threadPolicy_; }
// int getThreadPriority() { return threadPriority_; }
void changeThreadParams() {
int threadPolicy;
......@@ -113,8 +110,7 @@ struct RealTimeLogger::thread {
// Do a pause
if (!logger->spinOnce())
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
if (changedThreadParams)
changeThreadParams();
if (changedThreadParams) changeThreadParams();
}
}
};
......@@ -131,11 +127,10 @@ RealTimeLogger &RealTimeLogger::instance() {
}
void RealTimeLogger::destroy() {
if (instance_ == NULL)
return;
if (instance_ == NULL) return;
thread_->requestShutdown_ = true;
thread_->t_.join();
delete instance_;
delete thread_;
}
} // namespace dynamicgraph
} // namespace dynamicgraph
......@@ -14,9 +14,10 @@
#include <dynamic-graph/pool.h>
/*! System includes */
#include <sstream>
#include <stdlib.h>
#include <sstream>
using namespace std;
using namespace dynamicgraph;
using dynamicgraph::command::Command;
......@@ -46,7 +47,7 @@ Entity::Entity(const string &name__) : name(name__) {
Entity::~Entity() {
dgDEBUG(25) << "# In (" << name << " { " << endl;
for (std::map<const std::string, Command *>::iterator it = commandMap.begin();
it != commandMap.end(); it++) {
it != commandMap.end(); ++it) {
delete it->second;
}
dgDEBUGOUT(25);
......@@ -68,7 +69,7 @@ void Entity::signalRegistration(const SignalArray<int> &signals) {
const string &signame(buffer);
SignalMap::iterator sigkey = signalMap.find(signame);
if (sigkey != signalMap.end()) // key does exist
if (sigkey != signalMap.end()) // key does exist
{
dgERRORF("Key %s already exist in the signalMap.", signame.c_str());
if (sigkey->second != &sig) {
......@@ -87,7 +88,7 @@ void Entity::signalRegistration(const SignalArray<int> &signals) {
void Entity::signalDeregistration(const std::string &signame) {
SignalMap::iterator sigkey = signalMap.find(signame);
if (sigkey == signalMap.end()) // key does not exist
if (sigkey == signalMap.end()) // key does not exist
{
dgERRORF("Key %s does not exist in the signalMap.", signame.c_str());
throw ExceptionFactory(ExceptionFactory::UNREFERED_SIGNAL,
......@@ -105,14 +106,14 @@ std::string Entity::getDocString() const {
return docString;
}
#define __DG_ENTITY_GET_SIGNAL__(ITER_TYPE) \
SignalMap::ITER_TYPE sigkey = signalMap.find(signame); \
if (sigkey == signalMap.end()) /* key does NOT exist */ \
{ \
throw ExceptionFactory(ExceptionFactory::UNREFERED_SIGNAL, \
"The requested signal is not registered", ": %s", \
signame.c_str()); \
} \
#define __DG_ENTITY_GET_SIGNAL__(ITER_TYPE) \
SignalMap::ITER_TYPE sigkey = signalMap.find(signame); \
if (sigkey == signalMap.end()) /* key does NOT exist */ \
{ \
throw ExceptionFactory(ExceptionFactory::UNREFERED_SIGNAL, \
"The requested signal is not registered", ": %s", \
signame.c_str()); \
} \
return *(sigkey->second);
bool Entity::hasSignal(const string &signame) const {
......@@ -185,9 +186,9 @@ const std::string &Entity::getCommandList() const {
/// Add a command to Entity
void Entity::addCommand(const std::string &inName, Command *command) {
if (commandMap.count(inName) != 0) {
DG_THROW ExceptionFactory(ExceptionFactory::OBJECT_CONFLICT,
"Command " + inName +
" already registered in Entity.");
DG_THROW ExceptionFactory(
ExceptionFactory::OBJECT_CONFLICT,
"Command " + inName + " already registered in Entity.");
}
std::pair<const std::string, Command *> item(inName, command);
commandMap.insert(item);
......@@ -200,14 +201,14 @@ std::map<const std::string, Command *> Entity::getNewStyleCommandMap() {
Command *Entity::getNewStyleCommand(const std::string &commandName) {
if (commandMap.count(commandName) != 1) {
DG_THROW ExceptionFactory(ExceptionFactory::UNREFERED_FUNCTION,
"Command <" + commandName +
"> is not registered in Entity.");
DG_THROW ExceptionFactory(
ExceptionFactory::UNREFERED_FUNCTION,
"Command <" + commandName + "> is not registered in Entity.");
}
return commandMap[commandName];
}
void Entity::sendMsg(const std::string &msg, MsgType t, const char *file,
int line) {
logger_.sendMsg("[" + name + "]" + msg, t, file, line);
void Entity::sendMsg(const std::string &msg, MsgType t,
const std::string &lineId) {
logger_.stream(t, lineId) << "[" << name << "]" << msg << '\n';
}
......@@ -2,10 +2,11 @@
// JRL, CNRS/AIST.
//
#include "dynamic-graph/factory.h"
#include <boost/foreach.hpp>
#include "dynamic-graph/debug.h"
#include "dynamic-graph/factory.h"
using namespace std;
using namespace dynamicgraph;
......@@ -39,9 +40,10 @@ void FactoryStorage::registerEntity(const std::string &entname,
"Another entity class already defined with the same name. ",
"(while adding entity class <%s> inside the factory).",
entname.c_str());
dgERRORF("Another entity class already defined with the same name. "
"(while adding entity class <%s> inside the factory).",
entname.c_str());
dgERRORF(
"Another entity class already defined with the same name. "
"(while adding entity class <%s> inside the factory).",
entname.c_str());
} else {
if (!ent) {
// FIXME: we should have a better error code for that.
......@@ -118,4 +120,4 @@ EntityRegisterer::~EntityRegisterer() {
// The global factory.
FactoryStorage *FactoryStorage::instance_ = NULL;
} // end of namespace dynamicgraph.
} // end of namespace dynamicgraph.
......@@ -13,13 +13,15 @@
/* --- DYNAMIC-GRAPH --- */
#include "dynamic-graph/pool.h"
#include "dynamic-graph/debug.h"
#include "dynamic-graph/entity.h"
#include <list>
#include <sstream>
#include <string>
#include <typeinfo>
#include "dynamic-graph/debug.h"
#include "dynamic-graph/entity.h"
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
......@@ -57,7 +59,7 @@ PoolStorage::~PoolStorage() {
/* --------------------------------------------------------------------- */
void PoolStorage::registerEntity(const std::string &entname, Entity *ent) {
Entities::iterator entkey = entityMap.find(entname);
if (entkey != entityMap.end()) // key does exist
if (entkey != entityMap.end()) // key does exist
{
throw ExceptionFactory(
ExceptionFactory::OBJECT_CONFLICT,
......@@ -72,7 +74,7 @@ void PoolStorage::registerEntity(const std::string &entname, Entity *ent) {
void PoolStorage::deregisterEntity(const std::string &entname) {
Entities::iterator entkey = entityMap.find(entname);
if (entkey == entityMap.end()) // key doesnot exist
if (entkey == entityMap.end()) // key doesnot exist
{
throw ExceptionFactory(ExceptionFactory::OBJECT_CONFLICT,
"Entity not defined yet. ", "Entity name is <%s>.",
......@@ -207,7 +209,7 @@ static bool objectNameParser(std::istringstream &cmdparse, std::string &objName,
char buffer[SIZE];
cmdparse >> std::ws;
cmdparse.getline(buffer, SIZE, '.');
if (!cmdparse.good()) // The callback is not an object method
if (!cmdparse.good()) // The callback is not an object method
return false;
objName = buffer;
......
......@@ -2,10 +2,11 @@
// JRL, CNRS/AIST.
//
#include <cstring>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/exception-abstract.h>
#include <cstring>
namespace dynamicgraph {
const std::string ExceptionAbstract::EXCEPTION_NAME = "Abstract";
......@@ -23,8 +24,7 @@ const std::string &ExceptionAbstract::getStringMessage() const {
int ExceptionAbstract::getCode() const { return this->code; }
ExceptionAbstract::Param &ExceptionAbstract::Param::initCopy(const Param &p) {
if (&p == this)
return *this;
if (&p == this) return *this;
dgDEBUGIN(25);
if (p.pointersSet) {
......@@ -53,9 +53,9 @@ std::ostream &operator<<(std::ostream &os, const ExceptionAbstract &error) {
if (error.p.set)
os << "Thrown from " << error.p.file << ": " << error.p.function << " (#"
<< error.p.line << ")" << std::endl;
#endif // DYNAMICGRAPH_EXCEPTION_PASSING_PARAM
#endif // DYNAMICGRAPH_EXCEPTION_PASSING_PARAM
return os;
}
} // end of namespace dynamicgraph.
} // end of namespace dynamicgraph.
......@@ -7,11 +7,12 @@
*
*/
#include <cstdio>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/exception-factory.h>
#include <stdarg.h>
#include <cstdio>
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
......
......@@ -7,10 +7,11 @@
*
*/
#include <cstdio>
#include <dynamic-graph/exception-signal.h>
#include <stdarg.h>
#include <cstdio>
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
......
......@@ -7,10 +7,11 @@
*
*/
#include <cstdio>
#include <dynamic-graph/exception-traces.h>
#include <stdarg.h>
#include <cstdio>
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
......
......@@ -2,17 +2,24 @@
* Author: O. Stasse, 2019
* See LICENSE file in the root directory of this repository.
*/
#include <dynamic-graph/process-list.hh>
#include <fstream>
#include <sstream>
#include <string>
#include <dynamic-graph/process-list.hh>
using namespace dynamicgraph::CPU;
CPUData::CPUData()
: user_mode_time_(0), nice_time_(0), system_time_(0), idle_time_(0),
iowait_time_(0), irq_time_(0), softirq_time_(0), steal_time_(0),
guest_time_(0), guest_nice_time_(0), percent_(0.0) {}
: user_mode_time_(0),
nice_time_(0),
system_time_(0),
idle_time_(0),
iowait_time_(0),
irq_time_(0),
softirq_time_(0),
steal_time_(0),
guest_time_(0),
guest_nice_time_(0),
percent_(0.0) {}
void CPUData::ProcessLine(std::istringstream &aCPULine) {
unsigned long long int luser_mode_time = 0, lnice_time = 0, lsystem_time = 0,
......@@ -129,8 +136,7 @@ void System::readProcStat() {
// If we did not initialize
if (!init_) {
// Count the number of CPU.
if (lcpunb > cpuNb_)
cpuNb_ = lcpunb;
if (lcpunb > cpuNb_) cpuNb_ = lcpunb;
} else
// Otherwise process the line.
ProcessCPULine(lcpunb, anISSLine);
......@@ -141,8 +147,8 @@ void System::readProcStat() {
if (!init_) {
/// The number of CPU has been detected by going through /proc/stat.
vCPUData_.resize(cpuNb_ + 1);
for (int i = 0; i < (int)cpuNb_; i++)
vCPUData_[i].cpu_id_ = i;
for (unsigned long i = 0; i < (unsigned long)cpuNb_; i++)
vCPUData_[i].cpu_id_ = (int)i;
}
aif.close();
}
// -*- c++-mode -*-
// Copyright 2010 François Bleibel
// Thomas Moulard,
// Olivier Stasse,
// Nicolas Mansard
//
#include <algorithm>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/lambda/bind.hpp>
#include <dynamic-graph/dynamic-graph-api.h>
#include <dynamic-graph/exception-signal.h>
#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/signal-cast-helper.h>
#include <dynamic-graph/signal-caster.h>
#include <exception>
#include <sstream>
#include <string>
namespace dynamicgraph {
// Define a custom implementation of the DefaultCastRegisterer
// to workaround the limitations of the stream based approach.
// When dealing with double: displaying a double on a stream
// is *NOT* the opposite of reading a double from a stream.
//
// In practice, it means that there is no way to read
// a NaN, +inf, -inf from a stream!
//
// To workaround this problem, parse special values manually
// (the strings used are the one produces by displaying special
// values on a stream).
template <>
inline boost::any DefaultCastRegisterer<double>::cast(std::istringstream &iss) {
std::string tmp;
iss >> tmp;
if (tmp == "nan")
return std::numeric_limits<double>::quiet_NaN();
else if (tmp == "inf" || tmp == "+inf")
return std::numeric_limits<double>::infinity();
else if (tmp == "-inf")
return -1. * std::numeric_limits<double>::infinity();
try {
return boost::lexical_cast<double>(tmp);
} catch (boost::bad_lexical_cast &) {
boost::format fmt("failed to serialize %s (to double)");
fmt % tmp;
throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
}
}
/* Specialize Matrix and Vector traces. */
template <>
void DefaultCastRegisterer<dynamicgraph::Vector>::trace(
const boost::any &object, std::ostream &os) {
const dynamicgraph::Vector &v = boost::any_cast<dynamicgraph::Vector>(object);
for (int i = 0; i < v.size(); ++i) {
os << "\t" << v(i);
}
}
template <>
void DefaultCastRegisterer<dynamicgraph::Matrix>::trace(
const boost::any &object, std::ostream &os) {
const dynamicgraph::Matrix &m = boost::any_cast<dynamicgraph::Matrix>(object);
for (int i = 0; i < m.rows(); ++i)
for (int j = 0; j < m.cols(); ++j) {
os << "\t" << m(i, j);
}
}
// for std::string, do not check failure. If input stream contains an
// empty string, iss.fail() returns true and an exception is thrown
template <>
inline boost::any
DefaultCastRegisterer<std::string>::cast(std::istringstream &iss) {
std::string inst(iss.str());
return inst;
}
// for std::string, do not add std::endl at the end of the stream.
template <>
inline void DefaultCastRegisterer<std::string>::disp(const boost::any &object,
std::ostream &os) {
os << boost::any_cast<std::string>(object);
}
/// Registers useful casts
namespace {
DefaultCastRegisterer<double> double_reg;
DefaultCastRegisterer<int> int_reg;
DefaultCastRegisterer<unsigned int> uint_reg;
DefaultCastRegisterer<bool> bool_reg;
DefaultCastRegisterer<dynamicgraph::Vector> vectorCastRegisterer;
DefaultCastRegisterer<dynamicgraph::Matrix> matrixCastRegisterer;
DefaultCastRegisterer<boost::posix_time::ptime> ptimeCastRegisterer;
DefaultCastRegisterer<std::string> stringCastRegisterer;
} // end of anonymous namespace.
} // namespace dynamicgraph
// -*- c++-mode -*-
// Copyright 2010 François Bleibel
// Thomas Moulard,
// Olivier Stasse
//
#include <algorithm>
#include <boost/lambda/bind.hpp>
#include <dynamic-graph/dynamic-graph-api.h>
#include <dynamic-graph/exception-signal.h>
#include <dynamic-graph/signal-caster.h>
#include <exception>
#include <sstream>
#include <string>
#include <dynamic-graph/linear-algebra.h>
namespace dynamicgraph {
SignalCaster::SignalCaster() {}
SignalCaster::~SignalCaster() {}
void SignalCaster::destroy() {
delete instance_;
instance_ = 0;
}
void SignalCaster::registerCast(const std::type_info &type,
SignalCaster::displayer_type displayer,
SignalCaster::caster_type caster,
SignalCaster::tracer_type tracer) {
if (existsCast(type)) {
// If type name has already been registered for same type, do not throw.
if (type_info_[type.name()] != &type) {
std::string typeName(type.name());
std::ostringstream os;
os << "cast already registered for typename " << typeName << "\n"
<< "and types differ: " << &type << " != " << type_info_[type.name()]
<< ".\n"
<< "A possible reason is that the dynamic"
<< " library defining this type\n"
<< "has been loaded several times, defining different symbols"
<< " for the same type.";
throw ExceptionSignal(ExceptionSignal::GENERIC, os.str());
}
}
functions_[type.name()] = cast_functions_type(displayer, caster, tracer);
type_info_[type.name()] = &type;
}
void SignalCaster::unregisterCast(const std::type_info &type) {
size_t n = functions_.erase(type.name());
if (0 == n) // erase did not find element
// TODO: throw Cast not registered exception
throw ExceptionSignal(ExceptionSignal::GENERIC);
}
bool SignalCaster::existsCast(const std::type_info &type) const {
return functions_.find(type.name()) != functions_.end();
}
SignalCaster::cast_functions_type &
SignalCaster::getCast(const std::string &type_name) {
std::map<std::string, cast_functions_type>::iterator it =
functions_.find(type_name);
if (it == functions_.end())
// TODO: throw "cast not registered" exception
throw ExceptionSignal(ExceptionSignal::BAD_CAST, "Cast not registered");
return it->second;
}
void SignalCaster::disp(const boost::any &object, std::ostream &os) {
getCast(object.type().name()).get<0>()(object, os);
}
void SignalCaster::trace(const boost::any &object, std::ostream &os) {
getCast(object.type().name()).get<2>()(object, os);
}
std::vector<std::string> SignalCaster::listTypenames() const {
std::vector<std::string> typeList;
for (std::map<std::string, cast_functions_type>::const_iterator iter =
functions_.begin();
iter != functions_.end(); iter++)
typeList.push_back(iter->first);
return typeList;
}
boost::any SignalCaster::cast(const std::type_info &type,
std::istringstream &iss) {
return getCast(type.name()).get<1>()(iss);
}
/// Singleton on the library-wide instance of SignalCaster
SignalCaster *SignalCaster::getInstance(void) {
if (instance_ == 0) {
instance_ = new SignalCaster;
}
return instance_;
}
SignalCaster *SignalCaster::instance_ = 0;
} // namespace dynamicgraph
......@@ -12,15 +12,15 @@
/* --------------------------------------------------------------------- */
/* DG */
#include <boost/bind.hpp>
#include <iomanip>
#include <dynamic-graph/all-commands.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/tracer-real-time.h>
#include <boost/bind.hpp>
#include <iomanip>
using namespace std;
using namespace dynamicgraph;
......@@ -103,7 +103,7 @@ TracerRealTime::TracerRealTime(const std::string &n)
addCommand("setBufferSize",
makeDirectSetter(*this, &bufferSize,
docDirectSetter("bufferSize", "int")));
} // using namespace command
} // using namespace command
dgDEBUGOUT(15);
}
......@@ -125,12 +125,18 @@ void TracerRealTime::openFile(const SignalBase<int> &sig,
string filename = rootdir + basename + signame + suffix;
dgDEBUG(5) << "Sig <" << sig.getName() << ">: new file " << filename << endl;
std::ofstream *newfile = new std::ofstream(filename.c_str());
if (!newfile->good()) {
delete newfile;
DG_THROW ExceptionTraces(
ExceptionTraces::NOT_OPEN,
"Could not open file " + filename + " for signal " + signame, "");
}
dgDEBUG(5) << "Newfile:" << (void *)newfile << endl;
hardFiles.push_back(newfile);
dgDEBUG(5) << "Creating Outstringstream" << endl;
// std::stringstream * newbuffer = new std::stringstream ();
OutStringStream *newbuffer = new OutStringStream(); // std::stringstream ();
OutStringStream *newbuffer = new OutStringStream(); // std::stringstream ();
newbuffer->resize(bufferSize);
newbuffer->givenname = givenname;
files.push_back(newbuffer);
......@@ -140,6 +146,7 @@ void TracerRealTime::openFile(const SignalBase<int> &sig,
void TracerRealTime::closeFiles() {
dgDEBUGIN(15);
std::lock_guard<std::mutex> files_lock(files_mtx);
FileList::iterator iter = files.begin();
HardFileList::iterator hardIter = hardFiles.begin();
......@@ -180,7 +187,7 @@ void TracerRealTime::trace() {
"");
}
// std::stringstream & file = * dynamic_cast< stringstream* >(os);
OutStringStream *file = dynamic_cast<OutStringStream *>(os); // segfault
OutStringStream *file = dynamic_cast<OutStringStream *>(os); // segfault
if (NULL == file) {
DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
"The buffer is not open", "");
......@@ -193,29 +200,9 @@ void TracerRealTime::trace() {
}
if ((hardFile.good()) && (NULL != file)) {
// const unsigned int SIZE = 1024*8;
// char buffer[SIZE];
// streambuf * pbuf = file.rdbuf ();
// pbuf->pubseekpos(0);
// const unsigned int NB_BYTE = pbuf->in_avail ();
// dgDEBUG(35) << "Bytes in buffer: " << NB_BYTE << endl;
// //dgDEBUG(35) << "Copie" <<endl<<file.str ()<< endl;
// for( unsigned int index=0;index<NB_BYTE;index+=SIZE )
// {
// pbuf->pubseekpos( index );
// int nget = pbuf->sgetn( buffer,SIZE );
// dgDEBUG(35) << "Copie ["<<nget<<"] " <<buffer<<endl;
// hardFile.write( buffer,nget );
// }
// hardFile << file.str () << flush;
// file.seekp(0);
file->dump(hardFile);
file->empty();
hardFile.flush();
// file.str("");
}
++iter;
......@@ -241,13 +228,6 @@ void TracerRealTime::emptyBuffers() {
dgDEBUGOUT(15);
}
// void TracerRealTime::
// emptyBuffer( std::stringstream & file )
// {
// streambuf * pbuf = file.rdbuf ();
// pbuf->file.rdbuf () ->pubsetbuf( fileBuffer,10 );
// }
void TracerRealTime::recordSignal(std::ostream &os,
const SignalBase<int> &sig) {
dgDEBUGIN(15);
......@@ -264,7 +244,7 @@ void TracerRealTime::recordSignal(std::ostream &os,
<< "> " << endl;
} catch (ExceptionAbstract &exc) {
throw exc;
throw;
} catch (...) {
DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
"The buffer is not open", "");
......@@ -289,8 +269,7 @@ void TracerRealTime::display(std::ostream &os) const {
dgDEBUG(35) << "Next" << endl;
const OutStringStream *file = dynamic_cast<OutStringStream *>(*iterFile);
os << " -> " << (*iter)->getName();
if (file->givenname.length())
os << " (in " << file->givenname << ")";
if (file->givenname.length()) os << " (in " << file->givenname << ")";
os << "\t";
if (file) {
const std::streamsize PRECISION = os.precision();
......@@ -312,12 +291,11 @@ void TracerRealTime::display(std::ostream &os) const {
<< (((double)SIZE + 0.0) / (1 << dec)) << unit << "/"
<< std::setprecision(2) << (((double)MSIZE + 0.0) / (1 << dec)) << unit
<< "]\t";
if (file->full)
os << "(FULL)";
if (file->full) os << "(FULL)";
os.precision(PRECISION);
}
os << endl;
iterFile++;
++iterFile;
}
}
......
......@@ -12,7 +12,6 @@
/* --------------------------------------------------------------------- */
/* DG */
#include <boost/bind.hpp>
#include <dynamic-graph/all-commands.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/factory.h>
......@@ -20,6 +19,8 @@
#include <dynamic-graph/tracer.h>
#include <dynamic-graph/value.h>
#include <boost/bind.hpp>
using namespace std;
using namespace dynamicgraph;
using namespace dynamicgraph::command;
......@@ -31,9 +32,18 @@ DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(Tracer, "Tracer");
/* --------------------------------------------------------------------- */
Tracer::Tracer(const std::string n)
: Entity(n), toTraceSignals(), traceStyle(TRACE_STYLE_DEFAULT),
frequency(1), basename(), suffix(".dat"), rootdir(), namesSet(false),
files(), names(), play(false), timeStart(0),
: Entity(n),
toTraceSignals(),
traceStyle(TRACE_STYLE_DEFAULT),
frequency(1),
basename(),
suffix(".dat"),
rootdir(),
namesSet(false),
files(),
names(),
play(false),
timeStart(0),
triger(boost::bind(&Tracer::recordTrigger, this, _1, _2), sotNOSIGNAL,
"Tracer(" + n + ")::triger") {
signalRegistration(triger);
......@@ -62,8 +72,9 @@ Tracer::Tracer(const std::string n)
doc = docCommandVoid0("Close all the open files.");
addCommand("close", makeCommandVoid0(*this, &Tracer::closeFiles, doc));
doc = docCommandVoid0("If necessary, dump "
"(can be done automatically for some traces type).");
doc = docCommandVoid0(
"If necessary, dump "
"(can be done automatically for some traces type).");
addCommand("dump", makeCommandVoid0(*this, &Tracer::trace, doc));
doc = docCommandVoid0("Start the tracing process.");
......@@ -78,7 +89,7 @@ Tracer::Tracer(const std::string n)
addCommand("setTimeStart",
makeDirectSetter(*this, &timeStart,
docDirectSetter("timeStart", "int")));
} // using namespace command
} // using namespace command
}
/* --------------------------------------------------------------------- */
......@@ -88,11 +99,11 @@ Tracer::Tracer(const std::string n)
void Tracer::addSignalToTrace(const SignalBase<int> &sig,
const string &filename) {
dgDEBUGIN(15);
// openFile may throw so it should be called first.
if (namesSet) openFile(sig, filename);
toTraceSignals.push_back(&sig);
dgDEBUGF(15, "%p", &sig);
names.push_back(filename);
if (namesSet)
openFile(sig, filename);
triger.addDependency(sig);
dgDEBUGOUT(15);
}
......@@ -128,14 +139,12 @@ void Tracer::openFiles(const std::string &rootdir_,
dgDEBUGIN(15);
std::basic_string<char>::size_type n = rootdir_.length();
rootdir = rootdir_;
if ((0 < n) & ('/' != rootdir[n - 1]))
rootdir += '/';
if ((0 < n) & ('/' != rootdir[n - 1])) rootdir += '/';
basename = basename_;
suffix = suffix_;
if (files.size())
closeFiles();
if (files.size()) closeFiles();
SignalList::const_iterator iter = toTraceSignals.begin();
NameList::const_iterator iterName = names.begin();
......@@ -170,6 +179,7 @@ void Tracer::openFile(const SignalBase<int> &sig, const string &givenname) {
void Tracer::closeFiles() {
dgDEBUGIN(15);
std::lock_guard<std::mutex> files_lock(files_mtx);
for (FileList::iterator iter = files.begin(); files.end() != iter; ++iter) {
std::ostream *filePtr = *iter;
......@@ -192,6 +202,14 @@ void Tracer::record() {
dgDEBUGIN(15);
// Ensure record() never hangs. If the attempt to acquire the lock fails,
// then closeFiles() is active and we shouldn't write to files anyways.
std::unique_lock<std::mutex> files_lock(files_mtx, std::try_to_lock);
if (!files_lock.owns_lock()) {
dgDEBUGOUT(15);
return;
}
if (files.size() != toTraceSignals.size()) {
DG_THROW
ExceptionTraces(ExceptionTraces::NOT_OPEN, "No files open for tracing",
......
# Copyright 2010, Olivier Stasse, JRL, CNRS/AIST
#
# Copyright 2010-2020, Olivier Stasse, Guilhem Saurel, JRL, CNRS/AIST, LAAS-CNRS
ADD_DEFINITIONS(-DDEBUG=2)
add_definitions(-DBOOST_TEST_DYN_LINK -DBOOST_TEST_MAIN)
# Add Boost path to include directories.
#INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
# Make Boost.Test generates the main function in test cases.
ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK -DBOOST_TEST_MAIN)
ADD_DEFINITIONS(-DTESTS_DATADIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
ADD_DEFINITIONS(-DTESTS_PLUGINDIR="${LIBRARY_OUTPUT_PATH}")
ADD_DEFINITIONS(-DTESTS_DYNLIBSUFFIX="${CMAKE_SHARED_LIBRARY_SUFFIX}")
# DYNAMIC_GRAPH_TEST(NAME)
# ------------------------
#
# Define a test named `NAME'.
#
# This macro will create a binary from `NAME.cpp', link it against
# Boost and add it to the test suite.
#
MACRO(DYNAMIC_GRAPH_TEST NAME)
ADD_EXECUTABLE(${NAME} ${NAME}.cpp)
ADD_TEST(${NAME} ${RUNTIME_OUTPUT_DIRECTORY}/${NAME})
TARGET_LINK_LIBRARIES(${NAME} ${PROJECT_NAME})
ADD_DEPENDENCIES(${NAME} ${PROJECT_NAME})
# Link against Boost.
TARGET_LINK_LIBRARIES(${NAME} ${Boost_LIBRARIES})
ENDMACRO(DYNAMIC_GRAPH_TEST)
add_definitions(-DTESTS_DATADIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
add_definitions(-DTESTS_PLUGINDIR="${LIBRARY_OUTPUT_PATH}")
add_definitions(-DTESTS_DYNLIBSUFFIX="${CMAKE_SHARED_LIBRARY_SUFFIX}")
macro(DYNAMIC_GRAPH_TEST NAME)
add_unit_test(${NAME} "${NAME}.cpp")
target_link_libraries(${NAME} PRIVATE ${PROJECT_NAME}
Boost::unit_test_framework)
endmacro(DYNAMIC_GRAPH_TEST)
# Signal cast test.
SET(signalcast_libs signal-cast-registerer-libA signal-cast-registerer-libB)
set(signalcast_libs signal-cast-registerer-libA signal-cast-registerer-libB)
FOREACH(lib ${signalcast_libs})
ADD_LIBRARY(${lib} SHARED ${lib}.cpp)
foreach(lib ${signalcast_libs})
add_library(${lib} SHARED "${lib}.cpp")
target_link_libraries(${lib} PRIVATE ${PROJECT_NAME})
endforeach()
TARGET_LINK_LIBRARIES(${lib} ${PROJECT_NAME})
ADD_DEPENDENCIES(${lib} ${PROJECT_NAME})
ENDFOREACH()
DYNAMIC_GRAPH_TEST(signal-cast-registerer)
dynamic_graph_test(signal-cast-registerer)
# Unit testing.
IF(NOT APPLE)
DYNAMIC_GRAPH_TEST(entity)
ENDIF(NOT APPLE)
DYNAMIC_GRAPH_TEST(custom-entity)
DYNAMIC_GRAPH_TEST(factory)
DYNAMIC_GRAPH_TEST(pool)
DYNAMIC_GRAPH_TEST(signal-time-dependent)
DYNAMIC_GRAPH_TEST(value)
DYNAMIC_GRAPH_TEST(signal-ptr)
DYNAMIC_GRAPH_TEST(real-time-logger)
DYNAMIC_GRAPH_TEST(debug-trace)
DYNAMIC_GRAPH_TEST(debug-tracer)
TARGET_LINK_LIBRARIES(debug-tracer tracer)
DYNAMIC_GRAPH_TEST(debug-real-time-tracer)
TARGET_LINK_LIBRARIES(debug-real-time-tracer tracer-real-time tracer)
DYNAMIC_GRAPH_TEST(debug-logger)
DYNAMIC_GRAPH_TEST(debug-logger-winit)
DYNAMIC_GRAPH_TEST(signal-all)
DYNAMIC_GRAPH_TEST(command-test)
DYNAMIC_GRAPH_TEST(test-mt)
TARGET_LINK_LIBRARIES(test-mt tracer)
if(NOT APPLE)
dynamic_graph_test(entity)
endif(NOT APPLE)
dynamic_graph_test(custom-entity)
dynamic_graph_test(factory)
dynamic_graph_test(pool)
dynamic_graph_test(signal-time-dependent)
dynamic_graph_test(value)
dynamic_graph_test(signal-ptr)
dynamic_graph_test(real-time-logger)
dynamic_graph_test(debug-trace)
dynamic_graph_test(debug-tracer)
target_link_libraries(debug-tracer PRIVATE tracer)
dynamic_graph_test(debug-real-time-tracer)
target_link_libraries(debug-real-time-tracer PRIVATE tracer-real-time tracer)
dynamic_graph_test(debug-logger)
dynamic_graph_test(debug-logger-winit)
dynamic_graph_test(signal-all)
dynamic_graph_test(command-test)
dynamic_graph_test(test-mt)
target_link_libraries(test-mt PRIVATE tracer)
dynamic_graph_test(exceptions)
......@@ -5,21 +5,27 @@
* See LICENSE file
*
*/
#include "dynamic-graph/command-bind.h"
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <iostream>
#include <sstream>
#include "dynamic-graph/command-bind.h"
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#define ENABLE_RT_LOG
#include <dynamic-graph/logger.h>
#include <dynamic-graph/real-time-logger.h>
#define BOOST_TEST_MODULE debug - logger
#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;
......@@ -28,21 +34,25 @@ using namespace dynamicgraph::command;
namespace dynamicgraph {
class CustomEntity : public Entity {
public:
public:
static const std::string CLASS_NAME;
bool test_zero_arg_;
bool test_one_arg_;
bool test_two_args_;
bool test_three_args_;
bool test_four_args_;
bool test_one_arg_ret_;
bool test_two_args_ret_;
virtual const std::string &getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {
explicit CustomEntity(const std::string &n) : Entity(n) {
test_zero_arg_ = false;
test_one_arg_ = false;
test_two_args_ = false;
test_three_args_ = false;
test_four_args_ = false;
test_one_arg_ret_ = false;
test_two_args_ret_ = false;
addCommand("0_arg", makeCommandVoid0(*this, &CustomEntity::zero_arg,
docCommandVoid0("zero arg")));
......@@ -62,6 +72,29 @@ public:
makeCommandVoid4(
*this, &CustomEntity::four_args,
docCommandVoid4("four args", "int", "int", "int", "int")));
addCommand("1_arg_r",
makeCommandReturnType1(*this, &CustomEntity::one_arg_ret,
docCommandVoid1("one arg", "int")));
addCommand("2_args_r", makeCommandReturnType2(
*this, &CustomEntity::two_args_ret,
docCommandVoid2("two args", "int", "int")));
addCommand(
"cmd_verbose",
makeCommandVerbose(*this, &CustomEntity::cmd_verbose,
docCommandVerbose("Display some information")));
/// Generating an exception by adding a command which already exist
bool res = false;
std::string e_1_arg("1_arg");
try {
addCommand(e_1_arg, getNewStyleCommand(e_1_arg));
} catch (dynamicgraph::ExceptionFactory &aef) {
res = (aef.getCode() == dynamicgraph::ExceptionFactory::OBJECT_CONFLICT);
}
BOOST_CHECK(res);
}
~CustomEntity() {}
......@@ -79,15 +112,31 @@ public:
void four_args(const int &, const int &, const int &, const int &) {
test_four_args_ = true;
}
int one_arg_ret(const int &) {
test_one_arg_ret_ = true;
return 2;
}
std::string two_args_ret(const int &, const int &) {
test_two_args_ret_ = true;
return std::string("return");
}
void cmd_verbose(std::ostream &oss) {
std::string as("print verbose");
oss << as;
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(command_test) {
dynamicgraph::CustomEntity &entity =
*(dynamic_cast<dynamicgraph::CustomEntity *>(
dynamicgraph::CustomEntity *ptr_entity =
(dynamic_cast<dynamicgraph::CustomEntity *>(
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
"my-entity")));
dynamicgraph::CustomEntity &entity = *ptr_entity;
std::map<const std::string, Command *> aCommandMap =
entity.getNewStyleCommandMap();
......@@ -95,8 +144,7 @@ BOOST_AUTO_TEST_CASE(command_test) {
std::map<const std::string, Command *>::iterator it_map;
it_map = aCommandMap.find("0_arg");
if (it_map == aCommandMap.end())
BOOST_CHECK(false);
if (it_map == aCommandMap.end()) BOOST_CHECK(false);
it_map->second->execute();
BOOST_CHECK(entity.test_zero_arg_);
......@@ -111,50 +159,81 @@ BOOST_AUTO_TEST_CASE(command_test) {
for (unsigned int i = 0; i < 4; i++) {
it_map = aCommandMap.find(vec_fname[i]);
if (it_map == aCommandMap.end())
BOOST_CHECK(false);
if (it_map == aCommandMap.end()) BOOST_CHECK(false);
values.push_back(aValue);
it_map->second->setParameterValues(values);
it_map->second->execute();
it_map->second->owner();
it_map->second->getDocstring();
}
BOOST_CHECK(entity.test_one_arg_);
BOOST_CHECK(entity.test_two_args_);
BOOST_CHECK(entity.test_three_args_);
BOOST_CHECK(entity.test_four_args_);
// With return type.
vec_fname.clear();
vec_fname.push_back(std::string("1_arg_r"));
vec_fname.push_back(std::string("2_args_r"));
values.clear();
for (unsigned int i = 0; i < 2; i++) {
it_map = aCommandMap.find(vec_fname[i]);
if (it_map == aCommandMap.end()) {
BOOST_CHECK(false);
exit(-1);
}
values.push_back(aValue);
it_map->second->setParameterValues(values);
Value aValue = it_map->second->execute();
it_map->second->owner();
it_map->second->getDocstring();
}
BOOST_CHECK(entity.test_one_arg_ret_);
BOOST_CHECK(entity.test_two_args_ret_);
std::vector<Value> values_two;
values_two.push_back(aValue);
/// Wrong number of arguments
bool res=false;
bool res = false;
it_map = aCommandMap.find(std::string("2_args"));
try {
it_map->second->setParameterValues(values_two);
}
catch (const dynamicgraph::ExceptionAbstract &aea)
{
} catch (const dynamicgraph::ExceptionAbstract &aea) {
res = (aea.getCode() == dynamicgraph::ExceptionAbstract::ABSTRACT);
}
BOOST_CHECK(res);
double snd_arg_db=10.0;
double snd_arg_db = 10.0;
Value aValue2(snd_arg_db);
values_two.push_back(aValue2);
/// Wrong types of arguments
res=false;
res = false;
it_map = aCommandMap.find(std::string("2_args"));
try {
it_map->second->setParameterValues(values_two);
}
catch (const dynamicgraph::ExceptionAbstract &aea)
{
} catch (const dynamicgraph::ExceptionAbstract &aea) {
res = (aea.getCode() == dynamicgraph::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
/// Try to find the command 1_arg
res = false;
entity.getNewStyleCommand(vec_fname[0]);
BOOST_CHECK(true);
/// Generate an exception by searching a command with an empty name.w
std::string empty("");
try {
entity.getNewStyleCommand(empty);
} catch (dynamicgraph::ExceptionFactory &aef) {
res = (aef.getCode() == dynamicgraph::ExceptionFactory::UNREFERED_FUNCTION);
}
BOOST_CHECK(res);
/// delete the entity.
delete ptr_entity;
}
......@@ -5,11 +5,16 @@
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <sstream>
#define BOOST_TEST_MODULE customEntity
#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;
......@@ -19,7 +24,7 @@ struct CustomEntity : public dynamicgraph::Entity {
virtual const std::string &getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {}
explicit CustomEntity(const std::string &n) : Entity(n) {}
virtual ~CustomEntity() {}
......
......@@ -9,30 +9,33 @@
#include <sstream>
#define ENABLE_RT_LOG
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/logger.h>
#include <dynamic-graph/real-time-logger.h>
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#define BOOST_TEST_MODULE debug - logger
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#include <boost/test/unit_test.hpp>
#endif
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/thread/thread.hpp>
using boost::test_tools::output_test_stream;
namespace dynamicgraph {
class CustomEntity : public Entity {
public:
public:
static const std::string CLASS_NAME;
virtual const std::string &getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {
explicit CustomEntity(const std::string &n) : Entity(n) {
logger_.setTimeSample(0.001);
logger_.setStreamPrintPeriod(0.005);
logger_.setVerbosity(VERBOSITY_ALL);
......@@ -59,13 +62,10 @@ public:
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(debug_logger_wrong_initialization) {
std::ofstream of;
dynamicgraph::RealTimeLogger::instance();
// of.open("/tmp/dg-LOGS.txt",std::ofstream::out|std::ofstream::app);
// dgADD_OSTREAM_TO_RTLOG (of);
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
......
......@@ -5,68 +5,126 @@
* See LICENSE file
*
*/
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#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 ENABLE_RT_LOG
#include <dynamic-graph/logger.h>
#include <dynamic-graph/real-time-logger.h>
#define BOOST_TEST_MODULE debug - logger
#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:
public:
static const std::string CLASS_NAME;
virtual const std::string &getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {
explicit CustomEntity(const std::string &n) : Entity(n) {
logger_.setTimeSample(0.001);
logger_.setStreamPrintPeriod(0.005);
logger_.setVerbosity(VERBOSITY_NONE);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_NONE);
BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_ERROR).isNull());
logger_.setVerbosity(VERBOSITY_ERROR);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ERROR);
BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
logger_.setVerbosity(VERBOSITY_WARNING_ERROR);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_WARNING_ERROR);
BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
logger_.setVerbosity(VERBOSITY_INFO_WARNING_ERROR);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_INFO_WARNING_ERROR);
BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
logger_.setVerbosity(VERBOSITY_ALL);
LoggerVerbosity alv = logger_.getVerbosity();
BOOST_CHECK(alv == VERBOSITY_ALL);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ALL);
BOOST_CHECK(!logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
}
~CustomEntity() {}
void testDebugTrace() {
sendMsg("This is a message of level MSG_TYPE_DEBUG", MSG_TYPE_DEBUG);
sendMsg("This is a message of level MSG_TYPE_INFO", MSG_TYPE_INFO);
sendMsg("This is a message of level MSG_TYPE_WARNING", MSG_TYPE_WARNING);
sendMsg("This is a message of level MSG_TYPE_ERROR", MSG_TYPE_ERROR);
sendMsg("This is a message of level MSG_TYPE_DEBUG_STREAM",
MSG_TYPE_DEBUG_STREAM);
sendMsg("This is a message of level MSG_TYPE_INFO_STREAM",
MSG_TYPE_INFO_STREAM);
sendMsg("This is a message of level MSG_TYPE_WARNING_STREAM",
MSG_TYPE_WARNING_STREAM);
sendMsg("This is a message of level MSG_TYPE_ERROR_STREAM",
MSG_TYPE_ERROR_STREAM);
logger_.stream(MSG_TYPE_DEBUG)
<< "This is a message of level MSG_TYPE_DEBUG\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_INFO)
<< "This is a message of level MSG_TYPE_INFO\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_WARNING)
<< "This is a message of level MSG_TYPE_WARNING\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_ERROR)
<< "This is a message of level MSG_TYPE_ERROR\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_DEBUG_STREAM)
<< "This is a message of level MSG_TYPE_DEBUG_STREAM\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_INFO_STREAM)
<< "This is a message of level MSG_TYPE_INFO_STREAM\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_WARNING_STREAM)
<< "This is a message of level MSG_TYPE_WARNING_STREAM\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_ERROR_STREAM)
<< "This is a message of level MSG_TYPE_ERROR_STREAM\n";
/* Add test toString */
dynamicgraph::RealTimeLogger::instance().spinOnce();
double q = 1.0;
sendMsg("Value to display: " + toString(q));
logger_.stream() << "Value to display: " + toString(q) << '\n';
dynamicgraph::RealTimeLogger::instance().spinOnce();
std::vector<double> vq;
vq.resize(3);
vq[0] = 1.0;
vq[1] = 2.0;
vq[2] = 3.0;
sendMsg("Value to display: " + toString(vq));
logger_.stream(MSG_TYPE_INFO)
<< "Value to display: " << toString(vq) << '\n';
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_INFO)
<< "Value to display: " << toString(vq, 3, 10) << '\n';
dynamicgraph::RealTimeLogger::instance().spinOnce();
Eigen::Matrix<double, 3, 3> an_eig_m;
an_eig_m.Ones();
sendMsg("Value to display: " + toString(an_eig_m));
an_eig_m.setOnes();
logger_.stream(MSG_TYPE_INFO)
<< "Value to display: " << toString(an_eig_m) << '\n';
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.countdown();
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(debug_logger) {
std::ofstream of;
......@@ -83,8 +141,8 @@ BOOST_AUTO_TEST_CASE(debug_logger) {
entity.setTimeSample(0.002);
BOOST_CHECK_EQUAL(entity.getTimeSample(), 0.002);
entity.setStreamPrintPeriod(0.004);
BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.004);
entity.setStreamPrintPeriod(0.002);
BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.002);
for (unsigned int i = 0; i < 10000; i++) {
entity.testDebugTrace();
......
......@@ -4,8 +4,7 @@
*
*/
#include <iostream>
#include <dynamic-graph/command.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
......@@ -14,9 +13,14 @@
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/tracer-real-time.h>
#include <iostream>
#define BOOST_TEST_MODULE debug - tracer
#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;
......@@ -28,7 +32,7 @@ struct MyEntity : public dynamicgraph::Entity {
dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTwoTimeDepSOUT;
MyEntity(const std::string &name)
explicit MyEntity(const std::string &name)
: Entity(name),
m_sigdSIN("MyEntity(" + name + ")::input(double)::in_double"),
m_sigdTimeDepSOUT(boost::bind(&MyEntity::update, this, _1, _2),
......@@ -42,12 +46,6 @@ struct MyEntity : public dynamicgraph::Entity {
signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT << m_sigdTwoTimeDepSOUT);
}
virtual void display(std::ostream &os) const {
os << "Hello! My name is " << getName() << " !" << std::endl;
}
virtual const std::string &getClassName() const { return CLASS_NAME; }
double &update(double &res, const int &inTime) {
const double &aDouble = m_sigdSIN(inTime);
res = aDouble;
......@@ -55,57 +53,68 @@ struct MyEntity : public dynamicgraph::Entity {
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(MyEntity, "MyEntity");
} // namespace dynamicgraph
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(test_tracer) {
using namespace dynamicgraph;
// Creates a tracer.
TracerRealTime &atracer = *dynamic_cast<TracerRealTime *>(
FactoryStorage::getInstance()->newEntity("TracerRealTime",
"my-tracer"));
FactoryStorage::getInstance()->newEntity("TracerRealTime", "my-tracer"));
Entity &entity =
*FactoryStorage::getInstance()->newEntity("MyEntity",
"my-entity");
MyEntity &entity = *dynamic_cast<MyEntity *>(
FactoryStorage::getInstance()->newEntity("MyEntity", "my-entity"));
std::string rootdir("/tmp");
std::string basename("my-tracer");
std::string suffix(".dat");
/// Test openfiles
atracer.openFiles(rootdir, basename, suffix);
atracer.setBufferSize(1 << 14);
/// Add trace by name
// Check that an exception is thrown if the filename is invalid.
atracer.openFiles(rootdir, "invalid/filename", suffix);
BOOST_CHECK_THROW(
atracer.addSignalToTraceByName("my-entity.out_double", "output"),
ExceptionTraces);
// Test openfiles
atracer.openFiles(rootdir, basename, suffix);
// Add trace by name
atracer.addSignalToTraceByName("my-entity.out_double", "output");
/// Add trace by name
SignalBase<int> &aSignal = entity.getSignal("out2double");
Signal<double, int> &aSignalInt =
*(dynamic_cast<Signal<double, int> *>(
&entity.getSignal("in_double")));
SignalBase<int> &out_double = entity.getSignal("out_double");
SignalBase<int> &out_double_2 = entity.getSignal("out2double");
Signal<double, int> &in_double =
*(dynamic_cast<Signal<double, int> *>(&entity.getSignal("in_double")));
aSignalInt.setConstant(1.5);
in_double.setConstant(1.5);
atracer.start();
atracer.trace();
std::string emptybuf_cmd_str("empty");
command::Command *acmd = atracer.getNewStyleCommand(emptybuf_cmd_str);
acmd->execute();
for (int i = 0; i < 1000; i++) {
aSignal.setTime(i);
aSignalInt.setTime(i);
in_double.setTime(i);
out_double.recompute(i);
out_double_2.recompute(i);
atracer.recordTrigger(i, i);
}
output_test_stream output;
atracer.display(output);
atracer.stop();
atracer.trace();
atracer.clearSignalToTrace();
atracer.closeFiles();
acmd->execute();
atracer.record();
output_test_stream output;
atracer.display(output);
BOOST_CHECK(output.is_equal("TracerRealTime my-tracer [mode=pause] : "
"\n - Dep list: \n"));
BOOST_CHECK(output.is_equal(
"TracerRealTime my-tracer [mode=play] : \n"
" - Dep list: \n"
" -> MyEntity(my-entity)::input(double)::out_double (in output)"
" [8Ko/16Ko] \n"));
}
......@@ -3,13 +3,15 @@
* Olivier Stasse
*
*/
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#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
......@@ -18,17 +20,21 @@
#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:
public:
static const std::string CLASS_NAME;
virtual const std::string &getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {
explicit CustomEntity(const std::string &n) : Entity(n) {
dynamicgraph::dgDEBUGFLOW.openFile("/tmp/dynamic-graph-traces.txt");
}
~CustomEntity() {
......@@ -48,15 +54,18 @@ public:
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(testDebugTrace) {
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
dynamicgraph::CustomEntity &entity =
*(dynamic_cast<dynamicgraph::CustomEntity *>(
dynamicgraph::CustomEntity *ptr_entity =
(dynamic_cast<dynamicgraph::CustomEntity *>(
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
"my-entity")));
dynamicgraph::CustomEntity &entity = *ptr_entity;
entity.testDebugTrace();
/// Copy the debug file into the oss_debug_file
......@@ -79,14 +88,17 @@ BOOST_AUTO_TEST_CASE(testDebugTrace) {
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 }";
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;
}