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.

113 lines
2.4 KiB

  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. void OutputDebugLog(PWSTR buf)
  5. {
  6. ULONG Bytes;
  7. char str[2048];
  8. static HANDLE hFile = NULL;
  9. Bytes = wcslen( buf );
  10. WideCharToMultiByte(CP_ACP,
  11. 0,
  12. buf,
  13. -1,
  14. str,
  15. Bytes + 4,
  16. NULL,
  17. NULL
  18. );
  19. if (hFile == NULL)
  20. {
  21. WCHAR LogFileName[MAX_PATH];
  22. if (!GetWindowsDirectory( LogFileName, sizeof(LogFileName)/sizeof(WCHAR))) LogFileName[0] = '\0';
  23. wcscat( LogFileName, L"\\mmsyslog.txt" );
  24. hFile = CreateFile(
  25. LogFileName,
  26. GENERIC_READ | GENERIC_WRITE,
  27. FILE_SHARE_READ,
  28. NULL,
  29. OPEN_ALWAYS,
  30. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
  31. NULL
  32. );
  33. SetFilePointer(hFile,0,NULL,FILE_END);
  34. }
  35. if (hFile == INVALID_HANDLE_VALUE)
  36. {
  37. return;
  38. }
  39. WriteFile(
  40. hFile,
  41. str,
  42. Bytes,
  43. &Bytes,
  44. NULL
  45. );
  46. return;
  47. }
  48. void
  49. DebugLog(
  50. PWSTR FileName,
  51. ULONG LineNumber,
  52. PWSTR FormatStr,
  53. ...
  54. )
  55. {
  56. WCHAR buf[2048];
  57. PWSTR s,p;
  58. SYSTEMTIME CurrTime;
  59. va_list arg_ptr;
  60. GetSystemTime( &CurrTime );
  61. _try
  62. {
  63. p = buf;
  64. *p = 0;
  65. swprintf( p, L"%02d:%02d:%02d.%03d ",
  66. CurrTime.wHour,
  67. CurrTime.wMinute,
  68. CurrTime.wSecond,
  69. CurrTime.wMilliseconds
  70. );
  71. p += wcslen(p);
  72. if (FileName && LineNumber)
  73. {
  74. s = wcsrchr( FileName, L'\\' );
  75. if (s)
  76. {
  77. wcscpy( p, s+1 );
  78. p += wcslen(p);
  79. swprintf( p, L" @ %d ", LineNumber );
  80. p += wcslen(p);
  81. }
  82. }
  83. va_start( arg_ptr, FormatStr );
  84. wvsprintf(p,FormatStr,arg_ptr);
  85. va_end( arg_ptr );
  86. p += wcslen(p);
  87. wcscat( p, L"\r\n" );
  88. }
  89. except(EXCEPTION_EXECUTE_HANDLER)
  90. {
  91. buf[0] = 0;
  92. }
  93. if (buf[0] == 0)
  94. {
  95. return;
  96. }
  97. OutputDebugLog(buf);
  98. return;
  99. }