Skip to content

Commit

Permalink
core: refactor LogTimePrinter, LogNodePrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter D. Barnes, Jr. committed Oct 19, 2018
1 parent dff5692 commit 4ed3987
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/core/examples/sample-log-time-format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/core/model/log-macros-enabled.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand All @@ -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); \
Expand Down
16 changes: 8 additions & 8 deletions src/core/model/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -626,7 +626,7 @@ static void CheckEnvironmentVariables (void)
}
#endif
}
void LogSetTimePrinter (LogTimePrinter printer)
void LogSetTimePrinter (TimePrinter printer)
{
g_logTimePrinter = printer;
/** \internal
Expand All @@ -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;
}
Expand Down
37 changes: 14 additions & 23 deletions src/core/model/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);


/**
Expand Down
59 changes: 59 additions & 0 deletions src/core/model/node-printer.cc
Original file line number Diff line number Diff line change
@@ -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. <[email protected]>
*/

#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 changes: 51 additions & 0 deletions src/core/model/node-printer.h
Original file line number Diff line number Diff line change
@@ -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. <[email protected]>
*/
#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 */
66 changes: 5 additions & 61 deletions src/core/model/simulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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>
Expand Down
Loading

0 comments on commit 4ed3987

Please sign in to comment.