LEFT | RIGHT |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2010 NICTA | 3 * Copyright (c) 2010 NICTA |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 * \param [in] sig The signal condition. | 138 * \param [in] sig The signal condition. |
139 */ | 139 */ |
140 void sigHandler (int sig) | 140 void sigHandler (int sig) |
141 { | 141 { |
142 NS_LOG_FUNCTION (sig); | 142 NS_LOG_FUNCTION (sig); |
143 FlushStreams (); | 143 FlushStreams (); |
144 std::abort (); | 144 std::abort (); |
145 } | 145 } |
146 } // unnamed namespace | 146 } // unnamed namespace |
147 | 147 |
148 #ifndef _WIN32 | |
149 void· | 148 void· |
150 FlushStreams (void) | 149 FlushStreams (void) |
151 { | 150 { |
152 NS_LOG_FUNCTION_NOARGS (); | 151 NS_LOG_FUNCTION_NOARGS (); |
153 std::list<std::ostream*> **pl = PeekStreamList (); | 152 std::list<std::ostream*> **pl = PeekStreamList (); |
154 if (*pl == 0) | 153 if (*pl == 0) |
155 { | 154 { |
156 return; | 155 return; |
157 } | 156 } |
158 | 157 |
159 | 158 |
160 /* Override default SIGSEGV handler - will flush subsequent | 159 /* Override default SIGSEGV handler - will flush subsequent |
161 * streams even if one of the stream pointers is bad. | 160 * streams even if one of the stream pointers is bad. |
162 * The SIGSEGV override should only be active for the | 161 * The SIGSEGV override should only be active for the |
163 * duration of this function. */ | 162 * duration of this function. */ |
| 163 #ifndef _WIN32 |
164 struct sigaction hdl; | 164 struct sigaction hdl; |
165 hdl.sa_handler=sigHandler; | 165 hdl.sa_handler=sigHandler; |
166 sigaction (SIGSEGV, &hdl, 0); | 166 sigaction (SIGSEGV, &hdl, 0); |
| 167 #else |
| 168 typedef void(*SignalHandlerPointer) (int); |
| 169 SignalHandlerPointer previousHandler = signal(SIGSEGV, sigHandler); |
| 170 #endif |
167 | 171 |
168 std::list<std::ostream*> *l = *pl; | 172 std::list<std::ostream*> *l = *pl; |
169 | 173 |
170 /* Need to do it this way in case any of the ostream* causes SIGSEGV */ | 174 /* Need to do it this way in case any of the ostream* causes SIGSEGV */ |
171 while (!l->empty ()) | 175 while (!l->empty ()) |
172 { | 176 { |
173 std::ostream* s (l->front ()); | 177 std::ostream* s (l->front ()); |
174 l->pop_front (); | 178 l->pop_front (); |
175 s->flush (); | 179 s->flush (); |
176 } | 180 } |
177 | 181 |
| 182 #ifndef _WIN32 |
178 /* Restore default SIGSEGV handler (Not that it matters anyway) */ | 183 /* Restore default SIGSEGV handler (Not that it matters anyway) */ |
179 hdl.sa_handler=SIG_DFL; | 184 hdl.sa_handler=SIG_DFL; |
180 sigaction (SIGSEGV, &hdl, 0); | 185 sigaction (SIGSEGV, &hdl, 0); |
| 186 #else |
| 187 /* Restore default SIGSEGV handler (Not that it matters anyway) */ |
| 188 signal(SIGSEGV, previousHandler); |
| 189 #endif |
181 | 190 |
182 /* Flush all opened FILE* */ | 191 /* Flush all opened FILE* */ |
183 std::fflush (0); | 192 std::fflush (0); |
184 | 193 |
185 /* Flush stdandard streams - shouldn't be required (except for clog) */ | 194 /* Flush stdandard streams - shouldn't be required (except for clog) */ |
186 std::cout.flush (); | 195 std::cout.flush (); |
187 std::cerr.flush (); | 196 std::cerr.flush (); |
188 std::clog.flush (); | 197 std::clog.flush (); |
189 | 198 |
190 delete l; | 199 delete l; |
191 *pl = 0; | 200 *pl = 0; |
192 } | 201 } |
193 #else | 202 |
194 void | |
195 FlushStreams(void) | |
196 { | |
197 std::list<std::ostream*> **pl = PeekStreamList(); | |
198 if (pl == 0) | |
199 { | |
200 return; | |
201 } | |
202 | |
203 | |
204 /* Override default SIGSEGV handler - will flush subsequent | |
205 * streams even if one of the stream pointers is bad. | |
206 * The SIGSEGV override should only be active for the | |
207 * duration of this function. */ | |
208 typedef void(*SignalHandlerPointer) (int); | |
209 SignalHandlerPointer previousHandler = signal(SIGSEGV, sigHandler); | |
210 std::list<std::ostream*> *l = *pl; | |
211 | |
212 /* Need to do it this way in case any of the ostream* causes SIGSEGV */ | |
213 while (l != NULL && !l->empty()) | |
214 { | |
215 std::ostream* s(l->front()); | |
216 l->pop_front(); | |
217 s->flush(); | |
218 } | |
219 | |
220 /* Restore default SIGSEGV handler (Not that it matters anyway) */ | |
221 signal(SIGSEGV, previousHandler); | |
222 | |
223 /* Flush all opened FILE* */ | |
224 std::fflush(0); | |
225 | |
226 /* Flush stdandard streams - shouldn't be required (except for clog) */ | |
227 std::cout.flush(); | |
228 std::cerr.flush(); | |
229 std::clog.flush(); | |
230 | |
231 if (l != NULL) delete l; | |
232 *pl = 0; | |
233 } | |
234 #endif | |
235 } // namespace FatalImpl | 203 } // namespace FatalImpl |
236 ·· | 204 ·· |
237 } // namespace ns3 | 205 } // namespace ns3 |
LEFT | RIGHT |