tracer-real-time.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 /* --------------------------------------------------------------------- */
11 /* --- INCLUDE --------------------------------------------------------- */
12 /* --------------------------------------------------------------------- */
13 
14 /* DG */
16 #include <dynamic-graph/debug.h>
17 #include <dynamic-graph/factory.h>
18 #include <dynamic-graph/pool.h>
20 
21 #include <boost/bind.hpp>
22 #include <iomanip>
23 
24 using namespace std;
25 using namespace dynamicgraph;
26 
28 
29 /* --------------------------------------------------------------------- */
30 /* --- DGOUTSTRINGSTREAM ---------------------------------------------- */
31 /* --------------------------------------------------------------------- */
32 
33 OutStringStream::OutStringStream()
34  : std::ostringstream(), buffer(0), index(0), bufferSize(0), full(false) {
35  dgDEBUGINOUT(15);
36 }
37 
39  dgDEBUGIN(15);
40  delete[] buffer;
41  dgDEBUGOUT(15);
42 }
43 
44 void OutStringStream::resize(const std::streamsize &size) {
45  dgDEBUGIN(15);
46 
47  index = 0;
48  bufferSize = size;
49  full = false;
50 
51  delete[] buffer;
52  buffer = new char[static_cast<size_t>(size)];
53 
54  dgDEBUGOUT(15);
55 }
56 
57 bool OutStringStream::addData(const char *data, const std::streamoff &size) {
58  dgDEBUGIN(15);
59  std::streamsize towrite = static_cast<std::streamsize>(size);
60  if (index + towrite > bufferSize) {
61  dgDEBUGOUT(15);
62  full = true;
63  return false;
64  }
65  memcpy(buffer + index, data, static_cast<size_t>(towrite));
66  index += towrite;
67  dgDEBUGOUT(15);
68  return true;
69 }
70 
71 void OutStringStream::dump(std::ostream &os) {
72  dgDEBUGIN(15);
73  os.write(buffer, index);
74  dgDEBUGOUT(15);
75 }
76 
78  dgDEBUGIN(15);
79  index = 0;
80  full = false;
81  dgDEBUGOUT(15);
82 }
83 
84 /* --------------------------------------------------------------------- */
85 /* --------------------------------------------------------------------- */
86 /* --------------------------------------------------------------------- */
87 
88 TracerRealTime::TracerRealTime(const std::string &n)
89  : Tracer(n), bufferSize(BUFFER_SIZE_DEFAULT) {
90  dgDEBUGINOUT(15);
91 
92  /* --- Commands --- */
93  {
94  using namespace dynamicgraph::command;
95  std::string doc = docCommandVoid0(
96  "Trash the current content of the buffers, without saving it.");
97  addCommand("empty",
99 
100  addCommand("getBufferSize",
102  docDirectGetter("bufferSize", "int")));
103  addCommand("setBufferSize",
105  docDirectSetter("bufferSize", "int")));
106  } // using namespace command
107 
108  dgDEBUGOUT(15);
109 }
110 
111 /* --------------------------------------------------------------------- */
112 /* --------------------------------------------------------------------- */
113 /* --------------------------------------------------------------------- */
114 
116  const std::string &givenname) {
117  dgDEBUGIN(15);
118  string signame;
119  if (givenname.length()) {
120  signame = givenname;
121  } else {
122  signame = sig.shortName();
123  }
124 
125  string filename = rootdir + basename + signame + suffix;
126  dgDEBUG(5) << "Sig <" << sig.getName() << ">: new file " << filename << endl;
127  std::ofstream *newfile = new std::ofstream(filename.c_str());
128  if (!newfile->good()) {
129  delete newfile;
132  "Could not open file " + filename + " for signal " + signame, "");
133  }
134  dgDEBUG(5) << "Newfile:" << (void *)newfile << endl;
135  hardFiles.push_back(newfile);
136  dgDEBUG(5) << "Creating Outstringstream" << endl;
137 
138  // std::stringstream * newbuffer = new std::stringstream ();
139  OutStringStream *newbuffer = new OutStringStream(); // std::stringstream ();
140  newbuffer->resize(bufferSize);
141  newbuffer->givenname = givenname;
142  files.push_back(newbuffer);
143 
144  dgDEBUGOUT(15);
145 }
146 
148  dgDEBUGIN(15);
149  std::lock_guard<std::mutex> files_lock(files_mtx);
150 
151  FileList::iterator iter = files.begin();
152  HardFileList::iterator hardIter = hardFiles.begin();
153 
154  while (files.end() != iter) {
155  dgDEBUG(25) << "Close the files." << endl;
156 
157  std::stringstream *file = dynamic_cast<stringstream *>(*iter);
158  std::ofstream *hardFile = *hardIter;
159 
160  (*hardFile) << flush;
161  hardFile->close();
162  delete file;
163  delete hardFile;
164 
165  ++iter;
166  ++hardIter;
167  }
168 
169  dgDEBUG(25) << "Clear the lists." << endl;
170  files.clear();
171  hardFiles.clear();
172 
173  dgDEBUGOUT(15);
174 }
175 
177  dgDEBUGIN(15);
178 
179  FileList::iterator iter = files.begin();
180  HardFileList::iterator hardIter = hardFiles.begin();
181 
182  while (files.end() != iter) {
183  dgDEBUG(35) << "Next" << endl;
184  std::ostream *os = *iter;
185  if (NULL == os) {
186  DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN, "The buffer is null",
187  "");
188  }
189  // std::stringstream & file = * dynamic_cast< stringstream* >(os);
190  OutStringStream *file = dynamic_cast<OutStringStream *>(os); // segfault
191  if (NULL == file) {
193  "The buffer is not open", "");
194  }
195 
196  std::ofstream &hardFile = **hardIter;
197  if (!hardFile.good()) {
199  "The file is not open", "");
200  }
201 
202  if ((hardFile.good()) && (NULL != file)) {
203  file->dump(hardFile);
204  file->empty();
205  hardFile.flush();
206  }
207 
208  ++iter;
209  ++hardIter;
210  }
211 
212  dgDEBUGOUT(15);
213 }
214 
216  dgDEBUGIN(15);
217  for (FileList::iterator iter = files.begin(); files.end() != iter; ++iter) {
218  // std::stringstream & file = * dynamic_cast< stringstream* >(*iter);
219  try {
220  OutStringStream &file = *dynamic_cast<OutStringStream *>(*iter);
221  file.empty();
222  // file.str("");
223  } catch (...) {
225  "The buffer is not open", "");
226  }
227  }
228  dgDEBUGOUT(15);
229 }
230 
231 void TracerRealTime::recordSignal(std::ostream &os,
232  const SignalBase<sigtime_t> &sig) {
233  dgDEBUGIN(15);
234 
235  try {
236  OutStringStream &file = dynamic_cast<OutStringStream &>(os);
237  file.str("");
238  dgDEBUG(45) << "Empty file [" << file.tellp() << "] <" << file.str().c_str()
239  << "> " << endl;
240 
241  Tracer::recordSignal(file, sig);
242  file.addData(file.str().c_str(), file.tellp());
243  dgDEBUG(35) << "Write data [" << file.tellp() << "] <" << file.str().c_str()
244  << "> " << endl;
245 
246  } catch (ExceptionAbstract &exc) {
247  throw;
248  } catch (...) {
250  "The buffer is not open", "");
251  }
252 
253  dgDEBUGOUT(15);
254  return;
255 }
256 
257 /* --------------------------------------------------------------------- */
258 /* --------------------------------------------------------------------- */
259 /* --------------------------------------------------------------------- */
260 
261 void TracerRealTime::display(std::ostream &os) const {
262  os << CLASS_NAME << " " << name << " [mode=" << (play ? "play" : "pause")
263  << "] : " << endl
264  << " - Dep list: " << endl;
265 
266  FileList::const_iterator iterFile = files.begin();
267  for (SignalList::const_iterator iter = toTraceSignals.begin();
268  toTraceSignals.end() != iter; ++iter) {
269  dgDEBUG(35) << "Next" << endl;
270  const OutStringStream *file = dynamic_cast<OutStringStream *>(*iterFile);
271  os << " -> " << (*iter)->getName();
272  if (file->givenname.length()) os << " (in " << file->givenname << ")";
273  os << "\t";
274  if (file) {
275  const std::streamsize PRECISION = os.precision();
276  const std::streamsize SIZE = file->index;
277  const std::streamsize MSIZE = file->bufferSize;
278  unsigned int dec = 0;
279  std::string unit = "";
280  if ((SIZE >> 30) || (MSIZE >> 30)) {
281  dec = 30;
282  unit = "Go";
283  } else if ((SIZE >> 20) || (MSIZE >> 20)) {
284  dec = 20;
285  unit = "Mo";
286  } else if ((SIZE >> 10) || (MSIZE >> 10)) {
287  dec = 10;
288  unit = "Ko";
289  }
290  os << "[" << std::setw(1) << std::setprecision(1)
291  << (((double)SIZE + 0.0) / (1 << dec)) << unit << "/"
292  << std::setprecision(2) << (((double)MSIZE + 0.0) / (1 << dec)) << unit
293  << "]\t";
294  if (file->full) os << "(FULL)";
295  os.precision(PRECISION);
296  }
297  os << endl;
298  ++iterFile;
299  }
300 }
301 
302 std::ostream &operator<<(std::ostream &os, const TracerRealTime &t) {
303  t.display(os);
304  return os;
305 }
dynamicgraph::OutStringStream::givenname
std::string givenname
Definition: tracer-real-time.h:24
dynamicgraph
dynamicgraph::command
Definition: command-bind.h:31
dynamicgraph::OutStringStream::bufferSize
std::streamsize bufferSize
Definition: tracer-real-time.h:22
setup.data
data
Definition: setup.in.py:48
dynamicgraph::OutStringStream::buffer
char * buffer
Definition: tracer-real-time.h:20
dynamicgraph::TracerRealTime::hardFiles
HardFileList hardFiles
Definition: tracer-real-time.h:69
dynamicgraph::Tracer::basename
std::string basename
Definition: tracer.h:47
dgDEBUG
#define dgDEBUG(level)
Definition: debug.h:157
dynamicgraph::command::docCommandVoid0
std::string docCommandVoid0(const std::string &doc)
Definition: command-bind.h:70
dynamicgraph::Tracer::play
bool play
Definition: tracer.h:55
dynamicgraph::Entity::name
std::string name
Definition: include/dynamic-graph/entity.h:174
dynamicgraph::TracerRealTime::bufferSize
int bufferSize
Definition: tracer-real-time.h:68
dynamicgraph::Tracer::files_mtx
std::mutex files_mtx
Definition: tracer.h:30
dynamicgraph::TracerRealTime::TracerRealTime
TracerRealTime(const std::string &n)
Definition: tracer-real-time.cpp:88
dynamicgraph::OutStringStream
Stream for the tracer real-time.
Definition: tracer-real-time.h:18
dynamicgraph::ExceptionTraces::NOT_OPEN
@ NOT_OPEN
Definition: exception-traces.h:20
tracer-real-time.h
dynamicgraph::command::makeDirectGetter
DirectGetter< E, T > * makeDirectGetter(E &entity, T *ptr, const std::string &docString)
Definition: command-direct-getter.h:44
dynamicgraph::Tracer::toTraceSignals
SignalList toTraceSignals
Definition: tracer.h:29
dynamicgraph::Tracer::suffix
std::string suffix
Definition: tracer.h:48
dynamicgraph::TracerRealTime::closeFiles
virtual void closeFiles()
Definition: tracer-real-time.cpp:147
dynamicgraph::Tracer::rootdir
std::string rootdir
Definition: tracer.h:49
dynamicgraph::OutStringStream::resize
void resize(const std::streamsize &size)
Definition: tracer-real-time.cpp:44
dynamicgraph::ExceptionAbstract
Abstract root class for all dynamic-graph exceptions.
Definition: exception-abstract.h:31
dynamicgraph::Tracer::files
FileList files
Definition: tracer.h:52
dynamicgraph::TracerRealTime::emptyBuffers
void emptyBuffers()
Definition: tracer-real-time.cpp:215
sig
dynamicgraph::SignalArray_const< double > sig
Definition: signal-all.cpp:25
dgDEBUGOUT
#define dgDEBUGOUT(level)
Definition: debug.h:204
dgDEBUGIN
#define dgDEBUGIN(level)
VP_DEBUG.
Definition: debug.h:202
DG_THROW
#define DG_THROW
Definition: exception-abstract.h:24
dynamicgraph::OutStringStream::~OutStringStream
~OutStringStream()
Definition: tracer-real-time.cpp:38
dynamicgraph::OutStringStream::addData
bool addData(const char *data, const std::streamoff &size)
Definition: tracer-real-time.cpp:57
dynamicgraph::command::docDirectSetter
std::string docDirectSetter(const std::string &name, const std::string &type)
Definition: command-direct-setter.h:52
dynamicgraph::TracerRealTime::recordSignal
virtual void recordSignal(std::ostream &os, const SignalBase< sigtime_t > &sig)
Definition: tracer-real-time.cpp:231
factory.h
dynamicgraph::Tracer::recordSignal
virtual void recordSignal(std::ostream &os, const SignalBase< sigtime_t > &sig)
Definition: tracer.cpp:233
dynamicgraph::OutStringStream::dump
void dump(std::ostream &os)
Definition: tracer-real-time.cpp:71
dynamicgraph::OutStringStream::index
std::streamsize index
Definition: tracer-real-time.h:21
dynamicgraph::command::docDirectGetter
std::string docDirectGetter(const std::string &name, const std::string &type)
Definition: command-direct-getter.h:49
all-commands.h
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(TracerRealTime, "TracerRealTime")
dynamicgraph::TracerRealTime::display
void display(std::ostream &os) const
Display information on the entity inside the output stream os.
Definition: tracer-real-time.cpp:261
dynamicgraph::command::makeDirectSetter
DirectSetter< E, T > * makeDirectSetter(E &entity, T *ptr, const std::string &docString)
Definition: command-direct-setter.h:47
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
Add a command to Entity.
Definition: src/dgraph/entity.cpp:191
dynamicgraph::TracerRealTime::openFile
virtual void openFile(const SignalBase< sigtime_t > &sig, const std::string &filename)
Definition: tracer-real-time.cpp:115
dynamicgraph::SignalBase< sigtime_t >
dynamicgraph::Tracer
Tracer plug-in main class.
Definition: tracer.h:24
dgDEBUGINOUT
#define dgDEBUGINOUT(level)
Definition: debug.h:206
dynamicgraph::TracerRealTime::trace
virtual void trace()
Definition: tracer-real-time.cpp:176
dynamicgraph::command::makeCommandVoid0
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(void)> function, const std::string &docString)
Definition: command-bind.h:51
dynamicgraph::ExceptionTraces
Exceptions raised when an error related to traces happen.
Definition: exception-traces.h:18
dynamicgraph::TracerRealTime
Main class of the tracer real-time plug-in.
Definition: tracer-real-time.h:39
debug.h
dynamicgraph::OutStringStream::full
bool full
Definition: tracer-real-time.h:23
dynamicgraph::TracerRealTime::operator<<
DG_TRACERREALTIME_DLLAPI friend std::ostream & operator<<(std::ostream &os, const TracerRealTime &t)
Definition: tracer-real-time.cpp:302
pool.h
dynamicgraph::OutStringStream::empty
void empty()
Definition: tracer-real-time.cpp:77


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Aug 2 2024 08:36:38