VirtualBox

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

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

crOpenGL: allow debug output in release builds with CR_DEBUG/CR_DEBUG_FILE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.4 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
103#ifdef WINDOWS
104static void crRedirectIOToConsole()
105{
106 int hConHandle;
107 HANDLE StdHandle;
108 FILE *fp;
109
110 AllocConsole();
111
112 StdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
113 hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
114 fp = _fdopen( hConHandle, "w" );
115 *stdout = *fp;
116 *stderr = *fp;
117
118 StdHandle = GetStdHandle(STD_INPUT_HANDLE);
119 hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
120 fp = _fdopen( hConHandle, "r" );
121 *stdin = *fp;
122}
123#endif
124
125
126DECLEXPORT(void) crError( char *format, ... )
127{
128 va_list args;
129 static char txt[8092];
130 int offset;
131#ifdef WINDOWS
132 DWORD err;
133#endif
134
135 __crCheckCanada();
136 __crCheckSwedishChef();
137 __crCheckAustralia();
138 if (!my_hostname[0])
139 __getHostInfo();
140#ifdef WINDOWS
141 if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
142 {
143 static char buf[8092], *temp;
144
145 SetLastError(0);
146 sprintf( buf, "err=%d", err );
147
148 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
149 FORMAT_MESSAGE_FROM_SYSTEM |
150 FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
151 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
152 (LPTSTR) &temp, 0, NULL );
153 if ( temp )
154 {
155 crStrncpy( buf, temp, sizeof(buf)-1 );
156 buf[sizeof(buf)-1] = 0;
157 }
158
159 temp = buf + crStrlen(buf) - 1;
160 while ( temp > buf && isspace( *temp ) )
161 {
162 *temp = '\0';
163 temp--;
164 }
165
166 offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t----------------------\nCR Error(%s:%d): ", buf, my_hostname, my_pid );
167 }
168 else
169 {
170 offset = sprintf( txt, "OpenGL Error: ");
171 }
172#else
173 offset = sprintf( txt, "OpenGL Error: " );
174#endif
175 va_start( args, format );
176 vsprintf( txt + offset, format, args );
177#if defined(IN_GUEST) || defined(DEBUG_leo) || defined(DEBUG_ll158262)
178 outputChromiumMessage( stderr, txt );
179#else
180 LogRel(("%s\n", txt));
181#endif
182#ifdef WINDOWS
183 if (crGetenv( "CR_GUI_ERROR" ) != NULL)
184 {
185 MessageBox( NULL, txt, "Chromium Error", MB_OK );
186 }
187 else
188 {
189#endif
190 va_end( args );
191#ifdef WINDOWS
192 }
193#if !defined(DEBUG_leo) && !defined(DEBUG_ll158262)
194 if (crGetenv( "CR_DEBUG_ON_ERROR" ) != NULL)
195#endif
196 {
197 DebugBreak();
198 }
199#endif
200
201#ifdef IN_GUEST
202 /* Give chance for things to close down */
203 raise( SIGTERM );
204
205 exit(1);
206#endif
207}
208
209void crEnableWarnings(int onOff)
210{
211 warnings_enabled = onOff;
212}
213
214DECLEXPORT(void) crWarning( char *format, ... )
215{
216 if (warnings_enabled) {
217 va_list args;
218 static char txt[8092];
219 int offset;
220
221 __crCheckCanada();
222 __crCheckSwedishChef();
223 __crCheckAustralia();
224 if (!my_hostname[0])
225 __getHostInfo();
226 offset = sprintf( txt, "OpenGL Warning: ");
227 va_start( args, format );
228 vsprintf( txt + offset, format, args );
229#if defined(IN_GUEST) || defined(DEBUG_leo) || defined(DEBUG_ll158262)
230 outputChromiumMessage( stderr, txt );
231#else
232 LogRel(("%s\n", txt));
233#endif
234 va_end( args );
235 }
236}
237
238DECLEXPORT(void) crInfo( char *format, ... )
239{
240 va_list args;
241 static char txt[8092];
242 int offset;
243
244 __crCheckCanada();
245 __crCheckSwedishChef();
246 __crCheckAustralia();
247 if (!my_hostname[0])
248 __getHostInfo();
249 offset = sprintf( txt, "OpenGL Info: ");
250 va_start( args, format );
251 vsprintf( txt + offset, format, args );
252#if defined(IN_GUEST) || defined(DEBUG_leo) || defined(DEBUG_ll158262)
253 outputChromiumMessage( stderr, txt );
254#else
255 LogRel(("%s\n", txt));
256#endif
257 va_end( args );
258}
259
260DECLEXPORT(void) crDebug( char *format, ... )
261{
262 va_list args;
263 static char txt[8092];
264 int offset;
265#ifdef WINDOWS
266 DWORD err;
267#endif
268 static FILE *output;
269 static int first_time = 1;
270 static int silent = 0;
271
272 if (first_time)
273 {
274 const char *fname = crGetenv( "CR_DEBUG_FILE" );
275 first_time = 0;
276 if (fname)
277 {
278 char debugFile[1000], *p;
279 crStrcpy(debugFile, fname);
280 p = crStrstr(debugFile, "%p");
281 if (p) {
282 /* replace %p with process number */
283 unsigned long n = (unsigned long) crGetPID();
284 sprintf(p, "%lu", n);
285 }
286 fname = debugFile;
287 output = fopen( fname, "w" );
288 if (!output)
289 {
290 crError( "Couldn't open debug log %s", fname );
291 }
292 }
293 else
294 {
295#if defined(WINDOWS) && defined(IN_GUEST) && (defined(DEBUG_leo) || defined(DEBUG_ll158262))
296 crRedirectIOToConsole();
297#endif
298 output = stderr;
299 }
300#ifndef DEBUG
301 /* Release mode: only emit crDebug messages if CR_DEBUG
302 * or CR_DEBUG_FILE is set.
303 */
304 if (!fname && !crGetenv("CR_DEBUG"))
305 silent = 1;
306#endif
307 }
308
309 if (silent)
310 return;
311
312 __crCheckCanada();
313 __crCheckSwedishChef();
314 __crCheckAustralia();
315 if (!my_hostname[0])
316 __getHostInfo();
317
318#ifdef WINDOWS
319 if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
320 {
321 static char buf[8092], *temp;
322
323 SetLastError(0);
324 sprintf( buf, "err=%d", err );
325
326 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
327 FORMAT_MESSAGE_FROM_SYSTEM |
328 FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
329 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
330 (LPTSTR) &temp, 0, NULL );
331 if ( temp )
332 {
333 crStrncpy( buf, temp, sizeof(buf)-1 );
334 buf[sizeof(buf)-1] = 0;
335 }
336
337 temp = buf + crStrlen(buf) - 1;
338 while ( temp > buf && isspace( *temp ) )
339 {
340 *temp = '\0';
341 temp--;
342 }
343
344 offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
345 }
346 else
347 {
348 offset = sprintf( txt, "[0x%x] OpenGL Debug: ", crThreadID());
349 }
350#else
351 offset = sprintf( txt, "[0x%lx] OpenGL Debug: ", crThreadID());
352#endif
353 va_start( args, format );
354 vsprintf( txt + offset, format, args );
355#if defined(IN_GUEST) || defined(DEBUG_leo) || defined(DEBUG_ll158262)
356 outputChromiumMessage( output, txt );
357#else
358 if (output==stderr)
359 {
360 LogRel(("%s\n", txt));
361 }
362 else
363 {
364 outputChromiumMessage(output, txt);
365 }
366#endif
367 va_end( args );
368}
Note: See TracBrowser for help on using the repository browser.

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