View | Details | Raw Unified | Return to bug 2490
Collapse All | Expand All

(-)a/CHANGES.html (+5 lines)
 Lines 67-72    Link Here 
67
    Changing the Mean attribute has no more an effect on the distribution.
67
    Changing the Mean attribute has no more an effect on the distribution.
68
    See the documentation for the relationship between Mean, Scale and Shape. 
68
    See the documentation for the relationship between Mean, Scale and Shape. 
69
</li>
69
</li>
70
<li>The default logging timestamp precision has been changed from 6 digits
71
    to 9 digits, with a fixed format to ensure that 9 digits to the right of
72
    the decimal point are always printed.  Previously, default C++ iostream
73
    precision and formatting was used.
74
</li>
70
</ul>
75
</ul>
71
<h2>Changes to build system:</h2>
76
<h2>Changes to build system:</h2>
72
<ul>
77
<ul>
(-)a/doc/manual/source/logging.rst (+28 lines)
 Lines 342-347    Link Here 
342
342
343
2. Add logging statements (macro calls) to your functions and function bodies.
343
2. Add logging statements (macro calls) to your functions and function bodies.
344
344
345
Controlling timestamp precision
346
*******************************
347
348
Timestamps are printed out in units of seconds.  When used with the default
349
|ns3| time resolution of nanoseconds, the default timestamp precision is 9 
350
digits, with fixed format, to allow for 9 digits to be consistently printed 
351
to the right of the decimal point.  Example:
352
353
::
354
355
  +0.000123456s RandomVariableStream:SetAntithetic(0x805040, 0)
356
357
When the |ns3| simulation uses higher time resolution such as picoseconds
358
or femtoseconds, the precision is expanded accordingly; e.g. for picosecond:
359
360
::
361
362
  +0.000123456789s RandomVariableStream:SetAntithetic(0x805040, 0)
363
364
When the |ns3| simulation uses a time resolution lower than microseconds,
365
the default C++ precision of 5 is used.
366
 
367
An example program at ``src\core\examples\sample-log-time-format.cc``
368
demonstrates how to change the timestamp formatting.
369
370
The maximum useful precision is 20 decimal digits, since Time is signed 64 
371
bits.
372
345
Logging Macros
373
Logging Macros
346
==============
374
==============
347
375
(-)b7b9ef0b1842 (+138 lines)
Added Link Here 
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
 * In addition, the 'resolution' program argument allows one to experiment
73
 * with changing ns-3's time resolution from its default of Time::NS, such 
74
 * as Time::PS or Time::FS; the precision changes accordingly.
75
 *
76
 * The maximum useful precision is 20 decimal digits, since Time is 
77
 * signed 64 bits.
78
 */
