File: error.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (278 lines) | stat: -rw-r--r-- 7,488 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Descent 3 
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.

--- HISTORICAL COMMENTS FOLLOW ---

 * $Logfile: /DescentIII/Main/misc/error.cpp $
 * $Revision: 26 $
 * $Date: 10/09/01 5:40p $
 * $Author: Matt $
 *
 * Error-handling functions
 *
 * $Log: /DescentIII/Main/misc/error.cpp $
 *
 * 26    10/09/01 5:40p Matt
 * Made -nocrashbox work in release build.
 *
 * 25    9/15/01 1:46p Kevin
 * Added -nocrashbox to suppress crash dialog box
 *
 * 24    4/19/00 5:23p Matt
 * From Duane for 1.4
 * Changed to Mac error exit message
 *
 * 23    5/05/99 5:22p Samir
 * exit(0) instead of exit(1) and other stuff to see why error isn't
 * exiting in release?
 *
 * 22    4/14/99 1:35a Jeff
 * fixed case mismatched #includes
 *
 * 21    4/01/99 4:50p Matt
 * Took out Warning() function, chaning those calls to mprintf()
 *
 * 20    3/02/99 5:50p Kevin
 * Ouch. Duplicate structures existed and were conflicting.
 *
 * 19    2/15/99 1:22p Kevin
 * Changes for GameGauge
 *
 * 18    1/10/99 6:47a Jeff
 * Changes made to get linux version to compile
 *
 * 17    11/01/98 1:58a Jeff
 * converted the vsprintf calls to use the Pvsprintf, which is a safe
 * vsprintf, no buffer overflows allowed
 *
 * 16    10/18/98 8:52p Matt
 * Revamped debug/error system.
 *
 */

#include <cstdarg>
#include <cstdio>

#include <stdlib.h>
#include <string.h>

#include "mono.h"
#include "pserror.h"
#include "debug.h"
#include "application.h"

#define MAX_MSG_LEN 2000

//	Debug break chain handlers
void (*DebugBreak_callback_stop)() = NULL;
void (*DebugBreak_callback_resume)() = NULL;

//	library initialized flag
static bool Error_initialized = false;

//	message that is printed out when error occurs
static char Exit_message[MAX_MSG_LEN];
static char App_title[32];
static char Exit_title_str[64];

//	error message output function
void error_Spew();

//////////////////////////////////////////////////////////////////////////////

//	initializes error handler.
bool error_Init(bool debugger, const char *app_title) {
  Debug_Init(debugger);

  Error_initialized = true;
  Exit_message[0] = 0;

  strncpy(App_title, app_title, sizeof(App_title) - 1);
  App_title[sizeof(App_title) - 1] = 0;

  strncpy(Exit_title_str, app_title, sizeof(Exit_title_str) - 1);
  Exit_title_str[sizeof(Exit_title_str) - 1] = 0;

  atexit(error_Spew);

  return true;
}

//	exits the application and prints out a standard error message
void Error(const char *fmt, ...) {
  std::va_list arglist;
  int exit_msg_len;

  strcpy(Exit_message, "Error: ");

  va_start(arglist, fmt);
  exit_msg_len = strlen(Exit_message);
  std::vsnprintf(Exit_message + exit_msg_len, MAX_MSG_LEN - exit_msg_len, fmt, arglist);
  va_end(arglist);

  snprintf(Exit_title_str, sizeof(Exit_title_str), "%s Error", App_title);
  mprintf(0, "%s\n", Exit_message);

#ifdef _DEBUG
  int answer = IDOK;

  if (DebugBreak_callback_stop)
    (*DebugBreak_callback_stop)();

  if (Debug_break)
    answer = Debug_ErrorBox(OSMBOX_ABORTRETRYIGNORE, Exit_title_str, Exit_message, "Press RETRY to debug.");
  else
    answer = Debug_ErrorBox(OSMBOX_OKCANCEL, Exit_title_str, Exit_message,
                            "Press OK to exit, CANCEL to ignore this error and continue.");

  switch (answer) {
  case IDRETRY:
    debug_break(); // Step Out of this function to see where Error() was called
    // fall into ignore/cancel case
  case IDIGNORE:
  case IDCANCEL:
    if (DebugBreak_callback_resume)
      (*DebugBreak_callback_resume)();
    return;
  case IDOK:
  case IDABORT:
    break; // do nothing, and exit below
  }

  // Clear the message, since we don't need to see it again when we exit
  Exit_message[0] = 0;

#else
  if (!Error_initialized)
    error_Spew();
#endif

  // Clear the DEBUG_BREAK() callbacks
  SetDebugBreakHandlers(NULL, NULL);

  // Leave the program
  exit(0);
}

