A Discrete-Event Network Simulator
API
fatal-impl.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 NICTA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Quincy Tse <quincy.tse@nicta.com.au>
19  */
20 #include "fatal-impl.h"
21 #include "log.h"
22 
23 #include <iostream>
24 #include <list>
25 
26 #include <cstdlib>
27 #include <cstdio>
28 
29 #include <csignal>
30 
51 namespace ns3 {
52 
53 NS_LOG_COMPONENT_DEFINE ("FatalImpl");
54 
55 namespace FatalImpl {
56 
62 namespace {
63 
71 std::list<std::ostream*> **PeekStreamList (void)
72 {
74  static std::list<std::ostream*> *streams = 0;
75  return &streams;
76 }
77 
84 std::list<std::ostream*> *GetStreamList (void)
85 {
87  std::list<std::ostream*> **pstreams = PeekStreamList ();
88  if (*pstreams == 0)
89  {
90  *pstreams = new std::list<std::ostream*> ();
91  }
92  return *pstreams;
93 }
94 
95 } // anonymous namespace
96 
97 void
98 RegisterStream (std::ostream* stream)
99 {
100  NS_LOG_FUNCTION (stream);
101  GetStreamList ()->push_back (stream);
102 }
103 
104 void
105 UnregisterStream (std::ostream* stream)
106 {
107  NS_LOG_FUNCTION (stream);
108  std::list<std::ostream*> **pl = PeekStreamList ();
109  if (*pl == 0)
110  {
111  return;
112  }
113  (*pl)->remove (stream);
114  if ((*pl)->empty ())
115  {
116  delete *pl;
117  *pl = 0;
118  }
119 }
120 
127 namespace {
128 
138 void sigHandler (int sig)
139 {
140  NS_LOG_FUNCTION (sig);
141  FlushStreams ();
142  std::abort ();
143 }
144 } // anonymous namespace
145 
146 void
148 {
150  std::list<std::ostream*> **pl = PeekStreamList ();
151  if (*pl == 0)
152  {
153  return;
154  }
155 
156 
157  /* Override default SIGSEGV handler - will flush subsequent
158  * streams even if one of the stream pointers is bad.
159  * The SIGSEGV override should only be active for the
160  * duration of this function. */
161  struct sigaction hdl;
162  hdl.sa_handler=sigHandler;
163  sigaction (SIGSEGV, &hdl, 0);
164 
165  std::list<std::ostream*> *l = *pl;
166 
167  /* Need to do it this way in case any of the ostream* causes SIGSEGV */
168  while (!l->empty ())
169  {
170  std::ostream* s (l->front ());
171  l->pop_front ();
172  s->flush ();
173  }
174 
175  /* Restore default SIGSEGV handler (Not that it matters anyway) */
176  hdl.sa_handler=SIG_DFL;
177  sigaction (SIGSEGV, &hdl, 0);
178 
179  /* Flush all opened FILE* */
180  std::fflush (0);
181 
182  /* Flush stdandard streams - shouldn't be required (except for clog) */
183  std::cout.flush ();
184  std::cerr.flush ();
185  std::clog.flush ();
186 
187  delete l;
188  *pl = 0;
189 }
190 
191 } // namespace FatalImpl
192 
193 } // namespace ns3
Declaration of RegisterStream(), UnregisterStream(), and FlushStreams().
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void sigHandler(int sig)
Overrides normal SIGSEGV handler once the HandleTerminate function is run.
Definition: fatal-impl.cc:138
void FlushStreams(void)
Flush all currently registered streams.
Definition: fatal-impl.cc:147
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
std::list< std::ostream * > * GetStreamList(void)
Get the stream list, initializing it if necessary.
Definition: fatal-impl.cc:84
void RegisterStream(std::ostream *stream)
Register a stream to be flushed on abnormal exit.
Definition: fatal-impl.cc:98
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::list< std::ostream * > ** PeekStreamList(void)
Static variable pointing to the list of output streams to be flushed on fatal errors.
Definition: fatal-impl.cc:71
void UnregisterStream(std::ostream *stream)
Unregister a stream for flushing on abnormal exit.
Definition: fatal-impl.cc:105
Debug message logging.