core: (fixes #210): add simulation time and node printers to NS_FATAL...

This commit is contained in:
Peter Barnes
2020-09-03 08:04:02 +00:00
parent d66b180611
commit 35350ab51d
4 changed files with 151 additions and 25 deletions

View File

@@ -0,0 +1,84 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2020 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 "ns3/fatal-error.h"
#include "ns3/assert.h"
#include "ns3/simulator.h"
#include <iostream>
/**
* \file
* \ingroup core-examples
* \ingroup ptr
* Example program illustrating use of the NS_FATAL error handlers.
*/
using namespace ns3;
void
FatalNoMsg (void)
{
std::cerr << "\nEvent triggered fatal error without message, and continuing:"
<< std::endl;
NS_FATAL_ERROR_NO_MSG_CONT ();
}
void
FatalCont (void)
{
std::cerr << "\nEvent triggered fatal error, with custom message, and continuing:"
<< std::endl;
NS_FATAL_ERROR_CONT ("fatal error, but continuing");
}
void
Fatal (void)
{
std::cerr << "\nEvent triggered fatal error, with message, and terminating:"
<< std::endl;
NS_FATAL_ERROR ("fatal error, terminating");
}
int
main (int argc, char ** argv)
{
// First schedule some events
Simulator::Schedule (Seconds (1), FatalNoMsg);
Simulator::Schedule (Seconds (2), FatalCont);
Simulator::Schedule (Seconds (3), Fatal);
// Show some errors outside of simulation time
std::cerr << "\nFatal error with custom message, and continuing:" << std::endl;
NS_FATAL_ERROR_CONT ("fatal error, but continuing");
std::cerr << "\nFatal error without message, and continuing:" << std::endl;
NS_FATAL_ERROR_NO_MSG_CONT ();
// Now run the simulator
Simulator::Run ();
// Should not get here
NS_FATAL_ERROR ("fatal error, terminating");
NS_ASSERT_MSG (false, "Should not get here.");
return 0;
}

View File

@@ -50,6 +50,10 @@ def build(bld):
obj = bld.create_ns3_program('system-path-examples',
['core'])
obj.source = 'system-path-examples.cc'
obj = bld.create_ns3_program('fatal-example',
['core'])
obj.source = 'fatal-example.cc'
obj = bld.create_ns3_program('build-version-example',
['core'])

View File

@@ -26,6 +26,7 @@
#include <cstdlib>
#include "fatal-impl.h"
#include "log.h" // NS_LOG_APPEND...
/**
* \file
@@ -72,6 +73,8 @@
#define NS_FATAL_ERROR_IMPL_NO_MSG(fatal) \
do \
{ \
NS_LOG_APPEND_TIME_PREFIX_IMPL; \
NS_LOG_APPEND_NODE_PREFIX_IMPL; \
std::cerr << "file=" << __FILE__ << ", line=" << \
__LINE__ << std::endl; \
::ns3::FatalImpl::FlushStreams (); \

View File

@@ -27,8 +27,53 @@
* NS_LOG and related logging macro definitions.
*/
#ifdef NS3_LOG_ENABLE
// These two implementation macros
// NS_LOG_APPEND_TIME_PREFIX_IMPL
// NS_LOG_APPEND_NODE_PREFIX_IMPL
// need to be defined in all configurations (debug, release, optimized)
// for use by NS_FATAL_...
/**
* \ingroup logging
* Implementation details for NS_LOG_APPEND_TIME_PREFIX.
* \internal
* Logging implementation macro; should not be called directly.
* We define this separately so we can reuse the definition
* in NS_FATAL.
*/
#define NS_LOG_APPEND_TIME_PREFIX_IMPL \
do { \
ns3::TimePrinter printer = ns3::LogGetTimePrinter (); \
if (printer != 0) \
{ \
(*printer)(std::clog); \
std::clog << " "; \
} \
} while (false)
/**
* \ingroup logging
* Implementation details for NS_LOG_APPEND_NODE_PREFIX.
* \internal
* Logging implementation macro; should not be called directly.
* We define this separately so we can reuse the definition
* in NS_FATAL.
*/
#define NS_LOG_APPEND_NODE_PREFIX_IMPL \
do { \
ns3::NodePrinter printer = ns3::LogGetNodePrinter (); \
if (printer != 0) \
{ \
(*printer)(std::clog); \
std::clog << " "; \
} \
} while (false)
#ifdef NS3_LOG_ENABLE
/**
* \ingroup logging
@@ -36,15 +81,10 @@
* \internal
* Logging implementation macro; should not be called directly.
*/
#define NS_LOG_APPEND_TIME_PREFIX \
if (g_log.IsEnabled (ns3::LOG_PREFIX_TIME)) \
{ \
ns3::TimePrinter printer = ns3::LogGetTimePrinter (); \
if (printer != 0) \
{ \
(*printer)(std::clog); \
std::clog << " "; \
} \
#define NS_LOG_APPEND_TIME_PREFIX \
if (g_log.IsEnabled (ns3::LOG_PREFIX_TIME)) \
{ \
NS_LOG_APPEND_TIME_PREFIX_IMPL; \
}
/**
@@ -53,15 +93,10 @@
* \internal
* Logging implementation macro; should not be called directly.
*/
#define NS_LOG_APPEND_NODE_PREFIX \
if (g_log.IsEnabled (ns3::LOG_PREFIX_NODE)) \
{ \
ns3::NodePrinter printer = ns3::LogGetNodePrinter (); \
if (printer != 0) \
{ \
(*printer)(std::clog); \
std::clog << " "; \
} \
#define NS_LOG_APPEND_NODE_PREFIX \
if (g_log.IsEnabled (ns3::LOG_PREFIX_NODE)) \
{ \
NS_LOG_APPEND_NODE_PREFIX_IMPL; \
}
/**
@@ -70,12 +105,12 @@
* \internal
* Logging implementation macro; should not be called directly.
*/
#define NS_LOG_APPEND_FUNC_PREFIX \
if (g_log.IsEnabled (ns3::LOG_PREFIX_FUNC)) \
{ \
std::clog << g_log.Name () << ":" \
<< __FUNCTION__ << "(): "; \
} \
#define NS_LOG_APPEND_FUNC_PREFIX \
if (g_log.IsEnabled (ns3::LOG_PREFIX_FUNC)) \
{ \
std::clog << g_log.Name () << ":" \
<< __FUNCTION__ << "(): "; \
} \
/**
* \ingroup logging