A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
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
113
ReplaceTimePrinter
(
void
)
114
{
115
std::cout <<
"Replacing time printer function after Simulator::Run ()"
<< std::endl;
116
LogSetTimePrinter
(&
ReplacementTimePrinter
);
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
);
128
LogComponentEnableAll
(
LOG_PREFIX_TIME
);
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
}
ns3::LogSetTimePrinter
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition:
log.cc:620
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:228
ns3::RandomVariableStream::SetAntithetic
void SetAntithetic(bool isAntithetic)
Specify whether antithetic values should be generated.
Definition:
random-variable-stream.cc:88
ns3::Time::NS
@ NS
nanosecond
Definition:
nstime.h:118
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition:
simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::LOG_LEVEL_ALL
@ LOG_LEVEL_ALL
Print everything.
Definition:
log.h:116
ns3::Time::US
@ US
microsecond
Definition:
nstime.h:117
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition:
log.cc:361
ns3::LogComponentEnableAll
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition:
log.cc:385
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition:
simulator.h:557
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:106
ns3::Ptr< UniformRandomVariable >
ns3::Time::FS
@ FS
femtosecond
Definition:
nstime.h:120
ns3::NanoSeconds
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1313
ns3::Time::PS
@ PS
picosecond
Definition:
nstime.h:119
second.cmd
cmd
Definition:
second.py:35
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition:
simulator.cc:172
anonymous_namespace{sample-log-time-format.cc}::ReplaceTimePrinter
void ReplaceTimePrinter(void)
Set ReplacementTimePrinter as the time printer for log messages.
Definition:
sample-log-time-format.cc:113
ns3::LOG_PREFIX_TIME
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition:
log.h:119
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1289
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:136
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition:
nstime.h:380
ns3::Time::SetResolution
static void SetResolution(enum Unit resolution)
Definition:
time.cc:223
src
core
examples
sample-log-time-format.cc
Generated on Fri Oct 1 2021 17:02:56 for ns-3 by
1.8.20