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<int> &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 }
std::string rootdir
Definition: tracer.h:49
TracerRealTime(const std::string &n)
std::string docDirectGetter(const std::string &name, const std::string &type)
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(void)> function, const std::string &docString)
Definition: command-bind.h:51
std::string basename
Definition: tracer.h:47
DG_TRACERREALTIME_DLLAPI friend std::ostream & operator<<(std::ostream &os, const TracerRealTime &t)
bool addData(const char *data, const std::streamoff &size)
DirectGetter< E, T > * makeDirectGetter(E &entity, T *ptr, const std::string &docString)
#define dgDEBUGOUT(level)
Definition: debug.h:204
Stream for the tracer real-time.
#define dgDEBUGINOUT(level)
Definition: debug.h:206
data
Definition: setup.in.py:48
dynamicgraph::SignalArray_const< double > sig
Definition: signal-all.cpp:25
Main class of the tracer real-time plug-in.
virtual void openFile(const SignalBase< int > &sig, const std::string &filename)
std::string docCommandVoid0(const std::string &doc)
Definition: command-bind.h:70
void dump(std::ostream &os)
const std::string & getName() const
Definition: signal-base.h:42
std::string shortName() const
Definition: signal-base.h:159
#define dgDEBUG(level)
Definition: debug.h:157
void resize(const std::streamsize &size)
Abstract root class for all dynamic-graph exceptions.
FileList files
Definition: tracer.h:52
void display(std::ostream &os) const
Display information on the entity inside the output stream os.
Tracer plug-in main class.
Definition: tracer.h:24
SignalList toTraceSignals
Definition: tracer.h:29
#define DG_THROW
std::string suffix
Definition: tracer.h:48
virtual void recordSignal(std::ostream &os, const SignalBase< int > &sig)
std::mutex files_mtx
Definition: tracer.h:30
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(TracerRealTime, "TracerRealTime")
Exceptions raised when an error related to traces happen.
void addCommand(const std::string &name, command::Command *command)
Add a command to Entity.
virtual void recordSignal(std::ostream &os, const SignalBase< int > &sig)
Definition: tracer.cpp:232
std::string docDirectSetter(const std::string &name, const std::string &type)
#define dgDEBUGIN(level)
VP_DEBUG.
Definition: debug.h:202
DirectSetter< E, T > * makeDirectSetter(E &entity, T *ptr, const std::string &docString)


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03