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