core: refactor LogTimePrinter, LogNodePrinter
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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); \
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#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);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
59
src/core/model/node-printer.cc
Normal file
59
src/core/model/node-printer.cc
Normal file
@@ -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. <pdbarnes@llnl.gov>
|
||||
*/
|
||||
|
||||
#include "log.h"
|
||||
#include "node-printer.h"
|
||||
#include "simulator.h" // GetContext()
|
||||
|
||||
#include <iomanip>
|
||||
|
||||
/**
|
||||
* \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
|
||||
|
||||
51
src/core/model/node-printer.h
Normal file
51
src/core/model/node-printer.h
Normal file
@@ -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. <pdbarnes@llnl.gov>
|
||||
*/
|
||||
#ifndef NODE_PRINTER_H
|
||||
#define NODE_PRINTER_H
|
||||
|
||||
#include <ostream>
|
||||
|
||||
/**
|
||||
* \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 */
|
||||
@@ -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<SimulatorImpl> 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<SimulatorImpl>
|
||||
|
||||
62
src/core/model/time-printer.cc
Normal file
62
src/core/model/time-printer.cc
Normal file
@@ -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. <pdbarnes@llnl.gov>
|
||||
*/
|
||||
|
||||
#include "log.h"
|
||||
#include "time-printer.h"
|
||||
#include "simulator.h" // Now()
|
||||
#include "nstime.h"
|
||||
|
||||
#include <iomanip>
|
||||
|
||||
/**
|
||||
* \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
|
||||
|
||||
54
src/core/model/time-printer.h
Normal file
54
src/core/model/time-printer.h
Normal file
@@ -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. <pdbarnes@llnl.gov>
|
||||
*/
|
||||
#ifndef TIME_PRINTER_H
|
||||
#define TIME_PRINTER_H
|
||||
|
||||
#include <ostream>
|
||||
|
||||
/**
|
||||
* \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 */
|
||||
@@ -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':
|
||||
|
||||
Reference in New Issue
Block a user