A Discrete-Event Network Simulator
API
sample-log-time-format.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
87 #include "ns3/simulator.h"
88 #include "ns3/nstime.h"
89 #include "ns3/command-line.h"
90 #include "ns3/log.h"
91 #include "ns3/random-variable-stream.h"
92 
93 using namespace ns3;
94 
95 namespace {
96 
105 void
106 ReplacementTimePrinter (std::ostream &os)
107 {
108  os << Simulator::Now ().GetSeconds () << "s";
109 }
110 
112 void
114 {
115  std::cout << "Replacing time printer function after Simulator::Run ()" << std::endl;
117 }
118 
119 
120 } // unnamed namespace
121 
122 
123 int main (int argc, char *argv[])
124 {
125  bool replaceTimePrinter = false;
126  std::string resolution = "Time::NS";
127  LogComponentEnable ("RandomVariableStream", LOG_LEVEL_ALL);
129 
130  std::map<std::string, Time::Unit> resolutionMap = {{"Time::US", Time::US}, {"Time::NS", Time::NS}, {"Time::PS", Time::PS}, {"Time::FS", Time::FS}};
131 
133  cmd.AddValue ("replaceTimePrinter", "replace time printing function", replaceTimePrinter);
134  cmd.AddValue ("resolution", "time resolution", resolution);
135  cmd.Parse (argc, argv);
136 
137  auto search = resolutionMap.find (resolution);
138  if (search != resolutionMap.end ())
139  {
140  Time::SetResolution (search->second);
141  }
142 
143  Ptr<UniformRandomVariable> uniformRv = CreateObject<UniformRandomVariable> ();
144 
145  if (replaceTimePrinter)
146  {
148  }
149 
153  Simulator::Schedule (NanoSeconds (123456789), &UniformRandomVariable::SetAntithetic, uniformRv, false);
154 
155  Simulator::Run ();
157 }
nanosecond
Definition: nstime.h:117
void SetAntithetic(bool isAntithetic)
Specify whether antithetic values should be generated.
microsecond
Definition: nstime.h:116
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
cmd
Definition: second.py:35
picosecond
Definition: nstime.h:118
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1038
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
void ReplacementTimePrinter(std::ostream &os)
Pre-ns-3.26 LogTimePrinter equivalent.
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:393
Prefix all trace prints with simulation time.
Definition: log.h:116
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
void ReplaceTimePrinter(void)
Set ReplacementTimePrinter as the time printer for log messages.
void LogSetTimePrinter(LogTimePrinter printer)
Set the LogTimePrinter function to be used to prepend log messages with the simulation time...
Definition: log.cc:629
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
static void SetResolution(enum Unit resolution)
Definition: time.cc:180
Print everything.
Definition: log.h:113
femtosecond
Definition: nstime.h:119