A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sample-log-time-format.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5/**
6 * \file
7 * \ingroup core-examples
8 * \ingroup logging
9 * Example program that demonstrates how to replace the time printer.
10 */
11
12/**
13 * Example program that demonstrates how to replace the time printer.
14 *
15 * This program creates a sample object and schedules some methods to
16 * occur at future times. When run with no arguments, it prints out
17 * something like this:
18 * \code
19 * $ ./ns3 run sample-log-time-format
20 * RandomVariableStream:RandomVariableStream(0x184e3a0)
21 * RandomVariableStream:UniformRandomVariable(0x184e3a0)
22 * RandomVariableStream:SetStream(0x184e3a0, -1)
23 * RandomVariableStream:SetAntithetic(0x184e3a0, 0)
24 * +0.000000001s RandomVariableStream:SetAntithetic(0x184e3a0, 0)
25 * +0.000000123s RandomVariableStream:SetAntithetic(0x184e3a0, 0)
26 * +0.000123456s RandomVariableStream:SetAntithetic(0x184e3a0, 0)
27 * +0.123456789s RandomVariableStream:SetAntithetic(0x184e3a0, 0)
28 * RandomVariableStream:~RandomVariableStream(0x184e3a0)
29 * \endcode
30 *
31 * These statements are printed out because of these two program
32 * statements:
33 *
34 * \code
35 * LogComponentEnable ("RandomVariableStream", LOG_LEVEL_ALL);
36 * LogComponentEnableAll (LOG_PREFIX_TIME);
37 * \endcode
38 *
39 * The first statement enables logging on the methods found in
40 * random-variable-stream.cc. The second prepends a time prefix
41 * to logging statements. Note that only those logging statements
42 * occurring after Simulator::Run () is called have a time prefix;
43 * this is because the time printer is only hooked to the simulator
44 * once the simulator object is instantiated (after Simulator::Run ()
45 * is called).
46 *
47 * To change the format, one can schedule (at simulation time 0) a
48 * replacement function for printing time. This can be demonstrated
49 * by setting the 'replace-time-printer' parameter to true:
50 * \code
51 * ./ns3 run 'sample-log-time-format --replaceTimePrinter=1'
52 * RandomVariableStream:RandomVariableStream(0x15fb080)
53 * RandomVariableStream:UniformRandomVariable(0x15fb080)
54 * RandomVariableStream:SetStream(0x15fb080, -1)
55 * RandomVariableStream:SetAntithetic(0x15fb080, 0)
56 * Replacing time printer function after Simulator::Run ()
57 * 1e-09s RandomVariableStream:SetAntithetic(0x15fb080, 0)
58 * 1.23e-07s RandomVariableStream:SetAntithetic(0x15fb080, 0)
59 * 0.000123456s RandomVariableStream:SetAntithetic(0x15fb080, 0)
60 * 0.123457s RandomVariableStream:SetAntithetic(0x15fb080, 0)
61 * RandomVariableStream:~RandomVariableStream(0x15fb080)
62 * \endcode
63 *
64 * In the above, the default C++ iostream precision is instead used
65 * (which was the default for ns-3 versions 3.26 and earlier).
66 *
67 * In addition, the 'resolution' program argument allows one to experiment
68 * with changing ns-3's time resolution from its default of Time::NS, such
69 * as Time::PS or Time::FS; the precision changes accordingly.
70 *
71 * The maximum useful precision is 20 decimal digits, since Time is
72 * signed 64 bits.
73 */
74
75#include "ns3/command-line.h"
76#include "ns3/log.h"
77#include "ns3/nstime.h"
78#include "ns3/random-variable-stream.h"
79#include "ns3/simulator.h"
80
81#include <map>
82
83using namespace ns3;
84
85namespace
86{
87
88/**
89 * Pre-ns-3.26 TimePrinter equivalent (was called LogTimePrinter).
90 *
91 * Prior to ns-3.26, the time printer used default C++ iostream precision
92 * This function sets it back to the format used before ns-3.26
93 *
94 * \param [in] os The stream to print on.
95 */
96void
97ReplacementTimePrinter(std::ostream& os)
98{
99 os << Simulator::Now().GetSeconds() << "s";
100}
101
102/** Set ReplacementTimePrinter as the time printer for log messages. */
103void
105{
106 std::cout << "Replacing time printer function after Simulator::Run ()" << std::endl;
108}
109
110} // unnamed namespace
111
112int
113main(int argc, char* argv[])
114{
115 bool replaceTimePrinter = false;
116 std::string resolution = "Time::NS";
117 LogComponentEnable("RandomVariableStream", LOG_LEVEL_ALL);
119
120 std::map<std::string, Time::Unit> resolutionMap = {
121 {"Time::US", Time::US},
122 {"Time::NS", Time::NS},
123 {"Time::PS", Time::PS},
124 {"Time::FS", Time::FS},
125 };
126
127 CommandLine cmd(__FILE__);
128 cmd.AddValue("replaceTimePrinter", "replace time printing function", replaceTimePrinter);
129 cmd.AddValue("resolution", "time resolution", resolution);
130 cmd.Parse(argc, argv);
131
132 auto search = resolutionMap.find(resolution);
133 if (search != resolutionMap.end())
134 {
135 Time::SetResolution(search->second);
136 }
137
139
140 if (replaceTimePrinter)
141 {
143 }
144
145 Simulator::Schedule(NanoSeconds(1), &UniformRandomVariable::SetAntithetic, uniformRv, false);
146 Simulator::Schedule(NanoSeconds(123), &UniformRandomVariable::SetAntithetic, uniformRv, false);
148 &UniformRandomVariable::SetAntithetic,
149 uniformRv,
150 false);
152 &UniformRandomVariable::SetAntithetic,
153 uniformRv,
154 false);
155
158
159 return 0;
160}
Parse command-line arguments.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
@ US
microsecond
Definition nstime.h:107
@ PS
picosecond
Definition nstime.h:109
@ FS
femtosecond
Definition nstime.h:110
@ NS
nanosecond
Definition nstime.h:108
static void SetResolution(Unit resolution)
Definition time.cc:202
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:291
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition log.cc:481
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309