]> git.mxchange.org Git - flightgear.git/blob - Debug/fg_debug.c
Modified Files:
[flightgear.git] / Debug / fg_debug.c
1 /* -*- Mode: C++ -*-
2  *
3  * fg_debug.c -- Flight Gear debug utility functions
4  *
5  * Written by Paul Bleisch, started January 1998. 
6  *
7  * Copyright (C) 1998 Paul Bleisch, pbleisch@acm.org
8  *
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.
13  *
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.
18  *
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.
22  *
23  * $Id$
24  * (Log is kept at end of this file)
25  **************************************************************************/
26
27
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32
33 #include <Include/cmdargs.h> // Line to command line arguments
34
35 #include "fg_debug.h"
36
37
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;
42
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.
46
47 /* TODO: Actually make this thing thread safe */
48 #ifdef USETHREADS
49 #define FG_GRABDEBUGSEM  while( --fg_DebugSem < 0 ) { fg_DebugSem++; }
50 #define FG_RELEASEDEBUGSEM fg_DebugSem++;
51 #else
52 #define FG_GRABDEBUGSEM
53 #define FG_RELEASEDEBUGSEM
54 #endif
55
56 /* Used for convienence initialization from env variables.
57  */
58 static struct {
59     char *str;
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 },
74
75     /* Do not edit below here, last entry should be null */
76     { "FG_ALL",     0xFFFFFFFF },
77     { NULL, 0 } 
78 };
79
80 static fgDebugClass fgDebugStrToClass( char *str );
81
82
83 /* fgInitDebug =============================================================*/
84 void fgInitDebug( void ) {
85     char *pszClass, *pszPrio, *pszFile;
86
87     // Support for log file/alt debug output via command line, environment or
88     // reasonable default.
89
90     /*
91     if( strlen( logArgbuf ) > 3) {   // First check for command line option
92         // Assumed that we will append.
93         fg_DebugOutput = fopen(logArgbuf, "a+" );
94     }
95     */
96
97     if( !fg_DebugOutput ) {          // If not set on command line, environment?
98         pszFile = getenv( "FG_DEBUGFILE" );
99         if( pszFile ) {          // There is such an environmental variable.
100             fg_DebugOutput = fopen( pszFile, "a+" );
101         }
102     }
103
104     if( !fg_DebugOutput ) {         // If neither command line nor environment
105         fg_DebugOutput = stderr;      // then we use the fallback position
106     }
107     
108     FG_GRABDEBUGSEM;
109     fg_DebugSem = fg_DebugSem;  /* shut up GCC */
110
111     // Test command line option overridge of debug priority. If the value
112     // is in range (properly optioned) the we will override both defaults
113     // and the environmental value.
114
115     /*
116     if ((priorityArgValue >= FG_BULK) && (priorityArgValue <= FG_ABORT)) {
117         fg_DebugPriority = priorityArgValue;
118     } else {  // Either not set or out of range. We will not warn the user.
119     */
120         pszPrio = getenv( "FG_DEBUGPRIORITY" );
121         if( pszPrio ) {
122             fg_DebugPriority = atoi( pszPrio );
123             fprintf( stderr,
124                      "fg_debug.c: Environment overrides default debug priority (%d)\n",
125                      fg_DebugPriority );
126         }
127     /* } */
128
129
130     /*
131     if ((debugArgValue >= FG_ALL) && (debugArgValue < FG_UNDEFD)) {
132         fg_DebugPriority = priorityArgValue;
133     } else {  // Either not set or out of range. We will not warn the user.
134     */
135         pszClass = getenv( "FG_DEBUGCLASS" );
136         if( pszClass ) {
137             fg_DebugClass = fgDebugStrToClass( pszClass );
138             fprintf( stderr,
139                      "fg_debug.c: Environment overrides default debug class (0x%08X)\n",
140                      fg_DebugClass );
141         }
142     /* } */
143
144     FG_RELEASEDEBUGSEM;
145 }
146
147 /* fgDebugStrToClass ======================================================*/
148 fgDebugClass fgDebugStrToClass( char *str ) {
149     char *hex = "0123456789ABCDEF";
150     char *hexl = "0123456789abcdef";
151     char *pt, *p, *ph, ps = 1;
152     unsigned int val = 0, i;
153   
154     if( str == NULL ) {
155         return 0;
156     }
157
158     /* Check for 0xXXXXXX notation */
159     if( (p = strstr( str, "0x")) ) {
160         p++; p++;
161         while (*p) {
162             if( (ph = strchr(hex,*p)) || (ph = strchr(hexl,*p)) ){
163                 val <<= 4;
164                 val += ph-hex;
165                 p++;
166             } else {
167                 // fprintf( stderr, "Error in hex string '%s'\n", str ); 
168                 return FG_NONE;
169             }
170         }
171     } else {
172         /* Must be in string format */
173         p = str;
174         ps = 1;
175         while( ps ) {
176             while( *p && (*p==' ' || *p=='\t') ) p++; /* remove whitespace */
177             pt = p; /* mark token */
178             while( *p && (*p!='|') ) p++; /* find OR or EOS */
179             ps = *p; /* save value at p so we can attempt to be bounds safe */
180             *p++ = 0; /* terminate token */
181             /* determine value for token */
182             i=0; 
183             while( fg_DebugClasses[i].str && 
184                    strncmp( fg_DebugClasses[i].str, pt, 
185                             strlen(fg_DebugClasses[i].str)) ) i++;
186             if( fg_DebugClasses[i].str == NULL ) {
187                 fprintf( stderr,
188                          "fg_debug.c: Could not find message class '%s'\n",
189                          pt ); 
190             } else {
191                 val |= fg_DebugClasses[i].dbg_class;
192             }
193         }
194     }
195     return (fgDebugClass)val;
196 }
197
198
199 /* fgSetDebugOutput =======================================================*/
200 void fgSetDebugOutput( FILE *out ) {
201     FG_GRABDEBUGSEM;
202     fflush( fg_DebugOutput );
203     fg_DebugOutput = out;
204     FG_RELEASEDEBUGSEM;
205 }
206
207
208 /* fgSetDebugLevels =======================================================*/
209 void fgSetDebugLevels( fgDebugClass dbg_class, fgDebugPriority prio ) {
210     FG_GRABDEBUGSEM;
211     fg_DebugClass = dbg_class;
212     fg_DebugPriority = prio;
213     FG_RELEASEDEBUGSEM;
214 }
215
216
217 /* fgRegisterDebugCallback ================================================*/
218 fgDebugCallback fgRegisterDebugCallback( fgDebugCallback callback ) {
219     fgDebugCallback old;
220     FG_GRABDEBUGSEM;
221     old = fg_DebugCallback;
222     fg_DebugCallback = callback;
223     FG_RELEASEDEBUGSEM;
224     return old;
225 }
226
227
228 /* fgPrintf ===============================================================*/
229 int fgPrintf( fgDebugClass dbg_class, fgDebugPriority prio, char *fmt, ... ) {
230     char szOut[1024+1];
231     va_list ap;
232     int ret = 0;
233
234     // If no action to take, then don't bother with the semaphore
235     // activity Slight speed benefit.
236
237     // printf("dbg_class = %d  fg_DebugClass = %d\n", dbg_class, fg_DebugClass);
238     // printf("prio = %d  fg_DebugPriority = %d\n", prio, fg_DebugPriority);
239
240     if( !(dbg_class & fg_DebugClass) ) {
241         // Failed to match a specific debug class
242         if ( prio < fg_DebugPriority ) {
243             // priority is less than requested
244
245             // "ret" is zero anyway. But we might think about changing
246             // it upon some error condition?
247             return ret;
248         }
249     }
250
251     FG_GRABDEBUGSEM;
252
253     /* ret = vsprintf( szOut, fmt, (&fmt+1)); (but it didn't work, thus ... */
254     va_start (ap, fmt);
255     ret = vsprintf( szOut, fmt, ap);
256     va_end (ap);
257
258     if( fg_DebugCallback!=NULL && fg_DebugCallback(dbg_class, prio, szOut) ) {
259         FG_RELEASEDEBUGSEM;
260         return ret;
261     } else {
262         fprintf( fg_DebugOutput, szOut );
263         FG_RELEASEDEBUGSEM;
264         if( prio == FG_EXIT ) {
265             exit(0);
266         } else if( prio == FG_ABORT ) {
267             abort();
268         }
269     }
270     return ret;  
271 }
272
273
274 /* $Log$
275 /* Revision 1.2  1998/04/21 17:03:45  curt
276 /* Prepairing for C++ integration.
277 /*
278  * Revision 1.1  1998/04/18 03:52:04  curt
279  * Moved to Lib directory and created a libDebug.
280  *
281  * Revision 1.10  1998/03/14 00:31:21  curt
282  * Beginning initial terrain texturing experiments.
283  *
284  * Revision 1.9  1998/03/09 22:44:58  curt
285  * Modified so that you can specify FG_DEBUGCLASS ***or*** FG_DEBUG_PRIORITY
286  *
287  * Revision 1.8  1998/03/09 22:11:00  curt
288  * Processed through the format-o-matic.
289  *
290  * Revision 1.7  1998/02/16 13:39:43  curt
291  * Miscellaneous weekend tweaks.  Fixed? a cache problem that caused whole
292  * tiles to occasionally be missing.
293  *
294  */