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 | /* on windows guests we're typically get called in a context of VBoxOGL!DllMain ( which calls VBoxOGLcrutil!crNetInit ),
|
---|
45 | * which may lead to deadlocks..
|
---|
46 | * Avoid it as it is needed for debugging purposes only */
|
---|
47 | #if !defined(IN_GUEST) || !defined(RT_OS_WINDOWS)
|
---|
48 | if ( crGetHostname( my_hostname, sizeof( my_hostname ) ) )
|
---|
49 | #endif
|
---|
50 | {
|
---|
51 | crStrcpy( my_hostname, "????" );
|
---|
52 | }
|
---|
53 | temp = crStrchr( my_hostname, '.' );
|
---|
54 | if (temp)
|
---|
55 | {
|
---|
56 | *temp = '\0';
|
---|
57 | }
|
---|
58 | my_pid = crGetPID();
|
---|
59 | }
|
---|
60 |
|
---|
61 | static void __crCheckCanada(void)
|
---|
62 | {
|
---|
63 | static int first = 1;
|
---|
64 | if (first)
|
---|
65 | {
|
---|
66 | const char *env = crGetenv( "CR_CANADA" );
|
---|
67 | if (env)
|
---|
68 | canada = 1;
|
---|
69 | first = 0;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | static void __crCheckSwedishChef(void)
|
---|
74 | {
|
---|
75 | static int first = 1;
|
---|
76 | if (first)
|
---|
77 | {
|
---|
78 | const char *env = crGetenv( "CR_SWEDEN" );
|
---|
79 | if (env)
|
---|
80 | swedish_chef = 1;
|
---|
81 | first = 0;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | static void __crCheckAustralia(void)
|
---|
86 | {
|
---|
87 | static int first = 1;
|
---|
88 | if (first)
|
---|
89 | {
|
---|
90 | const char *env = crGetenv( "CR_AUSTRALIA" );
|
---|
91 | const char *env2 = crGetenv( "CR_AUSSIE" );
|
---|
92 | if (env || env2)
|
---|
93 | australia = 1;
|
---|
94 | first = 0;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | static void outputChromiumMessage( FILE *output, char *str )
|
---|
99 | {
|
---|
100 | fprintf( output, "%s%s%s%s\n", str,
|
---|
101 | swedish_chef ? " BORK BORK BORK!" : "",
|
---|
102 | canada ? ", eh?" : "",
|
---|
103 | australia ? ", mate!" : ""
|
---|
104 | );
|
---|
105 | fflush( output );
|
---|
106 |
|
---|
107 | #if defined(DEBUG) && defined(WINDOWS) /* && (!defined(DEBUG_misha) || !defined(IN_GUEST) ) */
|
---|
108 | OutputDebugString(str);
|
---|
109 | OutputDebugString("\n");
|
---|
110 | #endif
|
---|
111 | }
|
---|
112 |
|
---|
113 | #ifdef WINDOWS
|
---|
114 | static void crRedirectIOToConsole()
|
---|
115 | {
|
---|
116 | int hConHandle;
|
---|
117 | HANDLE StdHandle;
|
---|
118 | FILE *fp;
|
---|
119 |
|
---|
120 | AllocConsole();
|
---|
121 |
|
---|
122 | StdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
---|
123 | hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
|
---|
124 | fp = _fdopen( hConHandle, "w" );
|
---|
125 | *stdout = *fp;
|
---|
126 | *stderr = *fp;
|
---|
127 |
|
---|
128 | StdHandle = GetStdHandle(STD_INPUT_HANDLE);
|
---|
129 | hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
|
---|
130 | fp = _fdopen( hConHandle, "r" );
|
---|
131 | *stdin = *fp;
|
---|
132 | }
|
---|
133 | #endif
|
---|
134 |
|
---|
135 |
|
---|
136 | DECLEXPORT(void) crError(const char *format, ... )
|
---|
137 | {
|
---|
138 | va_list args;
|
---|
139 | static char txt[8092];
|
---|
140 | int offset;
|
---|
141 | #ifdef WINDOWS
|
---|
142 | DWORD err;
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | __crCheckCanada();
|
---|
146 | __crCheckSwedishChef();
|
---|
147 | __crCheckAustralia();
|
---|
148 | if (!my_hostname[0])
|
---|
149 | __getHostInfo();
|
---|
150 | #ifdef WINDOWS
|
---|
151 | if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
|
---|
152 | {
|
---|
153 | static char buf[8092], *temp;
|
---|
154 |
|
---|
155 | SetLastError(0);
|
---|
156 | sprintf( buf, "err=%d", err );
|
---|
157 |
|
---|
158 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
159 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
160 | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
|
---|
161 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
|
---|
162 | (LPTSTR) &temp, 0, NULL );
|
---|
163 | if ( temp )
|
---|
164 | {
|
---|
165 | crStrncpy( buf, temp, sizeof(buf)-1 );
|
---|
166 | buf[sizeof(buf)-1] = 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 | temp = buf + crStrlen(buf) - 1;
|
---|
170 | while ( temp > buf && isspace( *temp ) )
|
---|
171 | {
|
---|
172 | *temp = '\0';
|
---|
173 | temp--;
|
---|
174 | }
|
---|
175 |
|
---|
176 | offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t----------------------\nCR Error(%s:%d): ", buf, my_hostname, my_pid );
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | offset = sprintf( txt, "OpenGL Error: ");
|
---|
181 | }
|
---|
182 | #else
|
---|
183 | offset = sprintf( txt, "OpenGL Error: " );
|
---|
184 | #endif
|
---|
185 | va_start( args, format );
|
---|
186 | vsprintf( txt + offset, format, args );
|
---|
187 | #if defined(IN_GUEST)
|
---|
188 | crDebug("%s", txt);
|
---|
189 | outputChromiumMessage( stderr, txt );
|
---|
190 | #else
|
---|
191 | LogRel(("%s\n", txt));
|
---|
192 | #endif
|
---|
193 | #ifdef WINDOWS
|
---|
194 | if (crGetenv( "CR_GUI_ERROR" ) != NULL)
|
---|
195 | {
|
---|
196 | MessageBox( NULL, txt, "Chromium Error", MB_OK );
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | #endif
|
---|
201 | va_end( args );
|
---|
202 | #ifdef WINDOWS
|
---|
203 | }
|
---|
204 | #if !defined(DEBUG_leo) && !defined(DEBUG_ll158262) && !defined(DEBUG_misha)
|
---|
205 | if (crGetenv( "CR_DEBUG_ON_ERROR" ) != NULL)
|
---|
206 | #endif
|
---|
207 | {
|
---|
208 | DebugBreak();
|
---|
209 | }
|
---|
210 | #endif
|
---|
211 |
|
---|
212 | #ifdef IN_GUEST
|
---|
213 | /* Give chance for things to close down */
|
---|
214 | raise( SIGTERM );
|
---|
215 |
|
---|
216 | exit(1);
|
---|
217 | #endif
|
---|
218 | }
|
---|
219 |
|
---|
220 | void crEnableWarnings(int onOff)
|
---|
221 | {
|
---|
222 | warnings_enabled = onOff;
|
---|
223 | }
|
---|
224 |
|
---|
225 | DECLEXPORT(void) crWarning(const char *format, ... )
|
---|
226 | {
|
---|
227 | if (warnings_enabled) {
|
---|
228 | va_list args;
|
---|
229 | static char txt[8092];
|
---|
230 | int offset;
|
---|
231 |
|
---|
232 | __crCheckCanada();
|
---|
233 | __crCheckSwedishChef();
|
---|
234 | __crCheckAustralia();
|
---|
235 | if (!my_hostname[0])
|
---|
236 | __getHostInfo();
|
---|
237 | offset = sprintf( txt, "OpenGL Warning: ");
|
---|
238 | va_start( args, format );
|
---|
239 | vsprintf( txt + offset, format, args );
|
---|
240 | #if defined(IN_GUEST)
|
---|
241 | crDebug("%s", txt);
|
---|
242 | outputChromiumMessage( stderr, txt );
|
---|
243 | #else
|
---|
244 | LogRel(("%s\n", txt));
|
---|
245 | #endif
|
---|
246 | va_end( args );
|
---|
247 |
|
---|
248 | #if defined(WINDOWS) && defined(DEBUG) && !defined(IN_GUEST)
|
---|
249 | DebugBreak();
|
---|
250 | #endif
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | DECLEXPORT(void) crInfo(const char *format, ... )
|
---|
255 | {
|
---|
256 | va_list args;
|
---|
257 | static char txt[8092];
|
---|
258 | int offset;
|
---|
259 |
|
---|
260 | __crCheckCanada();
|
---|
261 | __crCheckSwedishChef();
|
---|
262 | __crCheckAustralia();
|
---|
263 | if (!my_hostname[0])
|
---|
264 | __getHostInfo();
|
---|
265 | offset = sprintf( txt, "OpenGL Info: ");
|
---|
266 | va_start( args, format );
|
---|
267 | vsprintf( txt + offset, format, args );
|
---|
268 | #if defined(IN_GUEST)
|
---|
269 | crDebug("%s", txt);
|
---|
270 | outputChromiumMessage( stderr, txt );
|
---|
271 | #else
|
---|
272 | LogRel(("%s\n", txt));
|
---|
273 | #endif
|
---|
274 | va_end( args );
|
---|
275 | }
|
---|
276 |
|
---|
277 | DECLEXPORT(void) crDebug(const char *format, ... )
|
---|
278 | {
|
---|
279 | va_list args;
|
---|
280 | static char txt[8092];
|
---|
281 | int offset;
|
---|
282 | #ifdef WINDOWS
|
---|
283 | DWORD err;
|
---|
284 | #endif
|
---|
285 | static FILE *output;
|
---|
286 | static int first_time = 1;
|
---|
287 | static int silent = 0;
|
---|
288 |
|
---|
289 | if (first_time)
|
---|
290 | {
|
---|
291 | const char *fname = crGetenv( "CR_DEBUG_FILE" );
|
---|
292 | char str[1024];
|
---|
293 |
|
---|
294 | #if defined(Linux) && defined(IN_GUEST) && defined(DEBUG_leo)
|
---|
295 | if (!fname)
|
---|
296 | {
|
---|
297 | char pname[1024];
|
---|
298 | crGetProcName(pname, 1024);
|
---|
299 | sprintf(str, "/home/leo/crlog_%s.txt", pname);
|
---|
300 | fname = &str[0];
|
---|
301 | }
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | first_time = 0;
|
---|
305 | if (fname)
|
---|
306 | {
|
---|
307 | char debugFile[1000], *p;
|
---|
308 | crStrcpy(debugFile, fname);
|
---|
309 | p = crStrstr(debugFile, "%p");
|
---|
310 | if (p) {
|
---|
311 | /* replace %p with process number */
|
---|
312 | unsigned long n = (unsigned long) crGetPID();
|
---|
313 | sprintf(p, "%lu", n);
|
---|
314 | }
|
---|
315 | fname = debugFile;
|
---|
316 | output = fopen( fname, "w" );
|
---|
317 | if (!output)
|
---|
318 | {
|
---|
319 | crError( "Couldn't open debug log %s", fname );
|
---|
320 | }
|
---|
321 | }
|
---|
322 | else
|
---|
323 | {
|
---|
324 | #if defined(WINDOWS) && defined(IN_GUEST) && (defined(DEBUG_leo) || defined(DEBUG_ll158262))
|
---|
325 | crRedirectIOToConsole();
|
---|
326 | #endif
|
---|
327 | output = stderr;
|
---|
328 | }
|
---|
329 | #if !defined(DEBUG)/* || defined(DEBUG_misha)*/
|
---|
330 | /* Release mode: only emit crDebug messages if CR_DEBUG
|
---|
331 | * or CR_DEBUG_FILE is set.
|
---|
332 | */
|
---|
333 | if (!fname && !crGetenv("CR_DEBUG"))
|
---|
334 | silent = 1;
|
---|
335 | #endif
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (silent)
|
---|
339 | return;
|
---|
340 |
|
---|
341 | __crCheckCanada();
|
---|
342 | __crCheckSwedishChef();
|
---|
343 | __crCheckAustralia();
|
---|
344 | if (!my_hostname[0])
|
---|
345 | __getHostInfo();
|
---|
346 |
|
---|
347 | #ifdef WINDOWS
|
---|
348 | if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
|
---|
349 | {
|
---|
350 | static char buf[8092], *temp;
|
---|
351 |
|
---|
352 | SetLastError(0);
|
---|
353 | sprintf( buf, "err=%d", err );
|
---|
354 |
|
---|
355 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
356 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
357 | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
|
---|
358 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
|
---|
359 | (LPTSTR) &temp, 0, NULL );
|
---|
360 | if ( temp )
|
---|
361 | {
|
---|
362 | crStrncpy( buf, temp, sizeof(buf)-1 );
|
---|
363 | buf[sizeof(buf)-1] = 0;
|
---|
364 | }
|
---|
365 |
|
---|
366 | temp = buf + crStrlen(buf) - 1;
|
---|
367 | while ( temp > buf && isspace( *temp ) )
|
---|
368 | {
|
---|
369 | *temp = '\0';
|
---|
370 | temp--;
|
---|
371 | }
|
---|
372 |
|
---|
373 | offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
|
---|
374 | }
|
---|
375 | else
|
---|
376 | {
|
---|
377 | offset = sprintf( txt, "[0x%x] OpenGL Debug: ", crThreadID());
|
---|
378 | }
|
---|
379 | #else
|
---|
380 | offset = sprintf( txt, "[0x%lx] OpenGL Debug: ", crThreadID());
|
---|
381 | #endif
|
---|
382 | va_start( args, format );
|
---|
383 | vsprintf( txt + offset, format, args );
|
---|
384 | #if defined(IN_GUEST)
|
---|
385 | outputChromiumMessage( output, txt );
|
---|
386 | #else
|
---|
387 | # if defined(DEBUG) && (defined(DEBUG_leo) || defined(DEBUG_ll158262))
|
---|
388 | outputChromiumMessage( output, txt );
|
---|
389 | # endif
|
---|
390 | #ifndef DEBUG_misha
|
---|
391 | if (output==stderr)
|
---|
392 | #endif
|
---|
393 | {
|
---|
394 | LogRel(("%s\n", txt));
|
---|
395 | }
|
---|
396 | #ifndef DEBUG_misha
|
---|
397 | else
|
---|
398 | #endif
|
---|
399 | {
|
---|
400 | outputChromiumMessage(output, txt);
|
---|
401 | }
|
---|
402 | #endif
|
---|
403 | va_end( args );
|
---|
404 | }
|
---|