A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
16/**
17 * \file
18 * \ingroup core-examples
19 * \ingroup logging
20 * Example program that demonstrates how to replace the time printer.
21 */
22
23/**
24 * Example program that demonstrates how to replace the time printer.
25 *
26 * This program creates a sample object and schedules some methods to
27 * occur at future times. When run with no arguments, it prints out
28 * something like this:
29 * \code
30 * $ ./ns3 run sample-log-time-format
31 * RandomVariableStream:RandomVariableStream(0x184e3a0)
32 * RandomVariableStream:UniformRandomVariable(0x184e3a0)
33 * RandomVariableStream:SetStream(0x184e3a0, -1)
34 * RandomVariableStream:SetAntithetic(0x184e3a0, 0)
35 * +0.000000001s RandomVariableStream:SetAntithetic(0x184e3a0, 0)
36 * +0.000000123s RandomVariableStream:SetAntithetic(0x184e3a0, 0)
37 * +0.000123456s RandomVariableStream:SetAntithetic(0x184e3a0, 0)
38 * +0.123456789s RandomVariableStream:SetAntithetic(0x184e3a0, 0)
39 * RandomVariableStream:~RandomVariableStream(0x184e3a0)
40 * \endcode
41 *
42 * These statements are printed out because of these two program
43 * statements:
44 *
45 * \code
46 * LogComponentEnable ("RandomVariableStream", LOG_LEVEL_ALL);
47 * LogComponentEnableAll (LOG_PREFIX_TIME);
48 * \endcode
49 *
50 * The first statement enables logging on the methods found in
51 * random-variable-stream.cc. The second prepends a time prefix
52 * to logging statements. Note that only those logging statements
53 * occurring after Simulator::Run () is called have a time prefix;
54 * this is because the time printer is only hooked to the simulator
55 * once the simulator object is instantiated (after Simulator::Run ()
56 * is called).
57 *
58 * To change the format, one can schedule (at simulation time 0) a
59 * replacement function for printing time. This can be demonstrated
60 * by setting the 'replace-time-printer' parameter to true:
61 * \code
62 * ./ns3 run 'sample-log-time-format --replaceTimePrinter=1'
63 * RandomVariableStream:RandomVariableStream(0x15fb080)
64 * RandomVariableStream:UniformRandomVariable(0x15fb080)
65 * RandomVariableStream:SetStream(0x15fb080, -1)
66 * RandomVariableStream:SetAntithetic(0x15fb080, 0)
67 * Replacing time printer function after Simulator::Run ()
68 * 1e-09s RandomVariableStream:SetAntithetic(0x15fb080, 0)
69 * 1.23e-07s RandomVariableStream:SetAntithetic(0x15fb080, 0)
70 * 0.000123456s RandomVariableStream:SetAntithetic(0x15fb080, 0)
71 * 0.123457s RandomVariableStream:SetAntithetic(0x15fb080, 0)
72 * RandomVariableStream:~RandomVariableStream(0x15fb080)
73 * \endcode
74 *
75 * In the above, the default C++ iostream precision is instead used
76 * (which was the default for ns-3 versions 3.26 and earlier).
77 *
78 * In addition, the 'resolution' program argument allows one to experiment
79 * with changing ns-3's time resolution from its default of Time::NS, such
80 * as Time::PS or Time::FS; the precision changes accordingly.
81 *
82 * The maximum useful precision is 20 decimal digits, since Time is
83 * signed 64 bits.
84 */
85
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
94using namespace ns3;
95
96namespace
97{
98
99/**
100 * Pre-ns-3.26 TimePrinter equivalent (was called LogTimePrinter).
101 *
102 * Prior to ns-3.26, the time printer used default C++ iostream precision
103 * This function sets it back to the format used before ns-3.26
104 *
105 * \param [in] os The stream to print on.
106 */
107void
108ReplacementTimePrinter(std::ostream& os)
109{
110 os << Simulator::Now().GetSeconds() << "s";
111}
112
113/** Set ReplacementTimePrinter as the time printer for log messages. */
114void
116{
117 std::cout << "Replacing time printer function after Simulator::Run ()" << std::endl;
119}
120
121} // unnamed namespace
122
123int
124main(int argc, char* argv[])
125{
126 bool replaceTimePrinter = false;
127 std::string resolution = "Time::NS";
128 LogComponentEnable("RandomVariableStream", LOG_LEVEL_ALL);
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 {
154 }
155
156 Simulator::Schedule(NanoSeconds(1), &UniformRandomVariable::SetAntithetic, uniformRv, false);
157 Simulator::Schedule(NanoSeconds(123), &UniformRandomVariable::SetAntithetic, uniformRv, false);
159 &UniformRandomVariable::SetAntithetic,
160 uniformRv,
161 false);
163 &UniformRandomVariable::SetAntithetic,
164 uniformRv,
165 false);
166
169
170 return 0;
171}
Parse command-line arguments.
Definition: command-line.h:232
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
@ US
microsecond
Definition: nstime.h:118
@ PS
picosecond
Definition: nstime.h:120
@ FS
femtosecond
Definition: nstime.h:121
@ NS
nanosecond
Definition: nstime.h:119
static void SetResolution(Unit resolution)
Definition: time.cc:213
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1362
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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:302
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition: log.cc:492
@ 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:320
ns cmd
Definition: second.py:40