Commit 7db88dd1 authored by Francois Bleibel's avatar Francois Bleibel
Browse files

Added tracer plugin to source.

parent 697ec31f
......@@ -53,6 +53,7 @@ class DYNAMICGRAPH_EXPORT ExceptionAbstract
ABSTRACT = 0
,SIGNAL = 100
,FACTORY = 200
,TRACES = 300
};
static const std::string EXCEPTION_NAME;
......
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Copyright Projet JRL-Japan, 2007
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* File: ExceptionTraces.h
* Project: SOT
* Author: Nicolas Mansard
*
* Version control
* ===============
*
* $Id$
*
* Description
* ============
*
*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#ifndef __TRACES_EXCEPTION_H
#define __TRACES_EXCEPTION_H
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
#include <dynamic-graph/exception-abstract.h>
#include <dynamic-graph/dynamic-graph-api.h>
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
namespace dynamicgraph {
/* \class ExceptionTraces
*/
class DYNAMICGRAPH_EXPORT ExceptionTraces
:public ExceptionAbstract
{
public:
enum ErrorCodeEnum
{
GENERIC = ExceptionAbstract::TRACES
,NOT_OPEN
};
static const std::string EXCEPTION_NAME;
virtual const std::string& getExceptionName( void ) const { return EXCEPTION_NAME; }
public:
ExceptionTraces ( const ExceptionTraces::ErrorCodeEnum& errcode,
const std::string & msg = "" );
ExceptionTraces( const ExceptionTraces::ErrorCodeEnum& errcode,
const std::string & msg,const char* format, ... );
virtual ~ExceptionTraces( void ){}
};
} //namespace dynamicgraph
#endif /* #ifndef __TRACES_EXCEPTION_H */
/*
* Local variables:
* c-basic-offset: 2
* End:
*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Copyright Projet JRL-Japan, 2007
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* File: Tracer.h
* Project: DG
* Author: Nicolas Mansard
*
* Version control
* ===============
*
* $Id$
*
* Description
* ============
*
*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#ifndef __TRACER_RT_H__
#define __TRACER_RT_H__
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* STD */
#include <sstream>
/* DG */
#include <dynamic-graph/tracer.h>
/* --------------------------------------------------------------------- */
/* --- API ------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
#if defined (WIN32)
# if defined (tracer_real_time_EXPORTS)
# define DGTRACERREALTIME_EXPORT __declspec(dllexport)
# else
# define DGTRACERREALTIME_EXPORT __declspec(dllimport)
# endif
#else
# define DGTRACERREALTIME_EXPORT
#endif
/* --------------------------------------------------------------------- */
/* --- TRACER ---------------------------------------------------------- */
/* --------------------------------------------------------------------- */
namespace dynamicgraph {
class DGTRACERREALTIME_EXPORT OutStringStream
: public std::ostringstream
{
public:
char * buffer;
unsigned int index;
unsigned int bufferSize;
bool full;
std::string givenname;
public:
OutStringStream( void );
~OutStringStream( void );
void resize( const unsigned int & size );
bool addData( const char * data, const unsigned int & size );
void dump( std::ostream& os );
void empty( void );
};
class DGTRACERREALTIME_EXPORT TracerRealTime
: public Tracer
{
public:
static const std::string CLASS_NAME;
virtual const std::string& getClassName( void ) { return CLASS_NAME; }
protected:
int bufferSize;
static const int BUFFER_SIZE_DEFAULT = 1048576; // 1Mo
typedef std::list< std::ofstream* > HardFileList;
HardFileList hardFiles;
public:
TracerRealTime( const std::string n );
virtual ~TracerRealTime( void ){ }
public:
virtual void closeFiles( void );
virtual void trace( void );
protected:
virtual void openFile( const SignalBase<int> & sig,
const std::string& filename );
virtual void recordSignal( std::ostream& os,
const SignalBase<int>& sig );
void emptyBuffers( void );
public:
void setBufferSize( const int & SIZE ) { bufferSize=SIZE; }
const int & getBufferSize( void ) { return bufferSize; }
public:
/* --- DISPLAY ------------------------------------------------------------ */
void display( std::ostream& os ) const;
DGTRACERREALTIME_EXPORT friend std::ostream& operator<< ( std::ostream& os,const TracerRealTime& t );
/* --- PARAMS --- */
virtual void commandLine( const std::string& cmdLine
,std::istringstream& cmdArgs
,std::ostream& os );
};
} // namespace dynamicgraph
#endif /* #ifndef __TRACER_RT_H__ */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Copyright Projet JRL-Japan, 2007
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* File: Tracer.h
* Project: DG
* Author: Nicolas Mansard
*
* Version control
* ===============
*
* $Id$
*
* Description
* ============
*
*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#ifndef __TRACER_H__
#define __TRACER_H__
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* STD */
#include <string>
#include <list>
#include <boost/function.hpp>
/* DG */
#include <dynamic-graph/signal-base.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/time-dependency.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-traces.h>
/* --------------------------------------------------------------------- */
/* --- API ------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
#if defined (WIN32)
# if defined (tracer_EXPORTS)
# define DGTRACER_EXPORT __declspec(dllexport)
# else
# define DGTRACER_EXPORT __declspec(dllimport)
# endif
#else
# define DGTRACER_EXPORT
#endif
/* --------------------------------------------------------------------- */
/* --- TRACER ---------------------------------------------------------- */
/* --------------------------------------------------------------------- */
namespace dynamicgraph {
class DGTRACER_EXPORT Tracer
: public Entity
{
protected:
typedef std::list< const SignalBase<int>* > SignalList;
SignalList toTraceSignals;
public:
static const std::string CLASS_NAME;
virtual const std::string& getClassName( void ) { return CLASS_NAME; }
public:
enum TraceStyle
{
WHEN_SAID /// Record, then trace to file only when said to.
,EACH_TIME /// Record and trace to file immediately.
,FREQUENTLY /// Record X time then trace (X is tuned by setFrenquence() ).
};
TraceStyle traceStyle;
static const TraceStyle TRACE_STYLE_DEFAULT = EACH_TIME;
double frequency;
std::string basename;
std::string suffix;
std::string rootdir;
bool namesSet;
typedef std::list< std::ostream* > FileList;
FileList files;
typedef std::list< std::string > NameList;
NameList names;
bool play;
int timeStart;
public:
Tracer( const std::string n );
virtual ~Tracer( void ){ closeFiles(); }
void addSignalToTrace( const SignalBase<int>& sig,
const std::string& filename="" );
void clearSignalToTrace( void );
//void parasite( SignalBase<int>& sig );
void openFiles( const std::string& rootdir, const std::string& basename,
const std::string& suffix );
virtual void closeFiles( void );
protected:
virtual void openFile( const SignalBase<int> & sig,
const std::string& filename );
public:
void setTraceStyle( const TraceStyle& style ){ traceStyle = style; }
TraceStyle getTraceStyle( void ){ return traceStyle; }
void setFrenquency( const double& frqu ){ frequency = frqu; }
double getFrequency( void ){ return frequency; }
void record( void );
virtual void recordSignal( std::ostream& os,
const SignalBase<int>& sig );
int& recordTrigger( int& dummy, const int& time );
virtual void trace( void );
public:
//SignalTrigerer<int> triger;
SignalTimeDependent<int,int> triger;
/* --- DISPLAY ------------------------------------------------------------ */
DGTRACER_EXPORT friend std::ostream& operator<< ( std::ostream& os,const Tracer& t );
/* --- PARAMS --- */
void display( std::ostream& os ) const;
virtual void commandLine( const std::string& cmdLine
,std::istringstream& cmdArgs
,std::ostream& os );
};
} // namespace dynamicgraph
#endif /* #ifndef __TRACER_H__ */
......@@ -55,6 +55,8 @@ SET(plugins_list
plugins/shell-functions
plugins/shell-procedure
debug/contiifstream
traces/tracer
traces/tracer-real-time
)
FOREACH(plugin_file ${plugins_list})
......
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Copyright Projet JRL-Japan, 2007
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* File: ExceptionTraces.cpp
* Project: SOT
* Author: Nicolas Mansard
*
* Version control
* ===============
*
* $Id$
*
* Description
* ============
*
*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <dynamic-graph/exception-traces.h>
#include <stdarg.h>
#include <cstdio>
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
const std::string ExceptionTraces::EXCEPTION_NAME = "Traces";
ExceptionTraces::
ExceptionTraces ( const ExceptionTraces::ErrorCodeEnum& errcode,
const std::string & msg )
:ExceptionAbstract(errcode,msg)
{
}
ExceptionTraces::
ExceptionTraces ( const ExceptionTraces::ErrorCodeEnum& errcode,
const std::string & msg,const char* format, ... )
:ExceptionAbstract(errcode,msg)
{
va_list args;
va_start(args,format);
const unsigned int SIZE = 256;
char buffer[SIZE];
vsnprintf(buffer,SIZE,format,args);
message += buffer;
va_end(args);
}
/*
* Local variables:
* c-basic-offset: 2
* End:
*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Copyright Projet JRL-Japan, 2007
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* File: Tracer.cpp
* Project: DG
* Author: Nicolas Mansard
*
* Version control
* ===============
*
* $Id$
*
* Description
* ============
*
*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* DG */
#include <iomanip>
#include <boost/bind.hpp>
#include <dynamic-graph/tracer-real-time.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/factory.h>
using namespace std;
using namespace dynamicgraph;
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(TracerRealTime,"TracerRealTime");
/* --------------------------------------------------------------------- */
/* --- DGOUTSTRINGSTREAM ---------------------------------------------- */
/* --------------------------------------------------------------------- */
OutStringStream::
OutStringStream( void )
: std::ostringstream()
,buffer( 0 ),index(0),bufferSize(0),full(false)
{
dgDEBUGINOUT(15);
}
OutStringStream::
~OutStringStream( void )
{
dgDEBUGIN(15);
if( buffer ) delete [] buffer ;
dgDEBUGOUT(15);
}
void OutStringStream::
resize( const unsigned int & size )
{
dgDEBUGIN(15);
index=0; bufferSize = size; full=false;
if( 0!=buffer ) delete [] buffer;
buffer = new char [size];
dgDEBUGOUT(15);
}
bool OutStringStream::
addData( const char * data, const unsigned int & size )
{
dgDEBUGIN(15);
unsigned int towrite = size;
if( index+towrite>bufferSize )
{
dgDEBUGOUT(15);
full=true;
return false;
}//towrite=bufferSize-index;
memcpy( buffer+index,data,towrite );
index+=towrite;
dgDEBUGOUT(15);
return true;
}
void OutStringStream::
dump( std::ostream& os )
{
dgDEBUGIN(15);
os.write( buffer,index );
dgDEBUGOUT(15);
}
void OutStringStream::
empty( void )
{
dgDEBUGIN(15);
index=0; full=false;
dgDEBUGOUT(15);
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
TracerRealTime::TracerRealTime( const std::string n )
:Tracer(n)
,bufferSize( BUFFER_SIZE_DEFAULT )
{
dgDEBUGINOUT(15);
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
void TracerRealTime::
openFile( const SignalBase<int> & sig,
const std::string& givenname )
{
dgDEBUGIN(15);
string signame;
if( givenname.length() )
{ signame = givenname; } else { signame = sig.shortName(); }
string filename = rootdir + basename + signame + suffix;
dgDEBUG(5) << "Sig <"<<sig.getName()
<< ">: new file "<< filename << endl;
std::ofstream * newfile = new std::ofstream( filename.c_str() );
hardFiles.push_back( newfile );
//std::stringstream * newbuffer = new std::stringstream();
OutStringStream * newbuffer = new OutStringStream(); // std::stringstream();
newbuffer->resize( bufferSize );
newbuffer->givenname = givenname;
files.push_back( newbuffer );
dgDEBUGOUT(15);
}
void TracerRealTime::
closeFiles( void )
{
dgDEBUGIN(15);
FileList::iterator iter = files.begin();
HardFileList::iterator hardIter = hardFiles.begin();
while( files.end()!=iter )
{
dgDEBUG(25) << "Close the files." << endl;
std::stringstream * file = dynamic_cast< stringstream* >(*iter);
std::ofstream * hardFile = *hardIter;
(*hardFile) <<flush; hardFile->close();
delete file;
delete hardFile;
++iter; ++hardIter;
}
dgDEBUG(25) << "Clear the lists." << endl;
files.clear();
hardFiles.clear();
dgDEBUGOUT(15);
}
void TracerRealTime::
trace( void )
{
dgDEBUGIN(15);
FileList::iterator iter = files.begin();
HardFileList::iterator hardIter = hardFiles.begin();
while( files.end()!=iter )
{
dgDEBUG(35) << "Next" << endl;
std::ostream * os = *iter;