Debian Codegraph @

misc/error.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
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 as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/misc/error.cpp $
21 * $Revision: 26 $
22 * $Date: 10/09/01 5:40p $
23 * $Author: Matt $
24 *
25 * Error-handling functions
26 *
27 * $Log: /DescentIII/Main/misc/error.cpp $
28 *
29 * 26 10/09/01 5:40p Matt
30 * Made -nocrashbox work in release build.
31 *
32 * 25 9/15/01 1:46p Kevin
33 * Added -nocrashbox to suppress crash dialog box
34 *
35 * 24 4/19/00 5:23p Matt
36 * From Duane for 1.4
37 * Changed to Mac error exit message
38 *
39 * 23 5/05/99 5:22p Samir
40 * exit(0) instead of exit(1) and other stuff to see why error isn't
41 * exiting in release?
42 *
43 * 22 4/14/99 1:35a Jeff
44 * fixed case mismatched #includes
45 *
46 * 21 4/01/99 4:50p Matt
47 * Took out Warning() function, chaning those calls to mprintf()
48 *
49 * 20 3/02/99 5:50p Kevin
50 * Ouch. Duplicate structures existed and were conflicting.
51 *
52 * 19 2/15/99 1:22p Kevin
53 * Changes for GameGauge
54 *
55 * 18 1/10/99 6:47a Jeff
56 * Changes made to get linux version to compile
57 *
58 * 17 11/01/98 1:58a Jeff
59 * converted the vsprintf calls to use the Pvsprintf, which is a safe
60 * vsprintf, no buffer overflows allowed
61 *
62 * 16 10/18/98 8:52p Matt
63 * Revamped debug/error system.
64 *
65 */
66
67 #include <cstdarg>
68 #include <cstdio>
69
70 #include <stdlib.h>
71 #include <string.h>
72
73 #include "mono.h"
74 #include "pserror.h"
75 #include "debug.h"
76 #include "application.h"
77
78 #define MAX_MSG_LEN 2000
79
80 // Debug break chain handlers
81 void (*DebugBreak_callback_stop)() = NULL;
82 void (*DebugBreak_callback_resume)() = NULL;
83
84 // library initialized flag
85 static bool Error_initialized = false;
86
87 // message that is printed out when error occurs
88 static char Exit_message[MAX_MSG_LEN];
89 static char App_title[32];
90 static char Exit_title_str[64];
91
92 // error message output function
93 void error_Spew();
94
95 //////////////////////////////////////////////////////////////////////////////
96
97 // initializes error handler.
98 bool error_Init(bool debugger, const char *app_title) {
99 Debug_Init(debugger);
100
101 Error_initialized = true;
102 Exit_message[0] = 0;
103
104 strncpy(App_title, app_title, sizeof(App_title) - 1);
105 App_title[sizeof(App_title) - 1] = 0;
106
107 strncpy(Exit_title_str, app_title, sizeof(Exit_title_str) - 1);
108 Exit_title_str[sizeof(Exit_title_str) - 1] = 0;
109
110 atexit(error_Spew);
111
112 return true;
113 }
114
115 // exits the application and prints out a standard error message
116 void Error(const char *fmt, ...) {
117 std::va_list arglist;
118 int exit_msg_len;
119
120 strcpy(Exit_message, "Error: ");
121
122 va_start(arglist, fmt);
123 exit_msg_len = strlen(Exit_message);
124 std::vsnprintf(Exit_message + exit_msg_len, MAX_MSG_LEN - exit_msg_len, fmt, arglist);
125 va_end(arglist);
126
127 snprintf(Exit_title_str, sizeof(Exit_title_str), "%s Error", App_title);
128 mprintf(0, "%s\n", Exit_message);
129
130 #ifdef _DEBUG
131 int answer = IDOK;
132
133 if (DebugBreak_callback_stop)
134 (*DebugBreak_callback_stop)();
135
136 if (Debug_break)
137 answer = Debug_ErrorBox(OSMBOX_ABORTRETRYIGNORE, Exit_title_str, Exit_message, "Press RETRY to debug.");
138 else
139 answer = Debug_ErrorBox(OSMBOX_OKCANCEL, Exit_title_str, Exit_message,
140 "Press OK to exit, CANCEL to ignore this error and continue.");
141
142 switch (answer) {
143 case IDRETRY:
144 debug_break(); // Step Out of this function to see where Error() was called
145 // fall into ignore/cancel case
146 case IDIGNORE:
147 case IDCANCEL:
148 if (DebugBreak_callback_resume)
149 (*DebugBreak_callback_resume)();
150 return;
151 case IDOK:
152 case IDABORT:
153 break; // do nothing, and exit below
154 }
155
156 // Clear the message, since we don't need to see it again when we exit
157 Exit_message[0] = 0;
158
159 #else
160 if (!Error_initialized)
161 error_Spew();
162 #endif
163
164 // Clear the DEBUG_BREAK() callbacks
165 SetDebugBreakHandlers(NULL, NULL);
166
167 // Leave the program
168 exit(0);
169 }
170
171 // Brings up an error message for an int3
172 void Int3MessageBox(const char *file, int line) {
173 #ifndef RELEASE
174 char title[128], message[500];
175 int answer;
176
177 snprintf(title, sizeof(title), "%s Debug Break", App_title);
178 snprintf(message, sizeof(message), "Int3 in %s at line %d.", file, line);
179
180 if (DebugBreak_callback_stop)
181 (*DebugBreak_callback_stop)();
182
183 answer = Debug_ErrorBox(OSMBOX_YESNO, title, message, "It's probably safe to continue. Continue?");
184
185 if (answer == IDNO) {
186 SetDebugBreakHandlers(NULL, NULL);
187 exit(1);
188 }
189
190 if (DebugBreak_callback_resume)
191 (*DebugBreak_callback_resume)();
192 #endif
193 }
194
195 // prints out an assertion error
196 void AssertionFailed(const char *expstr, const char *file, int line) {
197 #ifndef RELEASE
198 char title[128], message[500];
199 int answer;
200
201 snprintf(title, sizeof(title), "%s Assertion Failed", App_title);
202 snprintf(message, sizeof(message), "Assertion failed: (%s)\n\nFile %s at line %d.", expstr, file, line);
203
204 if (DebugBreak_callback_stop)
205 (*DebugBreak_callback_stop)();
206
207 answer = Debug_ErrorBox(OSMBOX_YESNO, title, message, "Continue?");
208
209 if (answer == IDNO) {
210 SetDebugBreakHandlers(NULL, NULL);
211 exit(1);
212 }
213
214 if (DebugBreak_callback_resume)
215 (*DebugBreak_callback_resume)();
216 #endif // #ifndef RELEASE
217 }
218
219 // error message output function
220 void error_Spew() {
221 if (Exit_message[0])
222 Debug_MessageBox(OSMBOX_OK, Exit_title_str, Exit_message);
223 }
224
225 //////////////////////////////////////////////////////////////////////////////
226 // MESSAGE BOX SYSTEM
227
228 char Messagebox_title[80] = "Message";
229
230 // Sets the title for future OutrageMessageBox() dialogs
231 void SetMessageBoxTitle(const char *title) { strncpy(Messagebox_title, title, sizeof(Messagebox_title)); }
232
233 #define BUF_LEN 1024
234
235 // Pops up a dialog box to display a message
236 void OutrageMessageBox(const char *str, ...) {
237 char buf[BUF_LEN];
238 std::va_list arglist;
239 int nchars;
240
241 va_start(arglist, str);
242 nchars = std::vsnprintf(buf, BUF_LEN, str, arglist);
243 va_end(arglist);
244
245 if (nchars >= BUF_LEN)
246 Debug_MessageBox(OSMBOX_OK, Messagebox_title,
247 "The dialog that follows this one overflowed its text buffer. The program may crash.");
248
249 Debug_MessageBox(OSMBOX_OK, Messagebox_title, buf);
250 }
251
252 int OutrageMessageBox(int type, const char *str, ...) {
253 char buf[BUF_LEN];
254 std::va_list arglist;
255 int os_flags = 0;
256 int nchars;
257
258 va_start(arglist, str);
259 nchars = std::vsnprintf(buf, BUF_LEN, str, arglist);
260 va_end(arglist);
261
262 if (type == MBOX_OK)
263 os_flags = OSMBOX_OK;
264 else if (type == MBOX_YESNO)
265 os_flags = OSMBOX_YESNO;
266 else if (type == MBOX_YESNOCANCEL)
267 os_flags = OSMBOX_YESNOCANCEL;
268 else if (type == MBOX_ABORTRETRYIGNORE)
269 os_flags = OSMBOX_ABORTRETRYIGNORE;
270 else
271 Int3();
272
273 if (nchars >= BUF_LEN)
274 Debug_MessageBox(OSMBOX_OK, Messagebox_title,
275 "The dialog that follows this one overflowed its text buffer. The program may crash.");
276
277 return Debug_MessageBox(os_flags, Messagebox_title, buf);
278 }