From 35350ab51d1dcba2679752557feee7faa9b8bb72 Mon Sep 17 00:00:00 2001 From: Peter Barnes Date: Thu, 3 Sep 2020 08:04:02 +0000 Subject: [PATCH] core: (fixes #210): add simulation time and node printers to NS_FATAL... --- src/core/examples/fatal-example.cc | 84 ++++++++++++++++++++++++++++ src/core/examples/wscript | 4 ++ src/core/model/fatal-error.h | 3 + src/core/model/log-macros-enabled.h | 85 ++++++++++++++++++++--------- 4 files changed, 151 insertions(+), 25 deletions(-) create mode 100644 src/core/examples/fatal-example.cc diff --git a/src/core/examples/fatal-example.cc b/src/core/examples/fatal-example.cc new file mode 100644 index 000000000..e45ffd246 --- /dev/null +++ b/src/core/examples/fatal-example.cc @@ -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. + */ + +#include "ns3/fatal-error.h" +#include "ns3/assert.h" +#include "ns3/simulator.h" + +#include + +/** + * \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; +} diff --git a/src/core/examples/wscript b/src/core/examples/wscript index 96c9f5c70..2b769349a 100644 --- a/src/core/examples/wscript +++ b/src/core/examples/wscript @@ -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']) diff --git a/src/core/model/fatal-error.h b/src/core/model/fatal-error.h index 4cd38bc35..2b5a6580a 100644 --- a/src/core/model/fatal-error.h +++ b/src/core/model/fatal-error.h @@ -26,6 +26,7 @@ #include #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 (); \ diff --git a/src/core/model/log-macros-enabled.h b/src/core/model/log-macros-enabled.h index f9abb2967..047598a1a 100644 --- a/src/core/model/log-macros-enabled.h +++ b/src/core/model/log-macros-enabled.h @@ -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