]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/error.cpp
Callback: work at any arity
[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 #include "globaldefs.h"
24
25 #include "debugging/debugging.h"
26 #include "igl.h"
27
28 #include "gtkutil/messagebox.h"
29 #include "console.h"
30 #include "preferences.h"
31
32
33 #if GDEF_OS_WINDOWS
34 #define UNICODE
35 #include <windows.h>
36 #else
37 #include <errno.h>
38 #include <unistd.h>
39 #endif
40
41
42
43 /*
44    =================
45    Error
46
47    For abnormal program terminations
48    =================
49  */
50
51 /*!
52    \todo
53    FIXME the prompt wether to do prefs dialog, may not even be possible
54    if the crash happens before the game is loaded
55  */
56
57 void Error( const char *error, ... ){
58         va_list argptr;
59         char text[4096];
60
61         va_start( argptr,error );
62         vsprintf( text, error,argptr );
63         va_end( argptr );
64
65         strcat( text, "\n" );
66
67 #if GDEF_OS_WINDOWS
68         if ( GetLastError() != 0 ) {
69                 LPVOID lpMsgBuf;
70                 FormatMessage(
71                         FORMAT_MESSAGE_ALLOCATE_BUFFER |
72                         FORMAT_MESSAGE_FROM_SYSTEM |
73                         FORMAT_MESSAGE_IGNORE_INSERTS,
74                         0,
75                         GetLastError(),
76                         MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
77                         (LPTSTR) &lpMsgBuf,
78                         0,
79                         0
80                         );
81                 strcat( text, "GetLastError: " );
82                 /*
83                    Gtk will only crunch 0<=char<=127
84                    this is a bit hackish, but I didn't find useful functions in win32 API for this
85                    http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=516
86                  */
87                 TCHAR *scan, *next = (TCHAR*)lpMsgBuf;
88                 do
89                 {
90                         scan = next;
91                         text[strlen( text ) + 1] = '\0';
92                         if ( ( scan[0] >= 0 ) && ( scan[0] <= 127 ) ) {
93                                 text[strlen( text )] = char(scan[0]);
94                         }
95                         else{
96                                 text[strlen( text )] = '?';
97                         }
98                         next = CharNext( scan );
99                 } while ( next != scan );
100                 strcat( text, "\n" );
101                 LocalFree( lpMsgBuf );
102         }
103 #else
104         if ( errno != 0 ) {
105                 strcat( text, "errno: " );
106                 strcat( text, strerror( errno ) );
107                 strcat( text, "\n" );
108         }
109 #endif
110
111
112 #if 0
113         // we need to have a current context to call glError()
114         if ( g_glwindow_globals.d_glBase != 0 ) {
115                 // glGetError .. can record several errors, clears after calling
116                 //++timo TODO: be able to deal with several errors if necessary, for now I'm just warning about pending error messages
117                 // NOTE: forget that, most boards don't seem to follow the OpenGL standard
118                 GLenum iGLError = glGetError();
119                 if ( iGLError != GL_NO_ERROR ) {
120                         // use our own gluErrorString
121                         strcat( text, "gluErrorString: " );
122                         strcat( text, (char*)gluErrorString( iGLError ) );
123                         strcat( text, "\n" );
124                 }
125         }
126 #endif
127
128         strcat( text, "An unrecoverable error has occured.\n" );
129
130         ERROR_MESSAGE( text );
131
132         // force close logging if necessary
133         Sys_LogFile( false );
134
135         _exit( 1 );
136 }