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) && !defined(DEBUG_misha)
|
---|
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(const 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(const 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) && !defined(DEBUG_misha)
|
---|
244 | DebugBreak();
|
---|
245 | #endif
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | DECLEXPORT(void) crInfo(const 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(const 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 | char str[1024];
|
---|
288 |
|
---|
289 | #if defined(Linux) && defined(IN_GUEST) && defined(DEBUG_leo)
|
---|
290 | if (!fname)
|
---|
291 | {
|
---|
292 | char pname[1024];
|
---|
293 | crGetProcName(pname, 1024);
|
---|
294 | sprintf(str, "/home/leo/crlog_%s.txt", pname);
|
---|
295 | fname = &str[0];
|
---|
296 | }
|
---|
297 | #endif
|
---|
298 |
|
---|
299 | first_time = 0;
|
---|
300 | if (fname)
|
---|
301 | {
|
---|
302 | char debugFile[1000], *p;
|
---|
303 | crStrcpy(debugFile, fname);
|
---|
304 | p = crStrstr(debugFile, "%p");
|
---|
305 | if (p) {
|
---|
306 | /* replace %p with process number */
|
---|
307 | unsigned long n = (unsigned long) crGetPID();
|
---|
308 | sprintf(p, "%lu", n);
|
---|
309 | }
|
---|
310 | fname = debugFile;
|
---|
311 | output = fopen( fname, "w" );
|
---|
312 | if (!output)
|
---|
313 | {
|
---|
314 | crError( "Couldn't open debug log %s", fname );
|
---|
315 | }
|
---|
316 | }
|
---|
317 | else
|
---|
318 | {
|
---|
319 | #if defined(WINDOWS) && defined(IN_GUEST) && (defined(DEBUG_leo) || defined(DEBUG_ll158262))
|
---|
320 | crRedirectIOToConsole();
|
---|
321 | #endif
|
---|
322 | output = stderr;
|
---|
323 | }
|
---|
324 | #if !defined(DEBUG) || defined(DEBUG_misha)
|
---|
325 | /* Release mode: only emit crDebug messages if CR_DEBUG
|
---|
326 | * or CR_DEBUG_FILE is set.
|
---|
327 | */
|
---|
328 | if (!fname && !crGetenv("CR_DEBUG"))
|
---|
329 | silent = 1;
|
---|
330 | #endif
|
---|
331 | }
|
---|
332 |
|
---|
333 | if (silent)
|
---|
334 | return;
|
---|
335 |
|
---|
336 | __crCheckCanada();
|
---|
337 | __crCheckSwedishChef();
|
---|
338 | __crCheckAustralia();
|
---|
339 | if (!my_hostname[0])
|
---|
340 | __getHostInfo();
|
---|
341 |
|
---|
342 | #ifdef WINDOWS
|
---|
343 | if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
|
---|
344 | {
|
---|
345 | static char buf[8092], *temp;
|
---|
346 |
|
---|
347 | SetLastError(0);
|
---|
348 | sprintf( buf, "err=%d", err );
|
---|
349 |
|
---|
350 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
351 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
352 | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
|
---|
353 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
|
---|
354 | (LPTSTR) &temp, 0, NULL );
|
---|
355 | if ( temp )
|
---|
356 | {
|
---|
357 | crStrncpy( buf, temp, sizeof(buf)-1 );
|
---|
358 | buf[sizeof(buf)-1] = 0;
|
---|
359 | }
|
---|
360 |
|
---|
361 | temp = buf + crStrlen(buf) - 1;
|
---|
362 | while ( temp > buf && isspace( *temp ) )
|
---|
363 | {
|
---|
364 | *temp = '\0';
|
---|
365 | temp--;
|
---|
366 | }
|
---|
367 |
|
---|
368 | offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
|
---|
369 | }
|
---|
370 | else
|
---|
371 | {
|
---|
372 | offset = sprintf( txt, "[0x%x] OpenGL Debug: ", crThreadID());
|
---|
373 | }
|
---|
374 | #else
|
---|
375 | offset = sprintf( txt, "[0x%lx] OpenGL Debug: ", crThreadID());
|
---|
376 | #endif
|
---|
377 | va_start( args, format );
|
---|
378 | vsprintf( txt + offset, format, args );
|
---|
379 | #if defined(IN_GUEST)
|
---|
380 | outputChromiumMessage( output, txt );
|
---|
381 | #else
|
---|
382 | # if defined(DEBUG) && (defined(DEBUG_leo) || defined(DEBUG_ll158262))
|
---|
383 | outputChromiumMessage( output, txt );
|
---|
384 | # endif
|
---|
385 | if (output==stderr)
|
---|
386 | {
|
---|
387 | LogRel(("%s\n", txt));
|
---|
388 | }
|
---|
389 | else
|
---|
390 | {
|
---|
391 | outputChromiumMessage(output, txt);
|
---|
392 | }
|
---|
393 | #endif
|
---|
394 | va_end( args );
|
---|
395 | }
|
---|