]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/sys/linux/glimp_logging.cpp.m4
hello world
[icculus/iodoom3.git] / neo / sys / linux / glimp_logging.cpp.m4
1 #include "idlib/precompiled.h"
2 #include "renderer/tr_local.h"
3 #include "sys/linux/local.h"
4 #include "glimp_local.h"
5 #pragma hdrstop
6
7 #include <unistd.h>
8 #define ID_LOG_TO_STDOUT 0
9
10 dnl =====================================================
11 dnl utils
12 dnl =====================================================
13
14 define(`forloop', 
15         `pushdef(`$1', `$2')_forloop(`$1', `$2', `$3', `$4')popdef(`$1')')
16 define(`_forloop',
17         `$4`'ifelse($1, `$3', ,
18         `define(`$1', incr($1))_forloop(`$1', `$2', `$3', `$4')')')
19         
20 dnl =====================================================
21 dnl the gl wgl glX definitions
22 dnl =====================================================
23 include(../gllog/gl_def.m4)
24
25 dnl =====================================================
26 dnl logging functions
27 dnl =====================================================
28
29 typedef struct {
30         GLenum  e;
31         const char *name;
32 } glEnumName_t;
33
34 #define DEF(x) { x, #x },
35
36 glEnumName_t    glEnumNames[] = {
37 #include "sys/linux/glimp_glenum.h"
38         { 0, NULL }
39 };
40
41 /*
42 ======================
43 EnumString
44 ======================
45 */
46 static const char *EnumString( GLenum t )
47 {
48         static char buffer[8][1024];
49         static int index = 0;
50
51         for ( glEnumName_t *n = glEnumNames ; n->name ; n++ ) {
52                 if ( t == n->e ) {
53                         return n->name;
54                 }
55         }
56
57         int oldIndex = index;
58         index = ( index + 1 ) & 7;
59         sprintf( buffer[oldIndex], "0x%x", t );
60
61         return buffer[oldIndex];
62 }
63
64 /*
65 ======================
66 FloatData
67 ======================
68 */
69 static const char *FloatData( const GLfloat *v, int count ) {
70         static char buffer[8][1024];
71         static int index = 0;
72         char *name;
73
74         name = buffer[index&7];
75         sprintf( name, "f%i", index );
76         index++;
77
78         fprintf( tr.logFile, "static float %s[%i] = {", name, count );
79         for( int i = 0 ; i < count ; i++ ) {
80                 if ( i < count - 1 ) {
81                         fprintf( tr.logFile, "%f,", v[i] );
82                 } else {
83                         fprintf( tr.logFile, "%f};\n", v[i] );
84                 }
85         }
86
87         return name;
88 }
89
90 #include "glimp_logfuncs.cpp"
91
92 dnl define(`log_func', `static `$1' APIENTRY log`$2'(`$3') {
93 dnl }')
94 dnl forloop(`i', gl_start, gl_end, `log_func(indir(`f'i`_ret'), indir(`f'i`_name'), indir(`f'i`_params'))
95 dnl ')
96 dnl forloop(`i', glX_start, glX_end, `log_func(indir(`f'i`_ret'), indir(`f'i`_name'), indir(`f'i`_params'))
97 dnl ')
98
99 /*
100 ======================
101 GLimp_BindLogging
102 ======================
103 */
104 void GLimp_BindLogging() {
105 define(`assign_funcptr', `qgl`$1' = log`$1';')
106 forloop(`i', gl_start, gl_end, `assign_funcptr(indir(`f'i`_name'))
107 ')
108
109 define(`assign_funcptr', `qglX`$1' = log`$1';')
110 forloop(`i', glX_start, glX_end, `assign_funcptr(indir(`f'i`_name'))
111 ')
112 }
113
114 /*
115 ======================
116 GLimp_EnableLogging
117 ======================
118 */
119 void GLimp_EnableLogging(bool enable) {
120         static bool             isEnabled = false;
121         static idStr    ospath;
122         static int              initialFrames;
123
124         // return if we're already active
125         if ( isEnabled && enable ) {
126                 // decrement log counter and stop if it has reached 0
127                 r_logFile.SetInteger( r_logFile.GetInteger() - 1 );
128                 if ( r_logFile.GetInteger() ) {
129                         return;
130                 }
131 #if ID_LOG_TO_STDOUT
132                 common->Printf( "end stdout GL loggging after %i frames.\n", initialFrames );
133 #else
134                 common->Printf( "closing GL logfile '%s' after %i frames.\n", ospath.c_str(), initialFrames );
135
136                 fclose( tr.logFile );
137 #endif
138                 enable = false;
139                 tr.logFile = NULL;
140         }
141
142         // return if we're already disabled
143         if ( !enable && !isEnabled ) {
144                 return;
145         }
146
147         isEnabled = enable;
148
149         if ( enable ) {
150                 if ( !tr.logFile ) {
151                         struct tm               *newtime;
152                         time_t                  aclock;
153                         idStr                   qpath;
154                         int                             i;
155
156                         initialFrames = r_logFile.GetInteger();
157
158 #if ID_LOG_TO_STDOUT
159                         tr.logFile = fdopen( STDOUT_FILENO, "w" );
160 #else
161                         // scan for an unused filename
162                         for ( i = 0 ; i < 9999 ; i++ ) {
163                                 sprintf( qpath, "renderlog_%i.txt", i ); 
164                                 if ( fileSystem->ReadFile( qpath, NULL, NULL ) == -1 ) {
165                                         break;          // use this name
166                                 }
167                         }
168
169                         ospath = fileSystem->RelativePathToOSPath( qpath );
170                         tr.logFile = fopen( ospath, "wt" );
171 #endif
172
173                         // write the time out to the top of the file
174                         time( &aclock );
175                         newtime = localtime( &aclock );
176                         fprintf( tr.logFile, "// %s", asctime( newtime ) );
177                         fprintf( tr.logFile, "// %s\n\n", com_version.GetString() );
178                 }
179
180                 GLimp_BindLogging();
181         } else {
182
183                 GLimp_BindNative();
184         }
185 }