tracer.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>
19 #include <dynamic-graph/tracer.h>
20 #include <dynamic-graph/value.h>
21 
22 #include <boost/bind.hpp>
23 
24 using namespace std;
25 using namespace dynamicgraph;
26 using namespace dynamicgraph::command;
27 
29 
30 /* --------------------------------------------------------------------- */
31 /* --- CLASS ----------------------------------------------------------- */
32 /* --------------------------------------------------------------------- */
33 
34 Tracer::Tracer(const std::string n)
35  : Entity(n),
36  toTraceSignals(),
37  traceStyle(TRACE_STYLE_DEFAULT),
38  frequency(1),
39  basename(),
40  suffix(".dat"),
41  rootdir(),
42  namesSet(false),
43  files(),
44  names(),
45  play(false),
46  timeStart(0),
47  triger("Tracer(" + n + ")::triger") {
49  triger.setFunction(boost::bind(&Tracer::recordTrigger, this, _1, _2));
50 
51  /* --- Commands --- */
52  {
53  using namespace dynamicgraph::command;
54  std::string doc;
55 
56  doc = docCommandVoid2("Add a new signal to trace.", "string (signal name)",
57  "string (filename, empty for default");
58  addCommand("add",
60 
61  doc =
62  docCommandVoid0("Remove all signals. If necessary, close open files.");
63  addCommand("clear",
65 
66  doc = docCommandVoid3(
67  "Gives the args for file opening, and "
68  "if signals have been set, open the corresponding files.",
69  "string (dirname)", "string (prefix)", "string (suffix)");
70  addCommand("open", makeCommandVoid3(*this, &Tracer::openFiles, doc));
71 
72  doc = docCommandVoid0("Close all the open files.");
73  addCommand("close", makeCommandVoid0(*this, &Tracer::closeFiles, doc));
74 
75  doc = docCommandVoid0(
76  "If necessary, dump "
77  "(can be done automatically for some traces type).");
78  addCommand("dump", makeCommandVoid0(*this, &Tracer::trace, doc));
79 
80  doc = docCommandVoid0("Start the tracing process.");
81  addCommand("start", makeCommandVoid0(*this, &Tracer::start, doc));
82 
83  doc = docCommandVoid0("Stop temporarily the tracing process.");
84  addCommand("stop", makeCommandVoid0(*this, &Tracer::stop, doc));
85 
86  addCommand("getTimeStart",
88  docDirectGetter("timeStart", "long int")));
89  addCommand("setTimeStart",
91  docDirectSetter("timeStart", "long int")));
92  } // using namespace command
93 }
94 
95 /* --------------------------------------------------------------------- */
96 /* --------------------------------------------------------------------- */
97 /* --------------------------------------------------------------------- */
98 
100  const string &filename) {
101  dgDEBUGIN(15);
102  // openFile may throw so it should be called first.
103  if (namesSet) openFile(sig, filename);
104  toTraceSignals.push_back(&sig);
105  dgDEBUGF(15, "%p", &sig);
106  names.push_back(filename);
108  dgDEBUGOUT(15);
109 }
110 
111 void Tracer::addSignalToTraceByName(const string &signame,
112  const string &filename) {
113  dgDEBUGIN(15);
114  istringstream iss(signame);
116  addSignalToTrace(sig, filename);
117  dgDEBUGOUT(15);
118 }
119 
125  closeFiles();
126  toTraceSignals.clear();
128 }
129 
130 // void Tracer::
131 // parasite( SignalBase<int>& sig )
132 // {
133 // triger.parasite(sig);
134 // }
135 
136 void Tracer::openFiles(const std::string &rootdir_,
137  const std::string &basename_,
138  const std::string &suffix_) {
139  dgDEBUGIN(15);
140  std::basic_string<char>::size_type n = rootdir_.length();
141  rootdir = rootdir_;
142  if ((0 < n) & ('/' != rootdir[n - 1])) rootdir += '/';
143 
144  basename = basename_;
145  suffix = suffix_;
146 
147  if (files.size()) closeFiles();
148 
149  SignalList::const_iterator iter = toTraceSignals.begin();
150  NameList::const_iterator iterName = names.begin();
151  while (toTraceSignals.end() != iter) {
152  dgDEBUG(15) << "Open <" << (*iter)->getName() << "> in <" << *iterName
153  << ">." << std::endl;
154  openFile(**iter, *iterName);
155  ++iter;
156  ++iterName;
157  }
158 
159  namesSet = true;
160  dgDEBUGOUT(15);
161 }
162 
164  const string &givenname) {
165  dgDEBUGIN(15);
166  string signame;
167  if (givenname.length()) {
168  signame = givenname;
169  } else {
170  signame = sig.shortName();
171  }
172 
173  string filename = rootdir + basename + signame + suffix;
174 
175  dgDEBUG(5) << "Sig <" << sig.getName() << ">: new file " << filename << endl;
176  std::ofstream *newfile = new std::ofstream(filename.c_str());
177  files.push_back(newfile);
178  dgDEBUGOUT(15);
179 }
180 
182  dgDEBUGIN(15);
183  std::lock_guard<std::mutex> files_lock(files_mtx);
184 
185  for (FileList::iterator iter = files.begin(); files.end() != iter; ++iter) {
186  std::ostream *filePtr = *iter;
187  delete filePtr;
188  }
189  files.clear();
190 
191  dgDEBUGOUT(15);
192 }
193 
194 /* --------------------------------------------------------------------- */
195 /* --------------------------------------------------------------------- */
196 /* --------------------------------------------------------------------- */
197 
199  if (!play) {
200  dgDEBUGINOUT(15);
201  return;
202  }
203 
204  dgDEBUGIN(15);
205 
206  // Ensure record() never hangs. If the attempt to acquire the lock fails,
207  // then closeFiles() is active and we shouldn't write to files anyways.
208  std::unique_lock<std::mutex> files_lock(files_mtx, std::try_to_lock);
209  if (!files_lock.owns_lock()) {
210  dgDEBUGOUT(15);
211  return;
212  }
213 
214  if (files.size() != toTraceSignals.size()) {
215  DG_THROW
216  ExceptionTraces(ExceptionTraces::NOT_OPEN, "No files open for tracing",
217  " (file=%d != %d=sig).", files.size(),
218  toTraceSignals.size());
219  }
220 
221  FileList::iterator iterFile = files.begin();
222  SignalList::iterator iterSig = toTraceSignals.begin();
223 
224  while (toTraceSignals.end() != iterSig) {
225  dgDEBUG(45) << "Try..." << endl;
226  recordSignal(**iterFile, **iterSig);
227  ++iterSig;
228  ++iterFile;
229  }
230  dgDEBUGOUT(15);
231 }
232 
233 void Tracer::recordSignal(std::ostream &os, const SignalBase<sigtime_t> &sig) {
234  dgDEBUGIN(15);
235 
236  try {
237  if (sig.getTime() > timeStart) {
238  os << sig.getTime() << "\t";
239  sig.trace(os);
240  os << endl;
241  }
242  } catch (ExceptionAbstract &exc) {
243  os << exc << std::endl;
244  } catch (...) {
245  os << "Unknown error occurred while reading signal." << std::endl;
246  }
247 
248  dgDEBUGOUT(15);
249 }
250 
252  dgDEBUGIN(15) << " time=" << time << endl;
253  record();
254  dgDEBUGOUT(15);
255  return dummy;
256 }
257 
258 void Tracer::trace() {}
259 
260 /* --------------------------------------------------------------------- */
261 /* --------------------------------------------------------------------- */
262 /* --------------------------------------------------------------------- */
263 
264 void Tracer::display(std::ostream &os) const {
265  os << CLASS_NAME << " " << name << " [mode=" << (play ? "play" : "pause")
266  << "] : " << endl
267  << " - Dep list: " << endl;
268  for (SignalList::const_iterator iter = toTraceSignals.begin();
269  toTraceSignals.end() != iter; ++iter) {
270  os << " -> " << (*iter)->getName() << endl;
271  }
272 }
273 
274 std::ostream &operator<<(std::ostream &os, const Tracer &t) {
275  t.display(os);
276  return os;
277 }
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(Tracer, "Tracer")
dynamicgraph::PoolStorage::getSignal
SignalBase< sigtime_t > & getSignal(std::istringstream &sigpath)
Get a signal by name.
Definition: src/dgraph/pool.cpp:203
dynamicgraph::Tracer::closeFiles
virtual void closeFiles()
Definition: tracer.cpp:181
dynamicgraph::Tracer::recordTrigger
sigtime_t & recordTrigger(sigtime_t &dummy, const sigtime_t &time)
Definition: tracer.cpp:251
dynamicgraph::Tracer::addSignalToTraceByName
void addSignalToTraceByName(const std::string &signame, const std::string &filename="")
Definition: tracer.cpp:111
dgDEBUGF
void dgDEBUGF(const int, const char *,...)
Definition: debug.h:169
dynamicgraph::Tracer::trace
virtual void trace()
Definition: tracer.cpp:258
dynamicgraph::Tracer::namesSet
bool namesSet
Definition: tracer.h:50
dynamicgraph
dynamicgraph::Tracer::display
void display(std::ostream &os) const
Display information on the entity inside the output stream os.
Definition: tracer.cpp:264
dynamicgraph::command
Definition: command-bind.h:31
dynamicgraph::Entity
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
Definition: include/dynamic-graph/entity.h:52
dynamicgraph::Tracer::record
void record()
Definition: tracer.cpp:198
dynamicgraph::command::makeCommandVoid3
CommandVoid3< E, T1, T2, T3 > * makeCommandVoid3(E &entity, typename CommandVoid3< E, T1, T2, T3 >::function_t function, const std::string &docString)
Definition: command-bind.h:234
dynamicgraph::Tracer::basename
std::string basename
Definition: tracer.h:47
dgDEBUG
#define dgDEBUG(level)
Definition: debug.h:157
dynamicgraph::Tracer::stop
void stop()
Definition: tracer.h:89
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::Tracer::files_mtx
std::mutex files_mtx
Definition: tracer.h:30
dynamicgraph::Tracer::clearSignalToTrace
void clearSignalToTrace()
Definition: tracer.cpp:124
dynamicgraph::Tracer::addSignalToTrace
void addSignalToTrace(const SignalBase< sigtime_t > &sig, const std::string &filename="")
Definition: tracer.cpp:99
dynamicgraph::Tracer::names
NameList names
Definition: tracer.h:54
dynamicgraph::ExceptionTraces::NOT_OPEN
@ NOT_OPEN
Definition: exception-traces.h:20
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
tracer.h
dynamicgraph::Tracer::suffix
std::string suffix
Definition: tracer.h:48
dynamicgraph::sigtime_t
int64_t sigtime_t
Definition: fwd.hh:12
dynamicgraph::Tracer::rootdir
std::string rootdir
Definition: tracer.h:49
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::SignalTimeDependent::addDependency
virtual void addDependency(const SignalBase< Time > &signal)
Definition: signal-time-dependent.h:136
dynamicgraph::command::docCommandVoid3
std::string docCommandVoid3(const std::string &doc, const std::string &type1, const std::string &type2, const std::string &type3)
Definition: command-bind.h:260
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::command::docDirectSetter
std::string docDirectSetter(const std::string &name, const std::string &type)
Definition: command-direct-setter.h:52
dynamicgraph::size_type
Matrix::Index size_type
Definition: linear-algebra.h:15
dynamicgraph::Tracer::operator<<
DG_TRACER_DLLAPI friend std::ostream & operator<<(std::ostream &os, const Tracer &t)
Definition: tracer.cpp:274
dynamicgraph::command::makeCommandVoid2
CommandVoid2< E, T1, T2 > * makeCommandVoid2(E &entity, boost::function< void(const T1 &, const T2 &)> function, const std::string &docString)
Definition: command-bind.h:167
dynamicgraph::command::docCommandVoid2
std::string docCommandVoid2(const std::string &doc, const std::string &type1, const std::string &type2)
Definition: command-bind.h:194
factory.h
dynamicgraph::Tracer::recordSignal
virtual void recordSignal(std::ostream &os, const SignalBase< sigtime_t > &sig)
Definition: tracer.cpp:233
dynamicgraph::command::docDirectGetter
std::string docDirectGetter(const std::string &name, const std::string &type)
Definition: command-direct-getter.h:49
all-commands.h
dynamicgraph::Tracer::timeStart
sigtime_t timeStart
Definition: tracer.h:56
dynamicgraph::PoolStorage::getInstance
static PoolStorage * getInstance()
Get unique instance of the class.
Definition: src/dgraph/pool.cpp:31
dynamicgraph::SignalTimeDependent::clearDependencies
virtual void clearDependencies()
Definition: signal-time-dependent.h:148
dynamicgraph::command::makeDirectSetter
DirectSetter< E, T > * makeDirectSetter(E &entity, T *ptr, const std::string &docString)
Definition: command-direct-setter.h:47
dynamicgraph::Tracer::openFile
virtual void openFile(const SignalBase< sigtime_t > &sig, const std::string &filename)
Definition: tracer.cpp:163
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
Add a command to Entity.
Definition: src/dgraph/entity.cpp:191
dynamicgraph::Tracer::triger
SignalTimeDependent< sigtime_t, sigtime_t > triger
Definition: tracer.h:93
dynamicgraph::SignalBase< sigtime_t >
dynamicgraph::Tracer::start
void start()
Definition: tracer.h:88
dynamicgraph::Tracer
Tracer plug-in main class.
Definition: tracer.h:24
dgDEBUGINOUT
#define dgDEBUGINOUT(level)
Definition: debug.h:206
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
Definition: src/dgraph/entity.cpp:59
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
value.h
debug.h
dynamicgraph::Tracer::openFiles
void openFiles(const std::string &rootdir, const std::string &basename, const std::string &suffix)
Definition: tracer.cpp:136
pool.h


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