|
|
| 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 |
|
| 17 |
/** |
| 18 |
* Example program that demonstrates how to replace the time printer. |
| 19 |
* |
| 20 |
* This program creates a sample object and schedules some methods to |
| 21 |
* occur at future times. When run with no arguments, it prints out |
| 22 |
* something like this: |
| 23 |
* \code |
| 24 |
* $ ./waf --run sample-log-time-format |
| 25 |
* RandomVariableStream:RandomVariableStream(0x184e3a0) |
| 26 |
* RandomVariableStream:UniformRandomVariable(0x184e3a0) |
| 27 |
* RandomVariableStream:SetStream(0x184e3a0, -1) |
| 28 |
* RandomVariableStream:SetAntithetic(0x184e3a0, 0) |
| 29 |
* +0.000000001s RandomVariableStream:SetAntithetic(0x184e3a0, 0) |
| 30 |
* +0.000000123s RandomVariableStream:SetAntithetic(0x184e3a0, 0) |
| 31 |
* +0.000123456s RandomVariableStream:SetAntithetic(0x184e3a0, 0) |
| 32 |
* +0.123456789s RandomVariableStream:SetAntithetic(0x184e3a0, 0) |
| 33 |
* RandomVariableStream:~RandomVariableStream(0x184e3a0) |
| 34 |
* \endcode |
| 35 |
* |
| 36 |
* These statements are printed out because of these two program |
| 37 |
* statements: |
| 38 |
* |
| 39 |
* \code |
| 40 |
* LogComponentEnable ("RandomVariableStream", LOG_LEVEL_ALL); |
| 41 |
* LogComponentEnableAll (LOG_PREFIX_TIME); |
| 42 |
* \endcode |
| 43 |
* |
| 44 |
* The first statement enables logging on the methods found in |
| 45 |
* random-variable-stream.cc. The second prepends a time prefix |
| 46 |
* to logging statements. Note that only those logging statements |
| 47 |
* occurring after Simulator::Run () is called have a time prefix; |
| 48 |
* this is because the time printer is only hooked to the simulator |
| 49 |
* once the simulator object is instantiated (after Simulator::Run () |
| 50 |
* is called). |
| 51 |
* |
| 52 |
* To change the format, one can schedule (at simulation time 0) a |
| 53 |
* replacement function for printing time. This can be demonstrated |
| 54 |
* by setting the 'replace-time-printer' parameter to true: |
| 55 |
* \code |
| 56 |
* ./waf --run 'sample-log-time-format --replaceTimePrinter=1' |
| 57 |
* RandomVariableStream:RandomVariableStream(0x15fb080) |
| 58 |
* RandomVariableStream:UniformRandomVariable(0x15fb080) |
| 59 |
* RandomVariableStream:SetStream(0x15fb080, -1) |
| 60 |
* RandomVariableStream:SetAntithetic(0x15fb080, 0) |
| 61 |
* Replacing time printer function after Simulator::Run () |
| 62 |
* 1e-09s RandomVariableStream:SetAntithetic(0x15fb080, 0) |
| 63 |
* 1.23e-07s RandomVariableStream:SetAntithetic(0x15fb080, 0) |
| 64 |
* 0.000123456s RandomVariableStream:SetAntithetic(0x15fb080, 0) |
| 65 |
* 0.123457s RandomVariableStream:SetAntithetic(0x15fb080, 0) |
| 66 |
* RandomVariableStream:~RandomVariableStream(0x15fb080) |
| 67 |
* \endcode |
| 68 |
* |
| 69 |
* In the above, the default C++ iostream precision is instead used |
| 70 |
* (which was the default for ns-3 versions 3.26 and earlier). |
| 71 |
* |
| 72 |
* The maximum useful precision is 20 decimal digits, since Time is |
| 73 |
* signed 64 bits. |
| 74 |
*/ |
| 75 |
|
| 76 |
#include "ns3/simulator.h" |
| 77 |
#include "ns3/nstime.h" |
| 78 |
#include "ns3/command-line.h" |
| 79 |
#include "ns3/log.h" |
| 80 |
#include "ns3/random-variable-stream.h" |
| 81 |
|
| 82 |
using namespace ns3; |
| 83 |
|
| 84 |
// Prior to ns-3.26, the time printer used default C++ iostream precision |
| 85 |
// This function sets it back to the format used before ns-3.26 |
| 86 |
void |
| 87 |
ReplacementTimePrinter (std::ostream &os) |
| 88 |
{ |
| 89 |
os << Simulator::Now ().GetSeconds () << "s"; |
| 90 |
} |
| 91 |
|
| 92 |
void |
| 93 |
ReplaceTimePrinter (void) |
| 94 |
{ |
| 95 |
std::cout << "Replacing time printer function after Simulator::Run ()" << std::endl; |
| 96 |
LogSetTimePrinter (&ReplacementTimePrinter); |
| 97 |
} |
| 98 |
|
| 99 |
int main (int argc, char *argv[]) |
| 100 |
{ |
| 101 |
bool replaceTimePrinter = false; |
| 102 |
LogComponentEnable ("RandomVariableStream", LOG_LEVEL_ALL); |
| 103 |
LogComponentEnableAll (LOG_PREFIX_TIME); |
| 104 |
|
| 105 |
CommandLine cmd; |
| 106 |
cmd.AddValue ("replaceTimePrinter", "replace time printing function", replaceTimePrinter); |
| 107 |
cmd.Parse (argc, argv); |
| 108 |
|
| 109 |
Ptr<UniformRandomVariable> uniformRv = CreateObject<UniformRandomVariable> (); |
| 110 |
|
| 111 |
if (replaceTimePrinter) |
| 112 |
{ |
| 113 |
Simulator::Schedule (Seconds (0), &ReplaceTimePrinter); |
| 114 |
} |
| 115 |
|
| 116 |
Simulator::Schedule (NanoSeconds (1), &UniformRandomVariable::SetAntithetic, uniformRv, false); |
| 117 |
Simulator::Schedule (NanoSeconds (123), &UniformRandomVariable::SetAntithetic, uniformRv, false); |
| 118 |
Simulator::Schedule (NanoSeconds (123456), &UniformRandomVariable::SetAntithetic, uniformRv, false); |
| 119 |
Simulator::Schedule (NanoSeconds (123456789), &UniformRandomVariable::SetAntithetic, uniformRv, false); |
| 120 |
|
| 121 |
Simulator::Run (); |
| 122 |
Simulator::Destroy (); |
| 123 |
} |