]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/error.cpp
set eol-style
[xonotic/netradiant.git] / radiant / error.cpp
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 #define UNICODE\r
23 #include "stdafx.h"\r
24 \r
25 #if defined (__linux__) || defined (__APPLE__)\r
26 #include <unistd.h>\r
27 #endif\r
28 \r
29 /*\r
30 =================\r
31 Error\r
32 \r
33 For abnormal program terminations\r
34 =================\r
35 */\r
36 \r
37 /*!\r
38 \todo \r
39 FIXME the prompt wether to do prefs dialog, may not even be possible \r
40 if the crash happens before the game is loaded\r
41 */\r
42 \r
43 void Error (const char *error, ...)\r
44 {\r
45   va_list argptr;\r
46   char  text[4096];\r
47 \r
48   va_start (argptr,error);\r
49   vsprintf (text, error,argptr);\r
50   va_end (argptr);\r
51 \r
52   strcat( text, "\n" );\r
53 \r
54 #if defined (__linux__) || defined (__APPLE__)\r
55   if (errno != 0)\r
56   {\r
57     strcat( text, "errno: " );\r
58     strcat( text, strerror (errno));\r
59     strcat( text, "\n");\r
60   }\r
61 #endif\r
62 \r
63 #ifdef _WIN32\r
64   if (GetLastError() != 0)\r
65   {\r
66     LPVOID lpMsgBuf;\r
67     FormatMessage( \r
68       FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
69       FORMAT_MESSAGE_FROM_SYSTEM | \r
70       FORMAT_MESSAGE_IGNORE_INSERTS,\r
71       NULL,\r
72       GetLastError(),\r
73       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language\r
74       (LPTSTR) &lpMsgBuf,\r
75       0,\r
76       NULL \r
77       );\r
78     strcat( text, "GetLastError: " );\r
79     /*\r
80     Gtk will only crunch 0<=char<=127\r
81     this is a bit hackish, but I didn't find useful functions in win32 API for this\r
82     http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=516\r
83     */\r
84     TCHAR *scan, *next = (TCHAR*)lpMsgBuf;\r
85     do\r
86     {\r
87       scan = next;\r
88       text[strlen(text)+1] = '\0';\r
89       if ((scan[0] >= 0) && (scan[0] <= 127))\r
90         text[strlen(text)] = scan[0];\r
91       else\r
92         text[strlen(text)] = '?';\r
93       next = CharNext(scan);\r
94     } while (next != scan);\r
95     strcat( text, "\n");\r
96     LocalFree( lpMsgBuf );\r
97   }\r
98 #endif\r
99 \r
100   // we need to have a current context to call glError()\r
101   if (g_qeglobals_gui.d_glBase != NULL)\r
102   {\r
103     // qglGetError .. can record several errors, clears after calling\r
104     //++timo TODO: be able to deal with several errors if necessary, for now I'm just warning about pending error messages\r
105     // NOTE: forget that, most boards don't seem to follow the OpenGL standard\r
106     GLenum iGLError = qglGetError();\r
107     if (iGLError != GL_NO_ERROR)\r
108     {\r
109       // use our own gluErrorString\r
110       strcat( text, "qgluErrorString: " );\r
111       strcat( text, (char*)qgluErrorString( iGLError ) );\r
112       strcat( text, "\n" );\r
113     }\r
114   }\r
115 \r
116   strcat (text, "An unrecoverable error has occured.\n"\r
117           "Would you like to edit Preferences before exiting Radiant?");\r
118 \r
119   Sys_Printf(text);\r
120 \r
121   if (gtk_MessageBox(NULL, text, "Error", MB_YESNO) == IDYES)\r
122   {\r
123     Sys_Printf("Doing prefs..\n");\r
124     g_PrefsDlg.LoadPrefs ();\r
125     g_PrefsDlg.DoModal();\r
126   }\r
127 \r
128   QGL_Shutdown();\r
129 \r
130   g_PrefsDlg.Destroy ();\r
131   g_dlgSurface.Destroy ();\r
132   g_dlgFind.Destroy ();\r
133 \r
134   // force close logging if necessary\r
135   g_PrefsDlg.mGamesDialog.m_bLogConsole = false;\r
136         Sys_LogFile();\r
137 \r
138   _exit (1);\r
139 }\r
140 \r
141 void WINAPI Error (char *error, ...)\r
142 {\r
143   va_list argptr;\r
144   char  text[1024];\r
145 \r
146   va_start (argptr,error);\r
147   vsprintf (text, error,argptr);\r
148   va_end (argptr);\r
149         \r
150         Error((const char *)text);\r
151 }\r