A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
92
#include <map>
93
94
using namespace
ns3
;
95
96
namespace
97
{
98
107
void
108
ReplacementTimePrinter
(std::ostream& os)
109
{
110
os <<
Simulator::Now
().
GetSeconds
() <<
"s"
;
111
}
112
114
void
115
ReplaceTimePrinter
()
116
{
117
std::cout <<
"Replacing time printer function after Simulator::Run ()"
<< std::endl;
118
LogSetTimePrinter
(&
ReplacementTimePrinter
);
119
}
120
121
}
// unnamed namespace
122
123
int
124
main(
int
argc,
char
* argv[])
125
{
126
bool
replaceTimePrinter =
false
;
127
std::string resolution =
"Time::NS"
;
128
LogComponentEnable
(
"RandomVariableStream"
,
LOG_LEVEL_ALL
);
129
LogComponentEnableAll
(
LOG_PREFIX_TIME
);
130
131
std::map<std::string, Time::Unit> resolutionMap = {
132
{
"Time::US"
,
Time::US
},
133
{
"Time::NS"
,
Time::NS
},
134
{
"Time::PS"
,
Time::PS
},
135
{
"Time::FS"
,
Time::FS
},
136
};
137
138
CommandLine
cmd
(__FILE__);
139
cmd
.AddValue(
"replaceTimePrinter"
,
"replace time printing function"
, replaceTimePrinter);
140
cmd
.AddValue(
"resolution"
,
"time resolution"
, resolution);
141
cmd
.Parse(argc, argv);
142
143
auto
search = resolutionMap.find(resolution);
144
if
(search != resolutionMap.end())
145
{
146
Time::SetResolution
(search->second);
147
}
148
149
Ptr<UniformRandomVariable>
uniformRv = CreateObject<UniformRandomVariable>();
150
151
if
(replaceTimePrinter)
152
{
153
Simulator::Schedule
(
Seconds
(0), &
ReplaceTimePrinter
);
154
}
155
156
Simulator::Schedule
(
NanoSeconds
(1), &
UniformRandomVariable::SetAntithetic
, uniformRv,
false
);
157
Simulator::Schedule
(
NanoSeconds
(123), &
UniformRandomVariable::SetAntithetic
, uniformRv,
false
);
158
Simulator::Schedule
(
NanoSeconds
(123456),
159
&
UniformRandomVariable::SetAntithetic
,
160
uniformRv,
161
false
);
162
Simulator::Schedule
(
NanoSeconds
(123456789),
163
&
UniformRandomVariable::SetAntithetic
,
164
uniformRv,
165
false
);
166
167
Simulator::Run
();
168
Simulator::Destroy
();
169
170
return
0;
171
}
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:232
ns3::Ptr< UniformRandomVariable >
ns3::RandomVariableStream::SetAntithetic
void SetAntithetic(bool isAntithetic)
Specify whether antithetic values should be generated.
Definition:
random-variable-stream.cc:92
ns3::Simulator::Schedule
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition:
simulator.h:571
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:142
ns3::Simulator::Now
static Time Now()
Return the current simulation virtual time.
Definition:
simulator.cc:208
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition:
simulator.cc:178
ns3::Time::GetSeconds
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition:
nstime.h:403
ns3::Time::US
@ US
microsecond
Definition:
nstime.h:118
ns3::Time::PS
@ PS
picosecond
Definition:
nstime.h:120
ns3::Time::FS
@ FS
femtosecond
Definition:
nstime.h:121
ns3::Time::NS
@ NS
nanosecond
Definition:
nstime.h:119
ns3::Time::SetResolution
static void SetResolution(Unit resolution)
Definition:
time.cc:213
ns3::NanoSeconds
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1362
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1326
anonymous_namespace{sample-log-time-format.cc}::ReplaceTimePrinter
void ReplaceTimePrinter()
Set ReplacementTimePrinter as the time printer for log messages.
Definition:
sample-log-time-format.cc:115
anonymous_namespace{sample-log-time-format.cc}::ReplacementTimePrinter
void ReplacementTimePrinter(std::ostream &os)
Pre-ns-3.26 TimePrinter equivalent (was called LogTimePrinter).
Definition:
sample-log-time-format.cc:108
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::LogComponentEnable
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition:
log.cc:302
ns3::LogSetTimePrinter
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition:
log.cc:492
ns3::LOG_LEVEL_ALL
@ LOG_LEVEL_ALL
Print everything.
Definition:
log.h:116
ns3::LOG_PREFIX_TIME
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition:
log.h:119
ns3::LogComponentEnableAll
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition:
log.cc:320
second.cmd
cmd
Definition:
second.py:40
src
core
examples
sample-log-time-format.cc
Generated on Sun Mar 3 2024 17:10:55 for ns-3 by
1.9.1