Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
4.4 KiB

  1. /*****************************************\
  2. * Data Logging -- Debug only *
  3. \*****************************************/
  4. //
  5. // Precompiled header
  6. // Note -- this is not required for this modules.
  7. // It is included only to allow use of precompiled header.
  8. //
  9. #include "local.h"
  10. #if 0
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #endif
  16. #pragma hdrstop
  17. #include "logit.h"
  18. // #if DBG
  19. int LoggingMode;
  20. time_t long_time; // has to be in DS, assumed by time() funcs
  21. int LineCount;
  22. char *month[] =
  23. {
  24. "January",
  25. "February",
  26. "March",
  27. "April",
  28. "May",
  29. "June",
  30. "July",
  31. "August",
  32. "September",
  33. "October",
  34. "November",
  35. "December"
  36. } ;
  37. /*
  38. - LogInit
  39. -
  40. * Purpose:
  41. * Determines if logging is desired and if so, adds a header to log file.
  42. *
  43. * Parameters:
  44. *
  45. */
  46. void LogInit( LPSTR Filename )
  47. {
  48. FILE *fp;
  49. struct tm *newtime;
  50. char am_pm[] = "a.m.";
  51. LoggingMode = 0;
  52. LineCount = 0;
  53. if ( fp = fopen( Filename, "r+" ) )
  54. {
  55. LoggingMode = 1;
  56. fclose( fp );
  57. // Get time and date information
  58. long_time = time( NULL); /* Get time as long integer. */
  59. newtime = localtime( &long_time ); /* Convert to local time. */
  60. if( newtime->tm_hour > 12 ) /* Set up extension. */
  61. am_pm[0] = 'p';
  62. if( newtime->tm_hour > 12 ) /* Convert from 24-hour */
  63. newtime->tm_hour -= 12; /* to 12-hour clock. */
  64. if( newtime->tm_hour == 0 ) /*Set hour to 12 if midnight. */
  65. newtime->tm_hour = 12;
  66. // Write out a header to file
  67. fp = fopen(Filename, "a" );
  68. fprintf( fp, "Logging information for DNS API source file\n" );
  69. fprintf( fp, "****************************************************\n" );
  70. fprintf( fp, "\tTime: %d:%02d %s\n\tDate: %s %d, 19%d\n",
  71. newtime->tm_hour, newtime->tm_min, am_pm,
  72. month[newtime->tm_mon], newtime->tm_mday,
  73. newtime->tm_year );
  74. fprintf( fp, "****************************************************\n\n" );
  75. fclose( fp );
  76. }
  77. }
  78. /*
  79. - LogIt
  80. -
  81. * Purpose:
  82. * Formats a string and prints it to a log file with handle hLog.
  83. *
  84. * Parameters:
  85. * LPSTR - Pointer to string to format
  86. * ... - variable argument list
  87. */
  88. void CDECL LogIt( LPSTR Filename, char * lpszFormat, ... )
  89. {
  90. FILE * fp;
  91. va_list pArgs;
  92. char szLogStr[1024];
  93. int i;
  94. if ( !LoggingMode )
  95. return;
  96. va_start( pArgs, lpszFormat);
  97. vsprintf(szLogStr, lpszFormat, pArgs);
  98. va_end(pArgs);
  99. i = strlen( szLogStr);
  100. szLogStr[i] = '\n';
  101. szLogStr[i+1] = '\0';
  102. if ( LineCount > 50000 )
  103. {
  104. fp = fopen( Filename, "w" );
  105. LineCount = 0;
  106. }
  107. else
  108. {
  109. fp = fopen( Filename, "a" );
  110. }
  111. if( fp)
  112. { // if we can't open file, do nothing
  113. fprintf( fp, szLogStr );
  114. LineCount++;
  115. fclose( fp );
  116. }
  117. }
  118. void LogTime( LPSTR Filename )
  119. {
  120. struct tm *newtime;
  121. char am_pm[] = "a.m.";
  122. if ( !LoggingMode )
  123. return;
  124. // Get time and date information
  125. long_time = time( NULL); /* Get time as long integer. */
  126. newtime = localtime( &long_time ); /* Convert to local time. */
  127. if ( !newtime )
  128. return;
  129. if( newtime->tm_hour > 12 ) /* Set up extension. */
  130. am_pm[0] = 'p';
  131. if( newtime->tm_hour > 12 ) /* Convert from 24-hour */
  132. newtime->tm_hour -= 12; /* to 12-hour clock. */
  133. if( newtime->tm_hour == 0 ) /*Set hour to 12 if midnight. */
  134. newtime->tm_hour = 12;
  135. // Write out a header to file
  136. LogIt( Filename, "DNS CLIENT API" );
  137. LogIt( Filename, "System Time Information" );
  138. LogIt( Filename, "****************************************************" );
  139. LogIt( Filename, "\tTime: %d:%02d %s\n\tDate: %s %d, 19%d",
  140. newtime->tm_hour, newtime->tm_min, am_pm,
  141. month[newtime->tm_mon], newtime->tm_mday,
  142. newtime->tm_year );
  143. LogIt( Filename, "****************************************************" );
  144. LogIt( Filename, "" );
  145. }
  146. DWORD LogIn( LPSTR Filename, char * string )
  147. {
  148. LogIt( Filename, "%s", string );
  149. return GetTickCount();
  150. }
  151. void LogOut( LPSTR Filename, char * string, DWORD InTime )
  152. {
  153. LogIt( Filename, "%s --- Duration: %ld milliseconds",
  154. string, GetTickCount() - InTime );
  155. }
  156. // #endif