]> git.mxchange.org Git - flightgear.git/blob - Main/fg_debug.c
Incorporated HUD changes and struct/typedef changes from 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 <Main/fg_debug.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30
31 static int fg_DebugSem = 1;
32 static fgDebugClass fg_DebugClass = FG_ALL;
33 static fgDebugPriority fg_DebugPriority = FG_INFO;
34 static fgDebugCallback fg_DebugCallback = NULL;
35
36 #ifndef __CYGWIN32__
37     static FILE *fg_DebugOutput = stderr;
38 #else /* __CYGWIN32__ */
39     static FILE *fg_DebugOutput = NULL;
40 #endif /* __CYGWIN32 */
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;
81
82 #ifdef __CYGWIN32__
83     fg_DebugOutput = stderr;
84 #endif /* __CYGWIN32 */
85
86   FG_GRABDEBUGSEM;
87   fg_DebugSem=fg_DebugSem;  /* shut up GCC */
88
89   pszPrio = getenv( "FG_DEBUGPRIORITY" );
90   if( pszPrio ) {
91     fg_DebugPriority = atoi( pszPrio );
92     fprintf( stderr, "fg_debug.c: Environment overrides default debug priority (%d)\n",
93              fg_DebugPriority );
94   }
95
96   pszClass = getenv( "FG_DEBUGCLASS" );
97   if( pszClass ) {
98     fg_DebugClass = fgDebugStrToClass( pszClass );
99     fprintf( stderr, "fg_debug.c: Environment overrides default debug class (0x%08X)\n",
100              fg_DebugClass );
101   }
102
103   FG_RELEASEDEBUGSEM;
104 }
105
106 /* fgDebugStrToClass ======================================================*/
107 fgDebugClass fgDebugStrToClass( char *str )
108 {
109   char *hex = "0123456789ABCDEF";
110   char *hexl = "0123456789abcdef";
111   char *pt, *p, *ph, ps=1;
112   unsigned int val = 0, i;
113   
114   if( str == NULL ) {
115     return 0;
116   }
117
118   /* Check for 0xXXXXXX notation */
119   if( (p = strstr( str, "0x")) ) {
120     p++; p++;
121     while (*p) {
122       if( (ph = strchr(hex,*p)) || (ph = strchr(hexl,*p)) ){
123         val <<= 4;
124         val += ph-hex;
125         p++;
126       }
127       else {
128         /* fprintf( stderr, "Error in hex string '%s'\n", str ); 
129          */
130         return FG_NONE;
131       }
132     }
133   }
134   else {
135     /* Must be in string format */
136     p = str;
137     ps = 1;
138     while( ps ) {
139       while( *p && (*p==' ' || *p=='\t') ) p++; /* remove whitespace */
140       pt = p; /* mark token */
141       while( *p && (*p!='|') ) p++; /* find OR or EOS */
142       ps = *p; /* save value at p so we can attempt to be bounds safe */
143       *p++ = 0; /* terminate token */
144       /* determine value for token */
145       i=0; 
146       while( fg_DebugClasses[i].str && 
147              strncmp( fg_DebugClasses[i].str, pt, strlen(fg_DebugClasses[i].str)) ) i++;
148       if( fg_DebugClasses[i].str == NULL ) {
149         fprintf( stderr, "fg_debug.c: Could not find message class '%s'\n", pt ); 
150       } else {
151         val |= fg_DebugClasses[i].dbg_class;
152       }
153     }
154   }
155   return (fgDebugClass)val;
156 }
157
158
159 /* fgSetDebugOutput =======================================================*/
160 void fgSetDebugOutput( FILE *out )
161 {
162   FG_GRABDEBUGSEM;
163   fflush( fg_DebugOutput );
164   fg_DebugOutput = out;
165   FG_RELEASEDEBUGSEM;
166 }
167
168
169 /* fgSetDebugLevels =======================================================*/
170 void fgSetDebugLevels( fgDebugClass dbg_class, fgDebugPriority prio )
171 {
172   FG_GRABDEBUGSEM;
173   fg_DebugClass = dbg_class;
174   fg_DebugPriority = prio;
175   FG_RELEASEDEBUGSEM;
176 }
177
178
179 /* fgRegisterDebugCallback ================================================*/
180 fgDebugCallback fgRegisterDebugCallback( fgDebugCallback callback )
181 {
182   fgDebugCallback old;
183   FG_GRABDEBUGSEM;
184   old = fg_DebugCallback;
185   fg_DebugCallback = callback;
186   FG_RELEASEDEBUGSEM;
187   return old;
188 }
189
190
191 /* fgPrintf ===============================================================*/
192 int fgPrintf( fgDebugClass dbg_class, fgDebugPriority prio, char *fmt, ... )
193 {
194   char szOut[1024+1];
195   va_list ap;
196   int ret = 0;
197
198   FG_GRABDEBUGSEM;
199
200   if( !(dbg_class & fg_DebugClass) || (prio < fg_DebugPriority) ) {
201     FG_RELEASEDEBUGSEM;
202     return 0;
203   } 
204
205   /* ret = vsprintf( szOut, fmt, (&fmt+1)); (but it didn't work, thus ... */
206   va_start (ap, fmt);
207   ret = vsprintf( szOut, fmt, ap);
208   va_end (ap);
209
210   if( fg_DebugCallback!=NULL && fg_DebugCallback(dbg_class, prio, szOut) ) {
211     FG_RELEASEDEBUGSEM;
212     return ret;
213   } 
214   else {
215     fprintf( fg_DebugOutput, szOut );
216     FG_RELEASEDEBUGSEM;
217     if( prio == FG_EXIT ) {
218       exit(0);
219     } 
220     else if( prio == FG_ABORT ) {
221       abort();
222     }
223   }
224   return ret;  
225
226