// Brings up an error message for an int3
void Int3MessageBox(const char *file, int line) {
#ifndef RELEASE
  char title[128], message[500];
  int answer;

  snprintf(title, sizeof(title), "%s Debug Break", App_title);
  snprintf(message, sizeof(message), "Int3 in %s at line %d.", file, line);

  if (DebugBreak_callback_stop)
    (*DebugBreak_callback_stop)();

  answer = Debug_ErrorBox(OSMBOX_YESNO, title, message, "It's probably safe to continue.  Continue?");

  if (answer == IDNO) {
    SetDebugBreakHandlers(NULL, NULL);
    exit(1);
  }

  if (DebugBreak_callback_resume)
    (*DebugBreak_callback_resume)();
#endif
}

//	prints out an assertion error
void AssertionFailed(const char *expstr, const char *file, int line) {
#ifndef RELEASE
  char title[128], message[500];
  int answer;

  snprintf(title, sizeof(title), "%s Assertion Failed", App_title);
  snprintf(message, sizeof(message), "Assertion failed: (%s)\n\nFile %s at line %d.", expstr, file, line);

  if (DebugBreak_callback_stop)
    (*DebugBreak_callback_stop)();

  answer = Debug_ErrorBox(OSMBOX_YESNO, title, message, "Continue?");

  if (answer == IDNO) {
    SetDebugBreakHandlers(NULL, NULL);
    exit(1);
  }

  if (DebugBreak_callback_resume)
    (*DebugBreak_callback_resume)();
#endif // #ifndef RELEASE
}

//	error message output function
void error_Spew() {
  if (Exit_message[0])
    Debug_MessageBox(OSMBOX_OK, Exit_title_str, Exit_message);
}

//////////////////////////////////////////////////////////////////////////////
//	MESSAGE BOX SYSTEM

char Messagebox_title[80] = "Message";

// Sets the title for future OutrageMessageBox() dialogs
void SetMessageBoxTitle(const char *title) { strncpy(Messagebox_title, title, sizeof(Messagebox_title)); }

#define BUF_LEN 1024

// Pops up a dialog box to display a message
void OutrageMessageBox(const char *str, ...) {
  char buf[BUF_LEN];
  std::va_list arglist;
  int nchars;

  va_start(arglist, str);
  nchars = std::vsnprintf(buf, BUF_LEN, str, arglist);
  va_end(arglist);

  if (nchars >= BUF_LEN)
    Debug_MessageBox(OSMBOX_OK, Messagebox_title,
                     "The dialog that follows this one overflowed its text buffer.  The program may crash.");

  Debug_MessageBox(OSMBOX_OK, Messagebox_title, buf);
}

int OutrageMessageBox(int type, const char *str, ...) {
  char buf[BUF_LEN];
  std::va_list arglist;
  int os_flags = 0;
  int nchars;

  va_start(arglist, str);
  nchars = std::vsnprintf(buf, BUF_LEN, str, arglist);
  va_end(arglist);

  if (type == MBOX_OK)
    os_flags = OSMBOX_OK;
  else if (type == MBOX_YESNO)
    os_flags = OSMBOX_YESNO;
  else if (type == MBOX_YESNOCANCEL)
    os_flags = OSMBOX_YESNOCANCEL;
  else if (type == MBOX_ABORTRETRYIGNORE)
    os_flags = OSMBOX_ABORTRETRYIGNORE;
  else
    Int3();

  if (nchars >= BUF_LEN)
    Debug_MessageBox(OSMBOX_OK, Messagebox_title,
                     "The dialog that follows this one overflowed its text buffer.  The program may crash.");

  return Debug_MessageBox(os_flags, Messagebox_title, buf);
}