A Discrete-Event Network Simulator
API
sample-log-time-format.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
86#include "ns3/command-line.h"
87#include "ns3/log.h"
88#include "ns3/nstime.h"
89#include "ns3/random-variable-stream.h"
90#include "ns3/simulator.h"
91
92using namespace ns3;
93
94namespace
95{
96
105void
106ReplacementTimePrinter(std::ostream& os)
107{
108 os << Simulator::Now().GetSeconds() << "s";
109}
110
112void
114{
115 std::cout << "Replacing time printer function after Simulator::Run ()" << std::endl;
117}
118
119} // unnamed namespace
120
121int
122main(int argc, char* argv[])
123{
124 bool replaceTimePrinter = false;
125 std::string resolution = "Time::NS";
126 LogComponentEnable("RandomVariableStream", LOG_LEVEL_ALL);
128
129 std::map<std::string, Time::Unit> resolutionMap = {
130 {"Time::US", Time::US},
131 {"Time::NS", Time::NS},
132 {"Time::PS", Time::PS},
133 {"Time::FS", Time::FS},
134 };
135
136 CommandLine cmd(__FILE__);
137 cmd.AddValue("replaceTimePrinter", "replace time printing function", replaceTimePrinter);
138 cmd.AddValue("resolution", "time resolution", resolution);
139 cmd.Parse(argc, argv);
140
141 auto search = resolutionMap.find(resolution);
142 if (search != resolutionMap.end())
143 {
144 Time::SetResolution(search->second);
145 }
146
147 Ptr<UniformRandomVariable> uniformRv = CreateObject<UniformRandomVariable>();
148
149 if (replaceTimePrinter)
150 {
151 Simulator::Schedule(Seconds(0), &ReplaceTimePrinter);
152 }
153
154 Simulator::Schedule(NanoSeconds(1), &UniformRandomVariable::SetAntithetic, uniformRv, false);
155 Simulator::Schedule(NanoSeconds(123), &UniformRandomVariable::SetAntithetic, uniformRv, false);
156 Simulator::Schedule(NanoSeconds(123456),
157 &UniformRandomVariable::SetAntithetic,
158 uniformRv,
159 false);
160 Simulator::Schedule(NanoSeconds(123456789),
161 &UniformRandomVariable::SetAntithetic,
162 uniformRv,
163 false);
164
165 Simulator::Run();
166 Simulator::Destroy();
167
168 return 0;
169}
Parse command-line arguments.
Definition: command-line.h:232
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1372
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
void ReplaceTimePrinter()
Set ReplacementTimePrinter as the time printer for log messages.
void ReplacementTimePrinter(std::ostream &os)
Pre-ns-3.26 TimePrinter equivalent (was called LogTimePrinter).
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:305
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition: log.cc:512
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:329
cmd
Definition: second.py:33