3 * fg_debug.c -- Flight Gear debug utility functions
5 * Written by Paul Bleisch, started January 1998.
7 * Copyright (C) 1998 Paul Bleisch, pbleisch@acm.org
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * (Log is kept at end of this file)
25 **************************************************************************/
33 #include <Include/cmdargs.h> // Line to command line arguments
38 static int fg_DebugSem = 1;
39 fgDebugClass fg_DebugClass = FG_NONE; // Need visibility for
40 fgDebugPriority fg_DebugPriority = FG_INFO; // command line processing.
41 static fgDebugCallback fg_DebugCallback = NULL;
43 FILE *fg_DebugOutput = NULL; // Visibility needed for command line processor.
44 // This can be set to a FILE from the command
45 // line. If not, it will be set to stderr.
47 /* TODO: Actually make this thing thread safe */
49 #define FG_GRABDEBUGSEM while( --fg_DebugSem < 0 ) { fg_DebugSem++; }
50 #define FG_RELEASEDEBUGSEM fg_DebugSem++;
52 #define FG_GRABDEBUGSEM
53 #define FG_RELEASEDEBUGSEM
56 /* Used for convienence initialization from env variables.
60 fgDebugClass dbg_class;
61 } fg_DebugClasses[] = {
62 { "FG_NONE", 0x00000000 },
63 { "FG_TERRAIN", 0x00000001 },
64 { "FG_ASTRO", 0x00000002 },
65 { "FG_FLIGHT", 0x00000004 },
66 { "FG_INPUT", 0x00000008 },
67 { "FG_GL", 0x00000010 },
68 { "FG_VIEW", 0x00000020 },
69 { "FG_COCKPIT", 0x00000040 },
70 { "FG_GENERAL", 0x00000080 },
71 { "FG_MATH", 0x00000100 },
72 { "FG_EVENT", 0x00000200 },
73 { "FG_AIRCRAFT",0x00000400 },
75 /* Do not edit below here, last entry should be null */
76 { "FG_ALL", 0xFFFFFFFF },
80 static fgDebugClass fgDebugStrToClass( char *str );
83 /* fgInitDebug =============================================================*/
84 void fgInitDebug( void ) {
85 char *pszClass, *pszPrio, *pszFile;
87 // Support for log file/alt debug output via command line, environment or
88 // reasonable default.
90 if( strlen( logArgbuf ) > 3) { // First check for command line option
91 // Assumed that we will append.
92 fg_DebugOutput = fopen(logArgbuf, "a+" );
95 if( !fg_DebugOutput ) { // If not set on command line, environment?
96 pszFile = getenv( "FG_DEBUGFILE" );
97 if( pszFile ) { // There is such an environmental variable.
98 fg_DebugOutput = fopen( pszFile, "a+" );
102 if( !fg_DebugOutput ) { // If neither command line nor environment
103 fg_DebugOutput = stderr; // then we use the fallback position
107 fg_DebugSem = fg_DebugSem; /* shut up GCC */
109 // Test command line option overridge of debug priority. If the value
110 // is in range (properly optioned) the we will override both defaults
111 // and the environmental value.
113 if ((priorityArgValue >= FG_BULK) && (priorityArgValue <= FG_ABORT)) {
114 fg_DebugPriority = priorityArgValue;
115 } else { // Either not set or out of range. We will not warn the user.
116 pszPrio = getenv( "FG_DEBUGPRIORITY" );
118 fg_DebugPriority = atoi( pszPrio );
120 "fg_debug.c: Environment overrides default debug priority (%d)\n",
126 if ((debugArgValue >= FG_ALL) && (debugArgValue < FG_UNDEFD)) {
127 fg_DebugPriority = priorityArgValue;
128 } else { // Either not set or out of range. We will not warn the user.
129 pszClass = getenv( "FG_DEBUGCLASS" );
131 fg_DebugClass = fgDebugStrToClass( pszClass );
133 "fg_debug.c: Environment overrides default debug class (0x%08X)\n",
141 /* fgDebugStrToClass ======================================================*/
142 fgDebugClass fgDebugStrToClass( char *str ) {
143 char *hex = "0123456789ABCDEF";
144 char *hexl = "0123456789abcdef";
145 char *pt, *p, *ph, ps = 1;
146 unsigned int val = 0, i;
152 /* Check for 0xXXXXXX notation */
153 if( (p = strstr( str, "0x")) ) {
156 if( (ph = strchr(hex,*p)) || (ph = strchr(hexl,*p)) ){
161 // fprintf( stderr, "Error in hex string '%s'\n", str );
166 /* Must be in string format */
170 while( *p && (*p==' ' || *p=='\t') ) p++; /* remove whitespace */
171 pt = p; /* mark token */
172 while( *p && (*p!='|') ) p++; /* find OR or EOS */
173 ps = *p; /* save value at p so we can attempt to be bounds safe */
174 *p++ = 0; /* terminate token */
175 /* determine value for token */
177 while( fg_DebugClasses[i].str &&
178 strncmp( fg_DebugClasses[i].str, pt,
179 strlen(fg_DebugClasses[i].str)) ) i++;
180 if( fg_DebugClasses[i].str == NULL ) {
182 "fg_debug.c: Could not find message class '%s'\n",
185 val |= fg_DebugClasses[i].dbg_class;
189 return (fgDebugClass)val;
193 /* fgSetDebugOutput =======================================================*/
194 void fgSetDebugOutput( FILE *out ) {
196 fflush( fg_DebugOutput );
197 fg_DebugOutput = out;
202 /* fgSetDebugLevels =======================================================*/
203 void fgSetDebugLevels( fgDebugClass dbg_class, fgDebugPriority prio ) {
205 fg_DebugClass = dbg_class;
206 fg_DebugPriority = prio;
211 /* fgRegisterDebugCallback ================================================*/
212 fgDebugCallback fgRegisterDebugCallback( fgDebugCallback callback ) {
215 old = fg_DebugCallback;
216 fg_DebugCallback = callback;
222 /* fgPrintf ===============================================================*/
223 int fgPrintf( fgDebugClass dbg_class, fgDebugPriority prio, char *fmt, ... ) {
228 // If no action to take, then don't bother with the semaphore
229 // activity Slight speed benefit.
231 // printf("dbg_class = %d fg_DebugClass = %d\n", dbg_class, fg_DebugClass);
232 // printf("prio = %d fg_DebugPriority = %d\n", prio, fg_DebugPriority);
234 if( !(dbg_class & fg_DebugClass) ) {
235 // Failed to match a specific debug class
236 if ( prio < fg_DebugPriority ) {
237 // priority is less than requested
239 // "ret" is zero anyway. But we might think about changing
240 // it upon some error condition?
247 /* ret = vsprintf( szOut, fmt, (&fmt+1)); (but it didn't work, thus ... */
249 ret = vsprintf( szOut, fmt, ap);
252 if( fg_DebugCallback!=NULL && fg_DebugCallback(dbg_class, prio, szOut) ) {
256 fprintf( fg_DebugOutput, szOut );
258 if( prio == FG_EXIT ) {
260 } else if( prio == FG_ABORT ) {
269 /* Revision 1.1 1998/04/18 03:52:04 curt
270 /* Moved to Lib directory and created a libDebug.
272 * Revision 1.10 1998/03/14 00:31:21 curt
273 * Beginning initial terrain texturing experiments.
275 * Revision 1.9 1998/03/09 22:44:58 curt
276 * Modified so that you can specify FG_DEBUGCLASS ***or*** FG_DEBUG_PRIORITY
278 * Revision 1.8 1998/03/09 22:11:00 curt
279 * Processed through the format-o-matic.
281 * Revision 1.7 1998/02/16 13:39:43 curt
282 * Miscellaneous weekend tweaks. Fixed? a cache problem that caused whole
283 * tiles to occasionally be missing.