]> git.mxchange.org Git - flightgear.git/blob - Main/fg_debug.c
Incorporated code changes contributed by Charlie Hotchkiss
[flightgear.git] / Main / fg_debug.c
1 /**************************************************************************
2  * fg_debug.c -- Flight Gear debug utility functions
3  *
4  * Written by Paul Bleisch, started January 1998. 
5  *
6  * Copyright (C) 1998 Paul Bleisch, pbleisch@acm.org
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * (Log is kept at end of this file)
23  **************************************************************************/
24
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29
30 #include <Include/cmdargs.h> // Line to command line arguments
31 #include <Main/fg_debug.h>
32
33 static int             fg_DebugSem      = 1;
34 fgDebugClass           fg_DebugClass    = FG_ALL;  // Need visibility for
35 fgDebugPriority        fg_DebugPriority = FG_INFO; // command line processing.
36 static fgDebugCallback fg_DebugCallback = NULL;
37
38 FILE *fg_DebugOutput = NULL;  // Visibility needed for command line processor.
39                               // This can be set to a FILE from the command
40                               // line. If not, it will be set to stderr.
41
42 /* TODO: Actually make this thing thread safe */
43 #ifdef USETHREADS
44 #define FG_GRABDEBUGSEM  while( --fg_DebugSem < 0 ) { fg_DebugSem++; }
45 #define FG_RELEASEDEBUGSEM fg_DebugSem++;
46 #else
47 #define FG_GRABDEBUGSEM
48 #define FG_RELEASEDEBUGSEM
49 #endif
50
51 /* Used for convienence initialization from env variables.
52  */
53 static struct {
54   char *str;
55   fgDebugClass dbg_class;
56 } fg_DebugClasses[] = {
57   { "FG_NONE",    0x00000000 },
58   { "FG_TERRAIN", 0x00000001 },
59   { "FG_ASTRO",   0x00000002 },
60   { "FG_FLIGHT",  0x00000004 },
61   { "FG_INPUT",   0x00000008 },
62   { "FG_GL",      0x00000010 },
63   { "FG_VIEW",    0x00000020 },
64   { "FG_COCKPIT", 0x00000040 },
65   { "FG_GENERAL", 0x00000080 },
66   { "FG_MATH",    0x00000100 },
67   { "FG_EVENT",   0x00000200 },
68   { "FG_AIRCRAFT",0x00000400 },
69
70   /* Do not edit below here, last entry should be null */
71   { "FG_ALL",     0xFFFFFFFF },
72   { NULL, 0 } };
73
74 static fgDebugClass fgDebugStrToClass( char *str );
75
76
77 /* fgInitDebug =============================================================*/
78 void fgInitDebug( void )
79 {
80   char *pszClass, *pszPrio, *pszFile;
81
82   // Support for log file/alt debug output via command line, environment or
83   // reasonable default.
84
85   if( strlen( logArgbuf ) > 3) {   // First check for command line option
86     fg_DebugOutput = fopen(logArgbuf, "a+" );  // Assumed that we will append.
87     }
88
89   if( !fg_DebugOutput ) {          // If not set on command line, environment?
90     pszFile = getenv( "FG_DEBUGFILE" );
91     if( pszFile ) {          // There is such an environmental variable.
92       fg_DebugOutput = fopen( pszFile, "a+" );
93       }
94     }
95
96   if( !fg_DebugOutput ) {         // If neither command line nor environment
97     fg_DebugOutput = stderr;      // then we use the fallback position
98     }                       
99
100   FG_GRABDEBUGSEM;
101   fg_DebugSem = fg_DebugSem;  /* shut up GCC */
102
103   // Test command line option overridge of debug priority. If the value
104   // is in range (properly optioned) the we will override both defaults
105   // and the environmental value.
106
107   if ((priorityArgValue >= FG_BULK) && (priorityArgValue <= FG_ABORT)) {
108     fg_DebugPriority = priorityArgValue;
109     }
110   else {  // Either not set or out of range. We will not warn the user.
111     pszPrio = getenv( "FG_DEBUGPRIORITY" );
112     if( pszPrio ) {
113       fg_DebugPriority = atoi( pszPrio );
114       fprintf( stderr,
115            "fg_debug.c: Environment overrides default debug priority (%d)\n",
116            fg_DebugPriority );
117       }
118     }
119
120
121   if ((debugArgValue >= FG_ALL) && (debugArgValue < FG_UNDEFD)) {
122     fg_DebugPriority = priorityArgValue;
123     }
124   else {  // Either not set or out of range. We will not warn the user.
125     pszClass = getenv( "FG_DEBUGCLASS" );
126     if( pszClass ) {
127       fg_DebugClass = fgDebugStrToClass( pszClass );
128       fprintf( stderr,
129             "fg_debug.c: Environment overrides default debug class (0x%08X)\n",
130                   fg_DebugClass );
131       }
132     }
133
134   FG_RELEASEDEBUGSEM;
135 }
136
137 /* fgDebugStrToClass ======================================================*/
138 fgDebugClass fgDebugStrToClass( char *str )
139 {
140   char *hex = "0123456789ABCDEF";
141   char *hexl = "0123456789abcdef";
142   char *pt, *p, *ph, ps = 1;
143   unsigned int val = 0, i;
144   
145   if( str == NULL ) {
146     return 0;
147   }
148
149   /* Check for 0xXXXXXX notation */
150   if( (p = strstr( str, "0x")) ) {
151     p++; p++;
152     while (*p) {
153       if( (ph = strchr(hex,*p)) || (ph = strchr(hexl,*p)) ){
154         val <<= 4;
155         val += ph-hex;
156         p++;
157       }
158       else {
159         /* fprintf( stderr, "Error in hex string '%s'\n", str ); 
160          */
161         return FG_NONE;
162       }
163     }
164   }
165   else {
166     /* Must be in string format */
167     p = str;
168     ps = 1;
169     while( ps ) {
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 */
176       i=0; 
177       while( fg_DebugClasses[i].str && 
178              strncmp( fg_DebugClasses[i].str, pt, strlen(fg_DebugClasses[i].str)) ) i++;
179       if( fg_DebugClasses[i].str == NULL ) {
180         fprintf( stderr, "fg_debug.c: Could not find message class '%s'\n", pt ); 
181       } else {
182         val |= fg_DebugClasses[i].dbg_class;
183       }
184     }
185   }
186   return (fgDebugClass)val;
187 }
188
189
190 /* fgSetDebugOutput =======================================================*/
191 void fgSetDebugOutput( FILE *out )
192 {
193   FG_GRABDEBUGSEM;
194   fflush( fg_DebugOutput );
195   fg_DebugOutput = out;
196   FG_RELEASEDEBUGSEM;
197 }
198
199
200 /* fgSetDebugLevels =======================================================*/
201 void fgSetDebugLevels( fgDebugClass dbg_class, fgDebugPriority prio )
202 {
203   FG_GRABDEBUGSEM;
204   fg_DebugClass = dbg_class;
205   fg_DebugPriority = prio;
206   FG_RELEASEDEBUGSEM;
207 }
208
209
210 /* fgRegisterDebugCallback ================================================*/
211 fgDebugCallback fgRegisterDebugCallback( fgDebugCallback callback )
212 {
213   fgDebugCallback old;
214   FG_GRABDEBUGSEM;
215   old = fg_DebugCallback;
216   fg_DebugCallback = callback;
217   FG_RELEASEDEBUGSEM;
218   return old;
219 }
220
221
222 /* fgPrintf ===============================================================*/
223 int fgPrintf( fgDebugClass dbg_class, fgDebugPriority prio, char *fmt, ... )
224 {
225   char szOut[1024+1];
226   va_list ap;
227   int ret = 0;
228
229   // If no action to take, then don't bother with the semaphore activity
230   // Slight speed benefit.
231
232   if( !(dbg_class & fg_DebugClass) || (prio < fg_DebugPriority) ) {
233     return ret;        // Its zero anyway. But we might think about changing
234                        // it upon some error condition?
235   }
236
237   FG_GRABDEBUGSEM;
238
239   /* ret = vsprintf( szOut, fmt, (&fmt+1)); (but it didn't work, thus ... */
240   va_start (ap, fmt);
241   ret = vsprintf( szOut, fmt, ap);
242   va_end (ap);
243
244   if( fg_DebugCallback!=NULL && fg_DebugCallback(dbg_class, prio, szOut) ) {
245     FG_RELEASEDEBUGSEM;
246     return ret;
247   }
248   else {
249     fprintf( fg_DebugOutput, szOut );
250     FG_RELEASEDEBUGSEM;
251     if( prio == FG_EXIT ) {
252       exit(0);
253     }
254     else if( prio == FG_ABORT ) {
255       abort();
256     }
257   }
258   return ret;  
259
260
261 // Revision history?
262 //
263