VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/error.c@ 21312

Last change on this file since 21312 was 21312, checked in by vboxsync, 16 years ago

crOpenGL: fix occasional local changes commited with r49656

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.5 KB
Line 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#include "cr_environment.h"
8#include "cr_error.h"
9#include "cr_string.h"
10#include "cr_net.h"
11#include "cr_process.h"
12
13#ifdef WINDOWS
14#define WIN32_LEAN_AND_MEAN
15#include <windows.h>
16#include <io.h>
17#include <fcntl.h>
18#endif
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <stdarg.h>
23#include <signal.h>
24
25#ifndef IN_GUEST
26#define LOG_GROUP LOG_GROUP_SHARED_CROPENGL
27#include <VBox/log.h>
28#endif
29
30static char my_hostname[256];
31#ifdef WINDOWS
32static HANDLE my_pid;
33#else
34static int my_pid = 0;
35#endif
36static int canada = 0;
37static int swedish_chef = 0;
38static int australia = 0;
39static int warnings_enabled = 1;
40
41void __getHostInfo( void )
42{
43 char *temp;
44 if ( crGetHostname( my_hostname, sizeof( my_hostname ) ) )
45 {
46 crStrcpy( my_hostname, "????" );
47 }
48 temp = crStrchr( my_hostname, '.' );
49 if (temp)
50 {
51 *temp = '\0';
52 }
53 my_pid = crGetPID();
54}
55
56static void __crCheckCanada(void)
57{
58 static int first = 1;
59 if (first)
60 {
61 const char *env = crGetenv( "CR_CANADA" );
62 if (env)
63 canada = 1;
64 first = 0;
65 }
66}
67
68static void __crCheckSwedishChef(void)
69{
70 static int first = 1;
71 if (first)
72 {
73 const char *env = crGetenv( "CR_SWEDEN" );
74 if (env)
75 swedish_chef = 1;
76 first = 0;
77 }
78}
79
80static void __crCheckAustralia(void)
81{
82 static int first = 1;
83 if (first)
84 {
85 const char *env = crGetenv( "CR_AUSTRALIA" );
86 const char *env2 = crGetenv( "CR_AUSSIE" );
87 if (env || env2)
88 australia = 1;
89 first = 0;
90 }
91}
92
93static void outputChromiumMessage( FILE *output, char *str )
94{
95 fprintf( output, "%s%s%s%s\n", str,
96 swedish_chef ? " BORK BORK BORK!" : "",
97 canada ? ", eh?" : "",
98 australia ? ", mate!" : ""
99 );
100 fflush( output );
101
102#if defined(DEBUG) && defined(WINDOWS)
103 OutputDebugString(str);
104 OutputDebugString("\n");
105#endif
106}
107
108#ifdef WINDOWS
109static void crRedirectIOToConsole()
110{
111 int hConHandle;
112 HANDLE StdHandle;
113 FILE *fp;
114
115 AllocConsole();
116
117 StdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
118 hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
119 fp = _fdopen( hConHandle, "w" );
120 *stdout = *fp;
121 *stderr = *fp;
122
123 StdHandle = GetStdHandle(STD_INPUT_HANDLE);
124 hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
125 fp = _fdopen( hConHandle, "r" );
126 *stdin = *fp;
127}
128#endif
129
130
131DECLEXPORT(void) crError( char *format, ... )
132{
133 va_list args;
134 static char txt[8092];
135 int offset;
136#ifdef WINDOWS
137 DWORD err;
138#endif
139
140 __crCheckCanada();
141 __crCheckSwedishChef();
142 __crCheckAustralia();
143 if (!my_hostname[0])
144 __getHostInfo();
145#ifdef WINDOWS
146 if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
147 {
148 static char buf[8092], *temp;
149
150 SetLastError(0);
151 sprintf( buf, "err=%d", err );
152
153 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
154 FORMAT_MESSAGE_FROM_SYSTEM |
155 FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
156 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
157 (LPTSTR) &temp, 0, NULL );
158 if ( temp )
159 {
160 crStrncpy( buf, temp, sizeof(buf)-1 );
161 buf[sizeof(buf)-1] = 0;
162 }
163
164 temp = buf + crStrlen(buf) - 1;
165 while ( temp > buf && isspace( *temp ) )
166 {
167 *temp = '\0';
168 temp--;
169 }
170
171 offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t----------------------\nCR Error(%s:%d): ", buf, my_hostname, my_pid );
172 }
173 else
174 {
175 offset = sprintf( txt, "OpenGL Error: ");
176 }
177#else
178 offset = sprintf( txt, "OpenGL Error: " );
179#endif
180 va_start( args, format );
181 vsprintf( txt + offset, format, args );
182#if defined(IN_GUEST)
183 outputChromiumMessage( stderr, txt );
184#else
185 LogRel(("%s\n", txt));
186#endif
187#ifdef WINDOWS
188 if (crGetenv( "CR_GUI_ERROR" ) != NULL)
189 {
190 MessageBox( NULL, txt, "Chromium Error", MB_OK );
191 }
192 else
193 {
194#endif
195 va_end( args );
196#ifdef WINDOWS
197 }
198#if !defined(DEBUG_leo) && !defined(DEBUG_ll158262)
199 if (crGetenv( "CR_DEBUG_ON_ERROR" ) != NULL)
200#endif
201 {
202 DebugBreak();
203 }
204#endif
205
206#ifdef IN_GUEST
207 /* Give chance for things to close down */
208 raise( SIGTERM );
209
210 exit(1);
211#endif
212}
213
214void crEnableWarnings(int onOff)
215{
216 warnings_enabled = onOff;
217}
218
219DECLEXPORT(void) crWarning( char *format, ... )
220{
221 if (warnings_enabled) {
222 va_list args;
223 static char txt[8092];
224 int offset;
225
226 __crCheckCanada();
227 __crCheckSwedishChef();
228 __crCheckAustralia();
229 if (!my_hostname[0])
230 __getHostInfo();
231 offset = sprintf( txt, "OpenGL Warning: ");
232 va_start( args, format );
233 vsprintf( txt + offset, format, args );
234#if defined(IN_GUEST)
235 outputChromiumMessage( stderr, txt );
236#else
237 LogRel(("%s\n", txt));
238#endif
239 va_end( args );
240
241#if defined(WINDOWS) && defined(DEBUG) && !defined(IN_GUEST)
242 DebugBreak();
243#endif
244 }
245}
246
247DECLEXPORT(void) crInfo( char *format, ... )
248{
249 va_list args;
250 static char txt[8092];
251 int offset;
252
253 __crCheckCanada();
254 __crCheckSwedishChef();
255 __crCheckAustralia();
256 if (!my_hostname[0])
257 __getHostInfo();
258 offset = sprintf( txt, "OpenGL Info: ");
259 va_start( args, format );
260 vsprintf( txt + offset, format, args );
261#if defined(IN_GUEST)
262 outputChromiumMessage( stderr, txt );
263#else
264 LogRel(("%s\n", txt));
265#endif
266 va_end( args );
267}
268
269DECLEXPORT(void) crDebug( char *format, ... )
270{
271 va_list args;
272 static char txt[8092];
273 int offset;
274#ifdef WINDOWS
275 DWORD err;
276#endif
277 static FILE *output;
278 static int first_time = 1;
279 static int silent = 0;
280
281 if (first_time)
282 {
283 const char *fname = crGetenv( "CR_DEBUG_FILE" );
284 first_time = 0;
285 if (fname)
286 {
287 char debugFile[1000], *p;
288 crStrcpy(debugFile, fname);
289 p = crStrstr(debugFile, "%p");
290 if (p) {
291 /* replace %p with process number */
292 unsigned long n = (unsigned long) crGetPID();
293 sprintf(p, "%lu", n);
294 }
295 fname = debugFile;
296 output = fopen( fname, "w" );
297 if (!output)
298 {
299 crError( "Couldn't open debug log %s", fname );
300 }
301 }
302 else
303 {
304#if defined(WINDOWS) && defined(IN_GUEST) && (defined(DEBUG_leo) || defined(DEBUG_ll158262))
305 crRedirectIOToConsole();
306#endif
307 output = stderr;
308 }
309#ifndef DEBUG
310 /* Release mode: only emit crDebug messages if CR_DEBUG
311 * or CR_DEBUG_FILE is set.
312 */
313 if (!fname && !crGetenv("CR_DEBUG"))
314 silent = 1;
315#endif
316 }
317
318 if (silent)
319 return;
320
321 __crCheckCanada();
322 __crCheckSwedishChef();
323 __crCheckAustralia();
324 if (!my_hostname[0])
325 __getHostInfo();
326
327#ifdef WINDOWS
328 if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
329 {
330 static char buf[8092], *temp;
331
332 SetLastError(0);
333 sprintf( buf, "err=%d", err );
334
335 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
336 FORMAT_MESSAGE_FROM_SYSTEM |
337 FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
338 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
339 (LPTSTR) &temp, 0, NULL );
340 if ( temp )
341 {
342 crStrncpy( buf, temp, sizeof(buf)-1 );
343 buf[sizeof(buf)-1] = 0;
344 }
345
346 temp = buf + crStrlen(buf) - 1;
347 while ( temp > buf && isspace( *temp ) )
348 {
349 *temp = '\0';
350 temp--;
351 }
352
353 offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
354 }
355 else
356 {
357 offset = sprintf( txt, "[0x%x] OpenGL Debug: ", crThreadID());
358 }
359#else
360 offset = sprintf( txt, "[0x%lx] OpenGL Debug: ", crThreadID());
361#endif
362 va_start( args, format );
363 vsprintf( txt + offset, format, args );
364#if defined(IN_GUEST)
365 outputChromiumMessage( output, txt );
366#else
367# if defined(DEBUG) && defined(DEBUG_leo)
368 outputChromiumMessage( output, txt );
369# endif
370 if (output==stderr)
371 {
372 LogRel(("%s\n", txt));
373 }
374 else
375 {
376 outputChromiumMessage(output, txt);
377 }
378#endif
379 va_end( args );
380}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette