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.

96 lines
2.8 KiB

  1. /*****************************************************************************\
  2. FILE: logging.cpp
  3. DESCRIPTION:
  4. Logging helper functions
  5. BryanSt 4/23/2001 (Bryan Starbuck)
  6. Copyright (C) Microsoft Corp 2001-2001. All rights reserved.
  7. \*****************************************************************************/
  8. #include <shlwapi.h> // SHGetValue, wnsprintf, SHFormatDateTime
  9. #ifndef ARRAYSIZE
  10. #define ARRAYSIZE(a) (sizeof((a))/sizeof((a)[0]))
  11. #endif // ARRAYSIZE
  12. HANDLE g_hLogFile = INVALID_HANDLE_VALUE;
  13. void WriteToLogFileA(LPCSTR pszMessage, ...)
  14. {
  15. va_list vaParamList;
  16. va_start(vaParamList, pszMessage);
  17. if (INVALID_HANDLE_VALUE == g_hLogFile)
  18. {
  19. TCHAR szPath[MAX_PATH];
  20. if (GetWindowsDirectory(szPath, ARRAYSIZE(szPath)))
  21. {
  22. PathAppend(szPath, TEXT("AutoPlay.log"));
  23. g_hLogFile = CreateFile(szPath, (GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  24. if (INVALID_HANDLE_VALUE != g_hLogFile)
  25. {
  26. CHAR szTimeDate[MAX_PATH];
  27. CHAR szHeader[MAX_PATH];
  28. FILETIME ftCurrentUTC;
  29. FILETIME ftCurrent;
  30. SYSTEMTIME stCurrent;
  31. DWORD cbWritten;
  32. SetFilePointer(g_hLogFile, 0, NULL, FILE_END);
  33. GetLocalTime(&stCurrent);
  34. SystemTimeToFileTime(&stCurrent, &ftCurrent);
  35. LocalFileTimeToFileTime(&ftCurrent, &ftCurrentUTC);
  36. SHFormatDateTimeA(&ftCurrentUTC, NULL, szTimeDate, ARRAYSIZE(szTimeDate));
  37. TCHAR szProcess[MAX_PATH];
  38. if (!GetModuleFileName(NULL, szProcess, ARRAYSIZE(szProcess)))
  39. {
  40. szProcess[0] = 0;
  41. }
  42. wnsprintfA(szHeader, ARRAYSIZE(szHeader), "\r\n\r\n%hs - (%ls)\r\n", szTimeDate, /*szUserName,*/ szProcess);
  43. WriteFile(g_hLogFile, szHeader, lstrlenA(szHeader), &cbWritten, NULL);
  44. }
  45. }
  46. }
  47. if (INVALID_HANDLE_VALUE != g_hLogFile)
  48. {
  49. CHAR szMessage[4000];
  50. DWORD cbWritten;
  51. wvsprintfA(szMessage, pszMessage, vaParamList);
  52. WriteFile(g_hLogFile, szMessage, lstrlenA(szMessage), &cbWritten, NULL);
  53. FlushFileBuffers(g_hLogFile);
  54. }
  55. va_end(vaParamList);
  56. }
  57. void WriteToLogFileW(LPCWSTR pszMessage)
  58. {
  59. DWORD cchSize = (lstrlen(pszMessage) + 1);
  60. LPSTR pszMessageAnsi = (LPSTR) LocalAlloc(LPTR, cchSize);
  61. if (pszMessageAnsi)
  62. {
  63. SHUnicodeToAnsi(pszMessage, pszMessageAnsi, cchSize);
  64. WriteToLogFileA(pszMessageAnsi);
  65. }
  66. }
  67. void CloseLogFile(void)
  68. {
  69. if (INVALID_HANDLE_VALUE != g_hLogFile)
  70. {
  71. HANDLE hLogFile = g_hLogFile;
  72. g_hLogFile = INVALID_HANDLE_VALUE;
  73. CloseHandle(hLogFile);
  74. }
  75. }