79
80
#include "ns3/simulator.h"
81
#include "ns3/nstime.h"
82
#include "ns3/command-line.h"
83
#include "ns3/log.h"
84
#include "ns3/random-variable-stream.h"
85
86
using namespace ns3;
87
88
// Prior to ns-3.26, the time printer used default C++ iostream precision
89
// This function sets it back to the format used before ns-3.26
90
void
91
ReplacementTimePrinter (std::ostream &os)
92
{
93
  os << Simulator::Now ().GetSeconds () << "s";
94
}
95
96
void
97
ReplaceTimePrinter (void)
98
{
99
  std::cout << "Replacing time printer function after Simulator::Run ()" << std::endl;
100
  LogSetTimePrinter (&ReplacementTimePrinter);
101
}
102
103
104
int main (int argc, char *argv[])
105
{
106
  bool replaceTimePrinter = false;
107
  std::string resolution = "Time::NS";
108
  LogComponentEnable ("RandomVariableStream", LOG_LEVEL_ALL);
109
  LogComponentEnableAll (LOG_PREFIX_TIME);
110
111
  std::map<std::string, Time::Unit> resolutionMap = {{"Time::US", Time::US}, {"Time::NS", Time::NS}, {"Time::PS", Time::PS}, {"Time::FS", Time::FS}};
112
113
  CommandLine cmd;
114
  cmd.AddValue ("replaceTimePrinter", "replace time printing function", replaceTimePrinter);
115
  cmd.AddValue ("resolution", "time resolution", resolution);
116
  cmd.Parse (argc, argv);
117
118
  auto search = resolutionMap.find (resolution);
119
  if (search != resolutionMap.end ())
120
    {
121
      Time::SetResolution (search->second);
122
    }
123
124
  Ptr<UniformRandomVariable> uniformRv = CreateObject<UniformRandomVariable> ();
125
126
  if (replaceTimePrinter)
127
    {
128
      Simulator::Schedule (Seconds (0), &ReplaceTimePrinter);
129
    }
130
131
  Simulator::Schedule (NanoSeconds (1), &UniformRandomVariable::SetAntithetic, uniformRv, false);
132
  Simulator::Schedule (NanoSeconds (123), &UniformRandomVariable::SetAntithetic, uniformRv, false);
133
  Simulator::Schedule (NanoSeconds (123456), &UniformRandomVariable::SetAntithetic, uniformRv, false);
134
  Simulator::Schedule (NanoSeconds (123456789), &UniformRandomVariable::SetAntithetic, uniformRv, false);
135
136
  Simulator::Run ();
137
  Simulator::Destroy ();
138
}
(-)a/src/core/examples/wscript (+3 lines)
 Lines 37-42    Link Here 
37
                                 ['core'])
37
                                 ['core'])
38
    obj.source = 'hash-example.cc'
38
    obj.source = 'hash-example.cc'
39
39
40
    obj = bld.create_ns3_program('sample-log-time-format', ['core'])
41
    obj.source = 'sample-log-time-format.cc'
42
40
    obj = bld.create_ns3_program('test-string-value-formatting', ['core'])
43
    obj = bld.create_ns3_program('test-string-value-formatting', ['core'])
41
    obj.source = 'test-string-value-formatting.cc'
44
    obj.source = 'test-string-value-formatting.cc'
42
45
(-)a/src/core/model/simulator.cc (-1 / +26 lines)
 Lines 37-42    Link Here 
37
#include <list>
37
#include <list>
38
#include <vector>
38
#include <vector>
39
#include <iostream>
39
#include <iostream>
40
#include <iomanip>
40
41
41
/**
42
/**
42
 * \file
43
 * \file
 Lines 85-91    Link Here 
85
static void
86
static void
86
TimePrinter (std::ostream &os)
87
TimePrinter (std::ostream &os)
87
{
88
{
88
  os << Simulator::Now ().GetSeconds () << "s";
89
  std::ios_base::fmtflags ff = os.flags (); // Save stream flags
90
  std::streamsize oldPrecision = os.precision ();
91
  if (Time::GetResolution () == Time::NS)
92
    {
93
      os << std::fixed << std::setprecision (9) << Simulator::Now ().As (Time::S);
94
    }
95
  else if (Time::GetResolution () == Time::PS) 
96
    {
97
      os << std::fixed << std::setprecision (12) << Simulator::Now ().As (Time::S);
98
    }
99
  else if (Time::GetResolution () == Time::FS) 
100
    {
101
      os << std::fixed << std::setprecision (15) << Simulator::Now ().As (Time::S);
102
    }
103
  else if (Time::GetResolution () == Time::US) 
104
    {
105
      os << std::fixed << std::setprecision (6) << Simulator::Now ().As (Time::S);
106
    }
107
  else
108
    {
109
      // default C++ precision of 5
110
      os << std::fixed << std::setprecision (5) << Simulator::Now ().As (Time::S);
111
    }
112
  os << std::setprecision (oldPrecision);
113
  os.flags (ff); // Restore stream flags
89
}
114
}
90
115
91
/**
116
/**

Return to bug 2490