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

(-)a/CHANGES.html (+5 lines)
 Lines 70-75    Link Here 
70
    See <a href=https://www.nsnam.org/bugzilla/show_bug.cgi?id=2467>bug 2467</a>
70
    See <a href=https://www.nsnam.org/bugzilla/show_bug.cgi?id=2467>bug 2467</a>
71
    for discussion.
71
    for discussion.
72
</li>
72
</li>
73
<li>The default logging timestamp precision has been changed from 6 digits
74
    to 9 digits, with a fixed format to ensure that 9 digits to the right of
75
    the decimal point are always printed.  Previously, default C++ iostream
76
    precision and formatting was used.
77
</li>
73
</ul>
78
</ul>
74
79
75
<hr>
80
<hr>
(-)a/doc/manual/source/logging.rst (+14 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
The default timestamp precision is 9 digits, with fixed format, to allow
349
for 9 digits to be consistently printed to the right of the decimal
350
point.  Example:
351
352
::
353
354
  0.000123456s RandomVariableStream:SetAntithetic(0x805040, 0)
355
 
356
An example program at ``src\core\examples\sample-log-time-format.cc``
357
demonstrates how to change the timestamp formatting.
358
345
Logging Macros
359
Logging Macros
346
==============
360
==============
347
361
(-)90d7a2f727b5 (+120 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(0x805040)
26
 * RandomVariableStream:UniformRandomVariable(0x805040)
27
 * RandomVariableStream:SetStream(0x805040, -1)
28
 * RandomVariableStream:SetAntithetic(0x805040, 0)
29
 * 0.000000001s RandomVariableStream:SetAntithetic(0x805040, 0)
30
 * 0.000000123s RandomVariableStream:SetAntithetic(0x805040, 0)
31
 * 0.000123456s RandomVariableStream:SetAntithetic(0x805040, 0)
32
 * 0.123456789s RandomVariableStream:SetAntithetic(0x805040, 0)
33
 * RandomVariableStream:~RandomVariableStream(0x805040)
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
73
#include "ns3/simulator.h"
74
#include "ns3/nstime.h"
75
#include "ns3/command-line.h"
76
#include "ns3/log.h"
77
#include "ns3/random-variable-stream.h"
78
79
using namespace ns3;
80
81
// Prior to ns-3.26, the time printer used default C++ iostream precision
82
// This function sets it back to the format used before ns-3.26
83
void
84
ReplacementTimePrinter (std::ostream &os)
85
{
86
  os << Simulator::Now ().GetSeconds () << "s";
87
}
88
89
void
90
ReplaceTimePrinter (void)
91
{
92
  std::cout << "Replacing time printer function after Simulator::Run ()" << std::endl;
93
  LogSetTimePrinter (&ReplacementTimePrinter);
94
}
95
96
int main (int argc, char *argv[])
97
{
98
  bool replaceTimePrinter = false;
99
  LogComponentEnable ("RandomVariableStream", LOG_LEVEL_ALL);
100
  LogComponentEnableAll (LOG_PREFIX_TIME);
101
102
  CommandLine cmd;
103
  cmd.AddValue ("replaceTimePrinter", "replace time printing function", replaceTimePrinter);
104
  cmd.Parse (argc, argv);
105
106
  Ptr<UniformRandomVariable> uniformRv = CreateObject<UniformRandomVariable> ();
107
108
  if (replaceTimePrinter)
109
    {
110
      Simulator::Schedule (Seconds (0), &ReplaceTimePrinter);
111
    }
112
113
  Simulator::Schedule (NanoSeconds (1), &UniformRandomVariable::SetAntithetic, uniformRv, false);
114
  Simulator::Schedule (NanoSeconds (123), &UniformRandomVariable::SetAntithetic, uniformRv, false);
115
  Simulator::Schedule (NanoSeconds (123456), &UniformRandomVariable::SetAntithetic, uniformRv, false);
116
  Simulator::Schedule (NanoSeconds (123456789), &UniformRandomVariable::SetAntithetic, uniformRv, false);
117
118
  Simulator::Run ();
119
  Simulator::Destroy ();
120
}
(-)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 / +5 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::streamsize oldPrecision = os.precision ();
90
  os << std::fixed << std::setprecision (9) << Simulator::Now ().GetSeconds () << "s";
91
  os << std::setprecision (oldPrecision);
92
  os.unsetf (std::ios_base::fixed);
89
}
93
}
90
94
91
/**
95
/**

Return to bug 2490