Leaked source code of windows server 2003
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.

141 lines
3.7 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 = _vsnprintf (message,LEN_DbgPrint, format, ap );
  77. message[LEN_DbgPrint-1] = '\0';
  78. va_end( ap );
  79. // assert( message_len < LEN_DbgPrint );
  80. if( LogFile ){
  81. EnterCriticalSection( &csLogit );
  82. fputs (message, LogFile);
  83. LeaveCriticalSection( &csLogit );
  84. // Don't flush more than once a second.
  85. thistick = GetTickCount();
  86. if( abs( thistick - lasttickflush ) > 1000 ){
  87. lasttickflush = thistick;
  88. fflush( LogFile );
  89. }
  90. }else{
  91. DbgPrint( message );
  92. }
  93. return message_len;
  94. }
  95. // ========================================================================
  96. // Closes the log file if open.
  97. // ========================================================================
  98. FILE *
  99. stoplogging( FILE * LogFile )
  100. {
  101. if( LogFile ){
  102. LogTime();
  103. LOGIT(( "lpd stoplogging\n"));
  104. fclose( LogFile );
  105. LogFile = NULL;
  106. // DeleteCriticalSection( &csLogit );
  107. }
  108. DbgPrint( "lpd: stopped logging.\n" );
  109. return LogFile;
  110. }
  111. // ========================================================================