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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7 */
8
9#include "time-printer.h"
10
11#include "log.h"
12#include "nstime.h"
13#include "simulator.h" // Now()
14
15#include <iomanip>
16
17/**
18 * \file
19 * \ingroup time
20 * ns3::DefaultTimePrinter implementation.
21 */
22
23namespace ns3
24{
25
26NS_LOG_COMPONENT_DEFINE("TimePrinter");
27
28void
29DefaultTimePrinter(std::ostream& os)
30{
31 std::ios_base::fmtflags ff = os.flags(); // Save stream flags
32 std::streamsize oldPrecision = os.precision();
33 os << std::fixed;
34 switch (Time::GetResolution())
35 {
36 case Time::US:
37 os << std::setprecision(6);
38 break;
39 case Time::NS:
40 os << std::setprecision(9);
41 break;
42 case Time::PS:
43 os << std::setprecision(12);
44 break;
45 case Time::FS:
46 os << std::setprecision(15);
47 break;
48
49 default:
50 // default C++ precision of 5
51 os << std::setprecision(5);
52 }
53 os << Simulator::Now().As(Time::S);
54
55 os << std::setprecision(oldPrecision);
56 os.flags(ff); // Restore stream flags
57}
58
59} // namespace ns3
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
static Unit GetResolution()
Definition time.cc:397
@ US
microsecond
Definition nstime.h:107
@ PS
picosecond
Definition nstime.h:109
@ FS
femtosecond
Definition nstime.h:110
@ S
second
Definition nstime.h:105
@ NS
nanosecond
Definition nstime.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void DefaultTimePrinter(std::ostream &os)
Default Time printer.
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.