1 //**************************************************************************
2 //**
3 //** ## ## ## ## ## #### #### ### ###
4 //** ## ## ## ## ## ## ## ## ## ## #### ####
5 //** ## ## ## ## ## ## ## ## ## ## ## ## ## ##
6 //** ## ## ######## ## ## ## ## ## ## ## ### ##
7 //** ### ## ## ### ## ## ## ## ## ##
8 //** # ## ## # #### #### ## ##
9 //**
10 //** $Id: exception.cpp 4219 2010-04-19 22:11:41Z firebrand_kh $
11 //**
12 //** Copyright (C) 1999-2010 Jānis Legzdiņš
13 //**
14 //** This program is free software; you can redistribute it and/or
15 //** modify it under the terms of the GNU General Public License
16 //** as published by the Free Software Foundation; either version 2
17 //** of the License, or (at your option) any later version.
18 //**
19 //** This program is distributed in the hope that it will be useful,
20 //** but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 //** GNU General Public License for more details.
23 //**
24 //**************************************************************************
25
26 // HEADER FILES ------------------------------------------------------------
27
28 #include "core.h"
29
30 // MACROS ------------------------------------------------------------------
31
32 // TYPES -------------------------------------------------------------------
33
34 // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
35
36 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
37
38 // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
39
40 // EXTERNAL DATA DECLARATIONS ----------------------------------------------
41
42 // PUBLIC DATA DEFINITIONS -------------------------------------------------
43
44 #ifdef USE_GUARD_SIGNAL_CONTEXT
45 jmp_buf __Context::Env;
46 const char* __Context::ErrToThrow;
47 #endif
48
49 // PRIVATE DATA DEFINITIONS ------------------------------------------------
50
51 static char* host_error_string;
52
53 // CODE --------------------------------------------------------------------
54
55 //==========================================================================
56 //
57 // VavoomError::VavoomError
58 //
59 //==========================================================================
60
VavoomError(const char * text)61 VavoomError::VavoomError(const char *text)
62 {
63 VStr::NCpy(message, text, MAX_ERROR_TEXT_SIZE - 1);
64 message[MAX_ERROR_TEXT_SIZE - 1] = 0;
65 }
66
67 //==========================================================================
68 //
69 // VavoomError::What
70 //
71 //==========================================================================
72
What() const73 const char* VavoomError::What() const
74 {
75 return message;
76 }
77
78 //==========================================================================
79 //
80 // Host_CoreDump
81 //
82 //==========================================================================
83
Host_CoreDump(const char * fmt,...)84 void Host_CoreDump(const char *fmt, ...)
85 {
86 bool first = false;
87
88 if (!host_error_string)
89 {
90 host_error_string = new char[32];
91 VStr::Cpy(host_error_string, "Stack trace: ");
92 first = true;
93 }
94
95 va_list argptr;
96 char string[1024];
97
98 va_start(argptr, fmt);
99 vsprintf(string, fmt, argptr);
100 va_end(argptr);
101
102 GLog.WriteLine("- %s", string);
103
104 char *new_string = new char[VStr::Length(host_error_string) +
105 VStr::Length(string) + 6];
106 VStr::Cpy(new_string, host_error_string);
107 if (first)
108 first = false;
109 else
110 strcat(new_string, " <- ");
111 strcat(new_string, string);
112 delete host_error_string;
113 host_error_string = NULL;
114 host_error_string = new_string;
115 }
116
117 //==========================================================================
118 //
119 // Host_GetCoreDump
120 //
121 //==========================================================================
122
Host_GetCoreDump()123 const char *Host_GetCoreDump()
124 {
125 return host_error_string ? host_error_string : "";
126 }
127
128 //==========================================================================
129 //
130 // Sys_Error
131 //
132 // Exits game and displays error message.
133 //
134 //==========================================================================
135
Sys_Error(const char * error,...)136 void Sys_Error(const char *error, ...)
137 {
138 va_list argptr;
139 char buf[1024];
140
141 va_start(argptr,error);
142 vsprintf(buf, error, argptr);
143 va_end(argptr);
144
145 GLog.WriteLine("Sys_Error: %s", buf);
146 throw VavoomError(buf);
147 }
148