utils/tracers: Modifying (simplifying) interface for ndnSIM tracers
Now most of the tracers internally keep created instances in a global
variable. From now on, to start tracing, one needs just install a
necessary tracer, like this:
ndn::L3RateTracer::InstallAll ("rate-trace.txt", Seconds (0.5));
Because of the change, it became possible to use the same tracers inside
python scripts.
Also, this commits contains corresponding documentation updates.
diff --git a/utils/tracers/l2-rate-tracer.cc b/utils/tracers/l2-rate-tracer.cc
index cf4af72..48d2589 100644
--- a/utils/tracers/l2-rate-tracer.cc
+++ b/utils/tracers/l2-rate-tracer.cc
@@ -38,15 +38,36 @@
namespace ns3 {
-boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer> > >
+static std::list< boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer> > > > g_tracers;
+
+template<class T>
+static inline void
+NullDeleter (T *ptr)
+{
+}
+
+void
L2RateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
{
std::list<Ptr<L2RateTracer> > tracers;
- boost::shared_ptr<std::ofstream> outputStream (new std::ofstream ());
- outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
+ boost::shared_ptr<std::ostream> outputStream;
+ if (file != "-")
+ {
+ boost::shared_ptr<std::ofstream> os (new std::ofstream ());
+ os->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
- if (!outputStream->is_open ())
- return boost::make_tuple (outputStream, tracers);
+ if (!os->is_open ())
+ {
+ NS_LOG_ERROR ("File " << file << " cannot be opened for writing. Tracing disabled");
+ return;
+ }
+
+ outputStream = os;
+ }
+ else
+ {
+ outputStream = boost::shared_ptr<std::ostream> (&std::cout, NullDeleter<std::ostream>);
+ }
for (NodeList::Iterator node = NodeList::Begin ();
node != NodeList::End ();
@@ -66,7 +87,7 @@
*outputStream << "\n";
}
- return boost::make_tuple (outputStream, tracers);
+ g_tracers.push_back (boost::make_tuple (outputStream, tracers));
}