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 |
|
---|
30 | static char my_hostname[256];
|
---|
31 | #ifdef WINDOWS
|
---|
32 | static HANDLE my_pid;
|
---|
33 | #else
|
---|
34 | static int my_pid = 0;
|
---|
35 | #endif
|
---|
36 | static int canada = 0;
|
---|
37 | static int swedish_chef = 0;
|
---|
38 | static int australia = 0;
|
---|
39 | static int warnings_enabled = 1;
|
---|
40 |
|
---|
41 | void __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 |
|
---|
56 | static 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 |
|
---|
68 | static 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 |
|
---|
80 | static 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 |
|
---|
93 | static 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
|
---|
109 | static 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 |
|
---|
131 | DECLEXPORT(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 | crDebug("%s", txt);
|
---|
184 | outputChromiumMessage( stderr, txt );
|
---|
185 | #else
|
---|
186 | LogRel(("%s\n", txt));
|
---|
187 | #endif
|
---|
188 | #ifdef WINDOWS
|
---|
189 | if (crGetenv( "CR_GUI_ERROR" ) != NULL)
|
---|
190 | {
|
---|
191 | MessageBox( NULL, txt, "Chromium Error", MB_OK );
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | #endif
|
---|
196 | va_end( args );
|
---|
197 | #ifdef WINDOWS
|
---|
198 | }
|
---|
199 | #if !defined(DEBUG_leo) && !defined(DEBUG_ll158262)
|
---|
200 | if (crGetenv( "CR_DEBUG_ON_ERROR" ) != NULL)
|
---|
201 | #endif
|
---|
202 | {
|
---|
203 | DebugBreak();
|
---|
204 | }
|
---|
205 | #endif
|
---|
206 |
|
---|
207 | #ifdef IN_GUEST
|
---|
208 | /* Give chance for things to close down */
|
---|
209 | raise( SIGTERM );
|
---|
210 |
|
---|
211 | exit(1);
|
---|
212 | #endif
|
---|
213 | }
|
---|
214 |
|
---|
215 | void crEnableWarnings(int onOff)
|
---|
216 | {
|
---|
217 | warnings_enabled = onOff;
|
---|
218 | }
|
---|
219 |
|
---|
220 | DECLEXPORT(void) crWarning( char *format, ... )
|
---|
221 | {
|
---|
222 | if (warnings_enabled) {
|
---|
223 | va_list args;
|
---|
224 | static char txt[8092];
|
---|
225 | int offset;
|
---|
226 |
|
---|
227 | __crCheckCanada();
|
---|
228 | __crCheckSwedishChef();
|
---|
229 | __crCheckAustralia();
|
---|
230 | if (!my_hostname[0])
|
---|
231 | __getHostInfo();
|
---|
232 | offset = sprintf( txt, "OpenGL Warning: ");
|
---|
233 | va_start( args, format );
|
---|
234 | vsprintf( txt + offset, format, args );
|
---|
235 | #if defined(IN_GUEST)
|
---|
236 | crDebug("%s", txt);
|
---|
237 | outputChromiumMessage( stderr, txt );
|
---|
238 | #else
|
---|
239 | LogRel(("%s\n", txt));
|
---|
240 | #endif
|
---|
241 | va_end( args );
|
---|
242 |
|
---|
243 | #if defined(WINDOWS) && defined(DEBUG) && !defined(IN_GUEST)
|
---|
244 | DebugBreak();
|
---|
245 | #endif
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | DECLEXPORT(void) crInfo( char *format, ... )
|
---|
250 | {
|
---|
251 | va_list args;
|
---|
252 | static char txt[8092];
|
---|
253 | int offset;
|
---|
254 |
|
---|
255 | __crCheckCanada();
|
---|
256 | __crCheckSwedishChef();
|
---|
257 | __crCheckAustralia();
|
---|
258 | if (!my_hostname[0])
|
---|
259 | __getHostInfo();
|
---|
260 | offset = sprintf( txt, "OpenGL Info: ");
|
---|
261 | va_start( args, format );
|
---|
262 | vsprintf( txt + offset, format, args );
|
---|
263 | #if defined(IN_GUEST)
|
---|
264 | crDebug("%s", txt);
|
---|
265 | outputChromiumMessage( stderr, txt );
|
---|
266 | #else
|
---|
267 | LogRel(("%s\n", txt));
|
---|
268 | #endif
|
---|
269 | va_end( args );
|
---|
270 | }
|
---|
271 |
|
---|
272 | DECLEXPORT(void) crDebug( char *format, ... )
|
---|
273 | {
|
---|
274 | va_list args;
|
---|
275 | static char txt[8092];
|
---|
276 | int offset;
|
---|
277 | #ifdef WINDOWS
|
---|
278 | DWORD err;
|
---|
279 | #endif
|
---|
280 | static FILE *output;
|
---|
281 | static int first_time = 1;
|
---|
282 | static int silent = 0;
|
---|
283 |
|
---|
284 | if (first_time)
|
---|
285 | {
|
---|
286 | const char *fname = crGetenv( "CR_DEBUG_FILE" );
|
---|
287 | first_time = 0;
|
---|
288 | if (fname)
|
---|
289 | {
|
---|
290 | char debugFile[1000], *p;
|
---|
291 | crStrcpy(debugFile, fname);
|
---|
292 | p = crStrstr(debugFile, "%p");
|
---|
293 | if (p) {
|
---|
294 | /* replace %p with process number */
|
---|
295 | unsigned long n = (unsigned long) crGetPID();
|
---|
296 | sprintf(p, "%lu", n);
|
---|
297 | }
|
---|
298 | fname = debugFile;
|
---|
299 | output = fopen( fname, "w" );
|
---|
300 | if (!output)
|
---|
301 | {
|
---|
302 | crError( "Couldn't open debug log %s", fname );
|
---|
303 | }
|
---|
304 | }
|
---|
305 | else
|
---|
306 | {
|
---|
307 | #if defined(WINDOWS) && defined(IN_GUEST) && (defined(DEBUG_leo) || defined(DEBUG_ll158262))
|
---|
308 | crRedirectIOToConsole();
|
---|
309 | #endif
|
---|
310 | output = stderr;
|
---|
311 | }
|
---|
312 | #ifndef DEBUG
|
---|
313 | /* Release mode: only emit crDebug messages if CR_DEBUG
|
---|
314 | * or CR_DEBUG_FILE is set.
|
---|
315 | */
|
---|
316 | if (!fname && !crGetenv("CR_DEBUG"))
|
---|
317 | silent = 1;
|
---|
318 | #endif
|
---|
319 | }
|
---|
320 |
|
---|
321 | if (silent)
|
---|
322 | return;
|
---|
323 |
|
---|
324 | __crCheckCanada();
|
---|
325 | __crCheckSwedishChef();
|
---|
326 | __crCheckAustralia();
|
---|
327 | if (!my_hostname[0])
|
---|
328 | __getHostInfo();
|
---|
329 |
|
---|
330 | #ifdef WINDOWS
|
---|
331 | if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
|
---|
332 | {
|
---|
333 | static char buf[8092], *temp;
|
---|
334 |
|
---|
335 | SetLastError(0);
|
---|
336 | sprintf( buf, "err=%d", err );
|
---|
337 |
|
---|
338 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
339 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
340 | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
|
---|
341 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
|
---|
342 | (LPTSTR) &temp, 0, NULL );
|
---|
343 | if ( temp )
|
---|
344 | {
|
---|
345 | crStrncpy( buf, temp, sizeof(buf)-1 );
|
---|
346 | buf[sizeof(buf)-1] = 0;
|
---|
347 | }
|
---|
348 |
|
---|
349 | temp = buf + crStrlen(buf) - 1;
|
---|
350 | while ( temp > buf && isspace( *temp ) )
|
---|
351 | {
|
---|
352 | *temp = '\0';
|
---|
353 | temp--;
|
---|
354 | }
|
---|
355 |
|
---|
356 | offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
|
---|
357 | }
|
---|
358 | else
|
---|
359 | {
|
---|
360 | offset = sprintf( txt, "[0x%x] OpenGL Debug: ", crThreadID());
|
---|
361 | }
|
---|
362 | #else
|
---|
363 | offset = sprintf( txt, "[0x%lx] OpenGL Debug: ", crThreadID());
|
---|
364 | #endif
|
---|
365 | va_start( args, format );
|
---|
366 | vsprintf( txt + offset, format, args );
|
---|
367 | #if defined(IN_GUEST)
|
---|
368 | outputChromiumMessage( output, txt );
|
---|
369 | #else
|
---|
370 | # if defined(DEBUG) && defined(DEBUG_leo)
|
---|
371 | outputChromiumMessage( output, txt );
|
---|
372 | # endif
|
---|
373 | if (output==stderr)
|
---|
374 | {
|
---|
375 | LogRel(("%s\n", txt));
|
---|
376 | }
|
---|
377 | else
|
---|
378 | {
|
---|
379 | outputChromiumMessage(output, txt);
|
---|
380 | }
|
---|
381 | #endif
|
---|
382 | va_end( args );
|
---|
383 | }
|
---|