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
93using namespace ns3;
94
95namespace {
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
120} // unnamed namespace
121
122
123int 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
132 CommandLine cmd (__FILE__);
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 {
147 Simulator::Schedule (Seconds (0), &ReplaceTimePrinter);
148 }
149
150 Simulator::Schedule (NanoSeconds (1), &UniformRandomVariable::SetAntithetic, uniformRv, false);
151 Simulator::Schedule (NanoSeconds (123), &UniformRandomVariable::SetAntithetic, uniformRv, false);
152 Simulator::Schedule (NanoSeconds (123456), &UniformRandomVariable::SetAntithetic, uniformRv, false);
153 Simulator::Schedule (NanoSeconds (123456789), &UniformRandomVariable::SetAntithetic, uniformRv, false);
154
155 Simulator::Run ();
156 Simulator::Destroy ();
157}
Parse command-line arguments.
Definition: command-line.h:229
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1268
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
void ReplaceTimePrinter(void)
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 LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition: log.cc:620
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:385
cmd
Definition: second.py:35