A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
time-printer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Lawrence Livermore National Laboratory
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18 */
19
20#include "time-printer.h"
21
22#include "log.h"
23#include "nstime.h"
24#include "simulator.h" // Now()
25
26#include <iomanip>
27
28/**
29 * \file
30 * \ingroup time
31 * ns3::DefaultTimePrinter implementation.
32 */
33
34namespace ns3
35{
36
37NS_LOG_COMPONENT_DEFINE("TimePrinter");
38
39void
40DefaultTimePrinter(std::ostream& os)
41{
42 std::ios_base::fmtflags ff = os.flags(); // Save stream flags
43 std::streamsize oldPrecision = os.precision();
44 os << std::fixed;
45 switch (Time::GetResolution())
46 {
47 case Time::US:
48 os << std::setprecision(6);
49 break;
50 case Time::NS:
51 os << std::setprecision(9);
52 break;
53 case Time::PS:
54 os << std::setprecision(12);
55 break;
56 case Time::FS:
57 os << std::setprecision(15);
58 break;
59
60 default:
61 // default C++ precision of 5
62 os << std::setprecision(5);
63 }
64 os << Simulator::Now().As(Time::S);
65
66 os << std::setprecision(oldPrecision);
67 os.flags(ff); // Restore stream flags
68}
69
70} // namespace ns3
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
static Unit GetResolution()
Definition: time.cc:408
@ US
microsecond
Definition: nstime.h:118
@ PS
picosecond
Definition: nstime.h:120
@ FS
femtosecond
Definition: nstime.h:121
@ S
second
Definition: nstime.h:116
@ NS
nanosecond
Definition: nstime.h:119
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void DefaultTimePrinter(std::ostream &os)
Default Time printer.
Definition: time-printer.cc:40
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::Simulator declaration.
Declaration of ns3::TimePrinter function pointer type and ns3::DefaultTimePrinter function.