author | Tom Goff <tgoff@tgoff.net> |
Fri, 23 Mar 2012 04:33:02 -0400 | |
changeset 7786 | 715b25bff41d |
parent 7256 | b04ba6772f8c |
child 9134 | 7a750f032acd |
permissions | -rw-r--r-- |
6371 | 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 |
||
22 |
#include <iostream> |
|
23 |
#include <list> |
|
24 |
||
25 |
#include <cstdlib> |
|
26 |
#include <cstdio> |
|
27 |
||
28 |
#include <csignal> |
|
29 |
||
30 |
namespace ns3 { |
|
31 |
namespace FatalImpl { |
|
32 |
||
6423 | 33 |
/** |
34 |
* Note on implementation: the singleton pattern we use here is tricky because |
|
35 |
* it has to deal with: |
|
36 |
* - make sure that whoever calls Register (potentially before main enters and |
|
37 |
* before any constructor run in this file) succeeds |
|
38 |
* - make sure that whoever calls Unregister (potentially before FlushStream runs |
|
39 |
* but also after it runs) succeeds |
|
40 |
* - make sure that the memory allocated with new is deallocated with delete before |
|
41 |
* the program exits so that valgrind reports no leaks |
|
42 |
* |
|
43 |
* This is why we go through all the painful hoops below. |
|
44 |
*/ |
|
45 |
||
6371 | 46 |
/* File-scope */ |
47 |
namespace { |
|
7169
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
48 |
std::list<std::ostream*> **PeekStreamList (void) |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
49 |
{ |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
50 |
static std::list<std::ostream*> *streams = 0; |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
51 |
return &streams; |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
52 |
} |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
53 |
std::list<std::ostream*> *GetStreamList (void) |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
54 |
{ |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
55 |
std::list<std::ostream*> **pstreams = PeekStreamList (); |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
56 |
if (*pstreams == 0) |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
57 |
{ |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
58 |
*pstreams = new std::list<std::ostream*> (); |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
59 |
} |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
60 |
return *pstreams; |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
61 |
} |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
62 |
struct destructor |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
63 |
{ |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
64 |
~destructor () |
6416
60416db0df14
avoid crash upon process exit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6371
diff
changeset
|
65 |
{ |
60416db0df14
avoid crash upon process exit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6371
diff
changeset
|
66 |
std::list<std::ostream*> **pstreams = PeekStreamList (); |
7169
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
67 |
delete *pstreams; |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
68 |
*pstreams = 0; |
6416
60416db0df14
avoid crash upon process exit
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6371
diff
changeset
|
69 |
} |
7169
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
70 |
}; |
6371 | 71 |
} |
72 |
||
73 |
void |
|
74 |
RegisterStream (std::ostream* stream) |
|
75 |
{ |
|
76 |
GetStreamList ()->push_back (stream); |
|
77 |
} |
|
78 |
||
79 |
void |
|
80 |
UnregisterStream (std::ostream* stream) |
|
81 |
{ |
|
6423 | 82 |
std::list<std::ostream*> **pl = PeekStreamList (); |
83 |
if (*pl == 0) |
|
84 |
{ |
|
85 |
return; |
|
86 |
} |
|
87 |
(*pl)->remove (stream); |
|
88 |
if ((*pl)->empty ()) |
|
89 |
{ |
|
90 |
delete *pl; |
|
91 |
*pl = 0; |
|
92 |
} |
|
6371 | 93 |
} |
94 |
||
95 |
||
96 |
namespace { |
|
7169
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
97 |
/* Overrides normal SIGSEGV handler once the |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
98 |
* HandleTerminate function is run. */ |
7256
b04ba6772f8c
rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents:
7169
diff
changeset
|
99 |
void sigHandler (int sig) |
7169
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
100 |
{ |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
101 |
FlushStreams (); |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
102 |
std::abort (); |
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
103 |
} |
6371 | 104 |
} |
105 |
||
106 |
void |
|
107 |
FlushStreams (void) |
|
108 |
{ |
|
6423 | 109 |
std::list<std::ostream*> **pl = PeekStreamList (); |
7786 | 110 |
if (*pl == 0) |
6423 | 111 |
{ |
112 |
return; |
|
113 |
} |
|
114 |
||
6371 | 115 |
|
116 |
/* Override default SIGSEGV handler - will flush subsequent |
|
117 |
* streams even if one of the stream pointers is bad. |
|
118 |
* The SIGSEGV override should only be active for the |
|
119 |
* duration of this function. */ |
|
6423 | 120 |
struct sigaction hdl; |
6371 | 121 |
hdl.sa_handler=sigHandler; |
122 |
sigaction (SIGSEGV, &hdl, 0); |
|
123 |
||
6423 | 124 |
std::list<std::ostream*> *l = *pl; |
6371 | 125 |
|
126 |
/* Need to do it this way in case any of the ostream* causes SIGSEGV */ |
|
127 |
while (!l->empty ()) |
|
128 |
{ |
|
129 |
std::ostream* s (l->front ()); |
|
130 |
l->pop_front (); |
|
131 |
s->flush (); |
|
132 |
} |
|
133 |
||
134 |
/* Restore default SIGSEGV handler (Not that it matters anyway) */ |
|
135 |
hdl.sa_handler=SIG_DFL; |
|
136 |
sigaction (SIGSEGV, &hdl, 0); |
|
137 |
||
138 |
/* Flush all opened FILE* */ |
|
139 |
std::fflush (0); |
|
140 |
||
141 |
/* Flush stdandard streams - shouldn't be required (except for clog) */ |
|
142 |
std::cout.flush (); |
|
143 |
std::cerr.flush (); |
|
144 |
std::clog.flush (); |
|
6423 | 145 |
|
146 |
delete l; |
|
147 |
*pl = 0; |
|
6371 | 148 |
} |
149 |
||
150 |
} //FatalImpl |
|
151 |
} //ns3 |