From 4ed398715110f6338b13c73db738df9652a013f8 Mon Sep 17 00:00:00 2001 From: "Peter D. Barnes, Jr." Date: Fri, 19 Oct 2018 00:48:53 -0400 Subject: [PATCH] core: refactor LogTimePrinter, LogNodePrinter --- src/core/examples/sample-log-time-format.cc | 2 +- src/core/model/log-macros-enabled.h | 4 +- src/core/model/log.cc | 16 ++--- src/core/model/log.h | 37 +++++------- src/core/model/node-printer.cc | 59 ++++++++++++++++++ src/core/model/node-printer.h | 51 ++++++++++++++++ src/core/model/simulator.cc | 66 ++------------------- src/core/model/time-printer.cc | 62 +++++++++++++++++++ src/core/model/time-printer.h | 54 +++++++++++++++++ src/core/wscript | 4 ++ 10 files changed, 260 insertions(+), 95 deletions(-) create mode 100644 src/core/model/node-printer.cc create mode 100644 src/core/model/node-printer.h create mode 100644 src/core/model/time-printer.cc create mode 100644 src/core/model/time-printer.h diff --git a/src/core/examples/sample-log-time-format.cc b/src/core/examples/sample-log-time-format.cc index 7e4d49bfd..8553253e5 100644 --- a/src/core/examples/sample-log-time-format.cc +++ b/src/core/examples/sample-log-time-format.cc @@ -95,7 +95,7 @@ using namespace ns3; namespace { /** - * Pre-ns-3.26 LogTimePrinter equivalent. + * Pre-ns-3.26 TimePrinter equivalent (was called LogTimePrinter). * * Prior to ns-3.26, the time printer used default C++ iostream precision * This function sets it back to the format used before ns-3.26 diff --git a/src/core/model/log-macros-enabled.h b/src/core/model/log-macros-enabled.h index d8dbe7a83..52f01a3c5 100644 --- a/src/core/model/log-macros-enabled.h +++ b/src/core/model/log-macros-enabled.h @@ -39,7 +39,7 @@ #define NS_LOG_APPEND_TIME_PREFIX \ if (g_log.IsEnabled (ns3::LOG_PREFIX_TIME)) \ { \ - ns3::LogTimePrinter printer = ns3::LogGetTimePrinter (); \ + ns3::TimePrinter printer = ns3::LogGetTimePrinter (); \ if (printer != 0) \ { \ (*printer)(std::clog); \ @@ -56,7 +56,7 @@ #define NS_LOG_APPEND_NODE_PREFIX \ if (g_log.IsEnabled (ns3::LOG_PREFIX_NODE)) \ { \ - ns3::LogNodePrinter printer = ns3::LogGetNodePrinter (); \ + ns3::NodePrinter printer = ns3::LogGetNodePrinter (); \ if (printer != 0) \ { \ (*printer)(std::clog); \ diff --git a/src/core/model/log.cc b/src/core/model/log.cc index 5793ae000..2df564fef 100644 --- a/src/core/model/log.cc +++ b/src/core/model/log.cc @@ -46,15 +46,15 @@ namespace ns3 { /** * \ingroup logging - * The LogTimePrinter. + * The Log TimePrinter. * This is private to the logging implementation. */ -static LogTimePrinter g_logTimePrinter = 0; +static TimePrinter g_logTimePrinter = 0; /** * \ingroup logging - * The LogNodePrinter. + * The Log NodePrinter. */ -static LogNodePrinter g_logNodePrinter = 0; +static NodePrinter g_logNodePrinter = 0; /** * \ingroup logging @@ -626,7 +626,7 @@ static void CheckEnvironmentVariables (void) } #endif } -void LogSetTimePrinter (LogTimePrinter printer) +void LogSetTimePrinter (TimePrinter printer) { g_logTimePrinter = printer; /** \internal @@ -635,16 +635,16 @@ void LogSetTimePrinter (LogTimePrinter printer) */ CheckEnvironmentVariables(); } -LogTimePrinter LogGetTimePrinter (void) +TimePrinter LogGetTimePrinter (void) { return g_logTimePrinter; } -void LogSetNodePrinter (LogNodePrinter printer) +void LogSetNodePrinter (NodePrinter printer) { g_logNodePrinter = printer; } -LogNodePrinter LogGetNodePrinter (void) +NodePrinter LogGetNodePrinter (void) { return g_logNodePrinter; } diff --git a/src/core/model/log.h b/src/core/model/log.h index 7fb04f205..ea34f9604 100644 --- a/src/core/model/log.h +++ b/src/core/model/log.h @@ -27,6 +27,8 @@ #include #include +#include "node-printer.h" +#include "time-printer.h" #include "log-macros-enabled.h" #include "log-macros-disabled.h" @@ -297,45 +299,34 @@ namespace ns3 { void LogComponentPrintList (void); /** - * Function signature for prepending the simulation time - * to a log message. - * - * \param [in,out] os The output stream to print on. - */ -typedef void (*LogTimePrinter)(std::ostream &os); -/** - * Function signature for prepending the node id - * to a log message. - * - * \param [in,out] os The output stream to print on. - */ -typedef void (*LogNodePrinter)(std::ostream &os); - -/** - * Set the LogTimePrinter function to be used + * Set the TimePrinter function to be used * to prepend log messages with the simulation time. * - * \param [in] lp The LogTimePrinter function. + * The default is DefaultTimePrinter(). + * + * \param [in] lp The TimePrinter function. */ -void LogSetTimePrinter (LogTimePrinter lp); +void LogSetTimePrinter (TimePrinter lp); /** * Get the LogTimePrinter function currently in use. - * \returns The LogTimePrinter function. + * \returns The current LogTimePrinter function. */ -LogTimePrinter LogGetTimePrinter (void); +TimePrinter LogGetTimePrinter (void); /** * Set the LogNodePrinter function to be used * to prepend log messages with the node id. * + * The default is DefaultNodePrinter(). + * * \param [in] np The LogNodePrinter function. */ -void LogSetNodePrinter (LogNodePrinter np); +void LogSetNodePrinter (NodePrinter np); /** * Get the LogNodePrinter function currently in use. - * \returns The LogNodePrinter function. + * \returns The current LogNodePrinter function. */ -LogNodePrinter LogGetNodePrinter (void); +NodePrinter LogGetNodePrinter (void); /** diff --git a/src/core/model/node-printer.cc b/src/core/model/node-printer.cc new file mode 100644 index 000000000..da754a044 --- /dev/null +++ b/src/core/model/node-printer.cc @@ -0,0 +1,59 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2018 Lawrence Livermore National Laboratory + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Peter D. Barnes, Jr. + */ + +#include "log.h" +#include "node-printer.h" +#include "simulator.h" // GetContext() + +#include + +/** + * \file + * \ingroup simulator + * ns3::DefaultNodePrinter implementation. + */ + +namespace ns3 { + + NS_LOG_COMPONENT_DEFINE ("NodePrinter"); + +/** + * \ingroup logging + * Default node id printer implementation. + * + * \param [in,out] os The output stream to print the node id on. + */ +void +DefaultNodePrinter (std::ostream &os) +{ + if (Simulator::GetContext () == Simulator::NO_CONTEXT) + { + os << "-1"; + } + else + { + os << Simulator::GetContext (); + } +} + + + +} // namespace ns3 + diff --git a/src/core/model/node-printer.h b/src/core/model/node-printer.h new file mode 100644 index 000000000..af399ab09 --- /dev/null +++ b/src/core/model/node-printer.h @@ -0,0 +1,51 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2018 Lawrence Livermore National Laboratory + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Peter D. Barnes, Jr. + */ +#ifndef NODE_PRINTER_H +#define NODE_PRINTER_H + +#include + +/** + * \file + * \ingroup logging + * Declaration of ns3::NodePrinter function pointer type + * and ns3::DefaultNodePrinter function. + */ + +namespace ns3 { + +/** + * Function signature for prepending the node id + * to a log message. + * + * \param [in,out] os The output stream to print on. + */ +typedef void (*NodePrinter)(std::ostream &os); + +/** + * Default Node printer. + * + * \param [in,out] os The output stream to print on. + */ +void DefaultNodePrinter (std::ostream &os); + +} // namespace ns3 + +#endif /* NODE_H */ diff --git a/src/core/model/simulator.cc b/src/core/model/simulator.cc index d2e08d0ec..091eaafd9 100644 --- a/src/core/model/simulator.cc +++ b/src/core/model/simulator.cc @@ -43,8 +43,7 @@ * \file * \ingroup simulator * ns3::Simulator implementation, as well as implementation pointer, - * global scheduler implementation, and default ns3::NodePrinter - * and ns3::TimePrinter. + * global scheduler implementation. */ namespace ns3 { @@ -77,61 +76,6 @@ static GlobalValue g_schedTypeImpl = GlobalValue ("SchedulerType", TypeIdValue (MapScheduler::GetTypeId ()), MakeTypeIdChecker ()); -/** - * \ingroup logging - * Default TimePrinter implementation. - * - * \param [in,out] os The output stream to print the time on. - */ -static void -TimePrinter (std::ostream &os) -{ - std::ios_base::fmtflags ff = os.flags (); // Save stream flags - std::streamsize oldPrecision = os.precision (); - if (Time::GetResolution () == Time::NS) - { - os << std::fixed << std::setprecision (9) << Simulator::Now ().As (Time::S); - } - else if (Time::GetResolution () == Time::PS) - { - os << std::fixed << std::setprecision (12) << Simulator::Now ().As (Time::S); - } - else if (Time::GetResolution () == Time::FS) - { - os << std::fixed << std::setprecision (15) << Simulator::Now ().As (Time::S); - } - else if (Time::GetResolution () == Time::US) - { - os << std::fixed << std::setprecision (6) << Simulator::Now ().As (Time::S); - } - else - { - // default C++ precision of 5 - os << std::fixed << std::setprecision (5) << Simulator::Now ().As (Time::S); - } - os << std::setprecision (oldPrecision); - os.flags (ff); // Restore stream flags -} - -/** - * \ingroup logging - * Default node id printer implementation. - * - * \param [in,out] os The output stream to print the node id on. - */ -static void -NodePrinter (std::ostream &os) -{ - if (Simulator::GetContext () == Simulator::NO_CONTEXT) - { - os << "-1"; - } - else - { - os << Simulator::GetContext (); - } -} - /** * \ingroup simulator * \brief Get the static SimulatorImpl instance. @@ -180,8 +124,8 @@ static SimulatorImpl * GetImpl (void) // Simulator::Now which would call Simulator::GetImpl, and, thus, get us // in an infinite recursion until the stack explodes. // - LogSetTimePrinter (&TimePrinter); - LogSetNodePrinter (&NodePrinter); + LogSetTimePrinter (&DefaultTimePrinter); + LogSetNodePrinter (&DefaultNodePrinter); } return *pimpl; } @@ -417,8 +361,8 @@ Simulator::SetImplementation (Ptr impl) // Simulator::Now which would call Simulator::GetImpl, and, thus, get us // in an infinite recursion until the stack explodes. // - LogSetTimePrinter (&TimePrinter); - LogSetNodePrinter (&NodePrinter); + LogSetTimePrinter (&DefaultTimePrinter); + LogSetNodePrinter (&DefaultNodePrinter); } Ptr diff --git a/src/core/model/time-printer.cc b/src/core/model/time-printer.cc new file mode 100644 index 000000000..e06c60a15 --- /dev/null +++ b/src/core/model/time-printer.cc @@ -0,0 +1,62 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2018 Lawrence Livermore National Laboratory + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Peter D. Barnes, Jr. + */ + +#include "log.h" +#include "time-printer.h" +#include "simulator.h" // Now() +#include "nstime.h" + +#include + +/** + * \file + * \ingroup time + * ns3::DefaultTimePrinter implementation. + */ + +namespace ns3 { + + NS_LOG_COMPONENT_DEFINE ("TimePrinter"); + +void +DefaultTimePrinter (std::ostream &os) +{ + std::ios_base::fmtflags ff = os.flags (); // Save stream flags + std::streamsize oldPrecision = os.precision (); + os << std::fixed; + switch (Time::GetResolution ()) + { + case Time::US : os << std::setprecision (6); break; + case Time::NS : os << std::setprecision (9); break; + case Time::PS : os << std::setprecision (12); break; + case Time::FS : os << std::setprecision (15); break; + default : + // default C++ precision of 5 + os << std::setprecision (5); + } + os << Simulator::Now ().As (Time::S); + + os << std::setprecision (oldPrecision); + os.flags (ff); // Restore stream flags +} + + +} // namespace ns3 + diff --git a/src/core/model/time-printer.h b/src/core/model/time-printer.h new file mode 100644 index 000000000..084753352 --- /dev/null +++ b/src/core/model/time-printer.h @@ -0,0 +1,54 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2018 Lawrence Livermore National Laboratory + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Peter D. Barnes, Jr. + */ +#ifndef TIME_PRINTER_H +#define TIME_PRINTER_H + +#include + +/** + * \file + * \ingroup time + * Declaration of ns3::TimePrinter function pointer type + * and ns3::DefaultTimePrinter function. + */ + +namespace ns3 { + +/** + * Function signature for features requiring a time formatter, + * such as logging or ShowProgress. + * + * A TimePrinter should write the current simulation time + * (Simulator::Now()) on the designated output stream. + * + * \param [in,out] os The output stream to print on. + */ +typedef void (*TimePrinter)(std::ostream &os); + +/** + * Default Time printer. + * + * \param [in,out] os The output stream to print on. + */ +void DefaultTimePrinter (std::ostream &os); + +} // namespace ns3 + +#endif /* TIME_H */ diff --git a/src/core/wscript b/src/core/wscript index 44f672da4..e8ffba3ba 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -212,6 +212,8 @@ def build(bld): 'model/hash-fnv.cc', 'model/hash.cc', 'model/des-metrics.cc', + 'model/node-printer.cc', + 'model/time-printer.cc', ] core_test = bld.create_ns3_module_test_library('core') @@ -325,6 +327,8 @@ def build(bld): 'model/non-copyable.h', 'model/build-profile.h', 'model/des-metrics.h', + 'model/node-printer.h', + 'model/time-printer.h', ] if sys.platform == 'win32':