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.

140 lines
3.6 KiB

  1. // Name: Mohsin Ahmed
  2. // Email: [email protected]
  3. // Date: Fri Jan 24 10:33:54 1997
  4. // File: d:/nt/PRIVATE/net/sockets/tcpsvcs/lpd/trace.c
  5. // Synopsis: Too many bugs, need to keep track in the field.
  6. #include "lpd.h"
  7. char LogFileName[1000];
  8. FILE * LogFile = NULL;
  9. // ========================================================================
  10. void
  11. LogTime( void )
  12. {
  13. time_t now;
  14. time( &now );
  15. LOGIT(( " Time %s", ctime( &now ) ));
  16. }
  17. // ========================================================================
  18. // Opens filename in append mode.
  19. // LogFileName is set to filename or debugger.
  20. // On success: LogFile handle
  21. // On failure: NULL
  22. // ========================================================================
  23. CRITICAL_SECTION csLogit;
  24. FILE *
  25. beginlogging( char * filename )
  26. {
  27. int ok;
  28. if( ! filename ){
  29. DbgPrint( "lpd No log file?\n");
  30. return 0;
  31. }
  32. if( LogFile ){
  33. DbgPrint( "lpd: Already logging.\n");
  34. return 0;
  35. }
  36. assert( LogFile == NULL );
  37. ok = ExpandEnvironmentStringsA( filename,
  38. LogFileName,
  39. sizeof( LogFileName ) );
  40. if( !ok ){
  41. DbgPrint("ExpandEnvironmentStrings(%s) failed, err=%d\n",
  42. filename, GetLastError() );
  43. strcpy( LogFileName, "<debugger>" );
  44. }else{
  45. LogFile = fopen( LogFileName, "a+" );
  46. if( LogFile == NULL ){
  47. DbgPrint( "lpd: Cannot open LogFileName=%s\n", LogFileName );
  48. strcpy( LogFileName, "<debugger>" );
  49. }
  50. }
  51. if( LogFile ){
  52. InitializeCriticalSection( &csLogit );
  53. LogTime();
  54. fprintf( LogFile, "==========================================\n");
  55. fprintf( LogFile, "lpd: LogFileName=%s\n", LogFileName );
  56. fprintf( LogFile, "built %s %s\n", __DATE__, __TIME__ );
  57. fprintf( LogFile, "from %s\n", __FILE__ );
  58. fprintf( LogFile, "==========================================\n");
  59. }else{
  60. DbgPrint("lpd: started, built %s %s.\n", __DATE__, __TIME__ );
  61. }
  62. return LogFile;
  63. }
  64. // ========================================================================
  65. // Like printf but send output to LogFile if open, else to Debugger.
  66. // ========================================================================
  67. static DWORD lasttickflush;
  68. int
  69. logit( char * format, ... )
  70. {
  71. va_list ap;
  72. char message[LEN_DbgPrint];
  73. int message_len;
  74. DWORD thistick;
  75. va_start( ap, format );
  76. message_len = vsprintf( message, format, ap );
  77. va_end( ap );
  78. assert( message_len < LEN_DbgPrint );
  79. if( LogFile ){
  80. EnterCriticalSection( &csLogit );
  81. fprintf( LogFile, message );
  82. LeaveCriticalSection( &csLogit );
  83. // Don't flush more than once a second.
  84. thistick = GetTickCount();
  85. if( abs( thistick - lasttickflush ) > 1000 ){
  86. lasttickflush = thistick;
  87. fflush( LogFile );
  88. }
  89. }else{
  90. DbgPrint( message );
  91. }
  92. return message_len;
  93. }
  94. // ========================================================================
  95. // Closes the log file if open.
  96. // ========================================================================
  97. FILE *
  98. stoplogging( FILE * LogFile )
  99. {
  100. if( LogFile ){
  101. LogTime();
  102. LOGIT(( "lpd stoplogging\n"));
  103. fclose( LogFile );
  104. LogFile = NULL;
  105. // DeleteCriticalSection( &csLogit );
  106. }
  107. DbgPrint( "lpd: stopped logging.\n" );
  108. return LogFile;
  109. }
  110. // ========================================================================