glimp_logging.cpp.m4 4.3 KB

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