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