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 (+18 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.  The default timestamp 
349
precision is 9 digits (nanosecond precision), with fixed format, to allow
350
for 9 digits to be consistently printed to the right of the decimal
351
point.  Example:
352
353
::
354
355
  +0.000123456s RandomVariableStream:SetAntithetic(0x805040, 0)
356
 
357
An example program at ``src\core\examples\sample-log-time-format.cc``
358
demonstrates how to change the timestamp formatting.
359
360
The maximum useful precision is 20 decimal digits, since Time is signed 64 
361
bits.
362
345
Logging Macros
363
Logging Macros
346
==============
364
==============
347
365
(-)b7b9ef0b1842 (+123 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
 * 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
}
(-)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 / +6 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
  os << std::fixed << std::setprecision (9) << Simulator::Now ().As (Time::S);
92
  os << std::setprecision (oldPrecision);
93
  os.flags (ff); // Restore stream flags
89
}
94
}
90
95
91
/**
96
/**

Return to bug 2490