2222#include " ns3/core-module.h"
2323
2424#include < chrono>
25+ #include < optional>
2526#include < string>
2627#include < thread>
2728
@@ -103,18 +104,21 @@ main(int argc, char** argv)
103104 Time interval = Seconds (10 );
104105 Time wait = MilliSeconds (10 );
105106 bool verbose = false ;
107+ bool showProgress = true ;
106108
107109 CommandLine cmd (__FILE__);
108110 cmd.AddValue (" stop" , " Simulation duration in virtual time." , stop);
109111 cmd.AddValue (" interval" , " Approximate reporting interval, in wall clock time." , interval);
110112 cmd.AddValue (" wait" , " Wallclock time to burn on each event." , wait);
111113 cmd.AddValue (" verbose" , " Turn on verbose progress message." , verbose);
114+ cmd.AddValue (" showProgress" , " Show progress reporting." , showProgress);
112115 cmd.Parse (argc, argv);
113116
114117 std::cout << " \n "
115118 << cmd.GetName () << " :\n "
116119 << " \n "
117120 << " verbose progress message: " << (verbose ? " on\n " : " off\n " )
121+ << " progress reporting: " << (showProgress ? " on\n " : " off\n " )
118122 << " target reporting interval: " << interval.As (Time::S) << " \n "
119123 << " average event sleep time: " << wait.As (Time::MS ) << " \n "
120124 << " total simulation run time: " << stop.As (Time::S) << std::endl;
@@ -123,8 +127,27 @@ main(int argc, char** argv)
123127 h->Event ();
124128
125129 Simulator::Stop (stop);
126- ShowProgress spinner (interval);
127- spinner.SetVerbose (verbose);
130+
131+ // ShowProgress begins reporting when constructed; use std::optional
132+ // to construct it conditionally while keeping it alive through
133+ // Simulator::Run(). Here, the "showProgress" command-line argument
134+ // controls whether ShowProgress runs or not, and the "verbose"
135+ // command-line argument controls the verbosity of ShowProgress's output.
136+ std::optional<ShowProgress> spinner;
137+ if (showProgress)
138+ {
139+ spinner.emplace (interval);
140+ // Note that in the above statement, by default, ShowProgress
141+ // writes to std::cout. You can redirect its output to std::cerr
142+ // by passing "std::cerr" as a second argument in the above.
143+ spinner->SetVerbose (verbose);
144+ }
145+
146+ // Note that if you simply want to enable ShowProgress unconditionally
147+ // in your program, you can simply write, without the std::optional:
148+ //
149+ // ShowProgress spinner(interval);
150+ // spinner.SetVerbose(verbose);
128151
129152 Simulator::Run ();
130153 Simulator::Destroy ();
0 commit comments