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.

97 lines
3.0 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. #include <strsafe.h>
  10. #ifndef ARRAYSIZE
  11. #define ARRAYSIZE(a) (sizeof((a))/sizeof((a)[0]))
  12. #endif // ARRAYSIZE
  13. HANDLE g_hLogFile = INVALID_HANDLE_VALUE;
  14. void WriteToLogFileA(LPCSTR pszMessage, ...)
  15. {
  16. va_list vaParamList;
  17. va_start(vaParamList, pszMessage);
  18. if (INVALID_HANDLE_VALUE == g_hLogFile)
  19. {
  20. TCHAR szPath[MAX_PATH];
  21. if (GetWindowsDirectory(szPath, ARRAYSIZE(szPath)))
  22. {
  23. PathAppend(szPath, TEXT("AutoPlay.log"));
  24. g_hLogFile = CreateFile(szPath, (GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  25. if (INVALID_HANDLE_VALUE != g_hLogFile)
  26. {
  27. CHAR szTimeDate[MAX_PATH];
  28. CHAR szHeader[MAX_PATH];
  29. FILETIME ftCurrentUTC;
  30. FILETIME ftCurrent;
  31. SYSTEMTIME stCurrent;
  32. DWORD cbWritten;
  33. SetFilePointer(g_hLogFile, 0, NULL, FILE_END);
  34. GetLocalTime(&stCurrent);
  35. SystemTimeToFileTime(&stCurrent, &ftCurrent);
  36. LocalFileTimeToFileTime(&ftCurrent, &ftCurrentUTC);
  37. SHFormatDateTimeA(&ftCurrentUTC, NULL, szTimeDate, ARRAYSIZE(szTimeDate));
  38. TCHAR szProcess[MAX_PATH];
  39. if (!GetModuleFileName(NULL, szProcess, ARRAYSIZE(szProcess)))
  40. {
  41. szProcess[0] = 0;
  42. }
  43. StringCchPrintfA(szHeader, ARRAYSIZE(szHeader), "\r\n\r\n%hs - (%ls)\r\n", szTimeDate, /*szUserName,*/ szProcess);
  44. WriteFile(g_hLogFile, szHeader, lstrlenA(szHeader), &cbWritten, NULL);
  45. }
  46. }
  47. }
  48. if (INVALID_HANDLE_VALUE != g_hLogFile)
  49. {
  50. CHAR szMessage[4000];
  51. DWORD cbWritten;
  52. StringCchVPrintfA(szMessage, ARRAYSIZE(szMessage), pszMessage, vaParamList);
  53. WriteFile(g_hLogFile, szMessage, lstrlenA(szMessage), &cbWritten, NULL);
  54. FlushFileBuffers(g_hLogFile);
  55. }
  56. va_end(vaParamList);
  57. }
  58. void WriteToLogFileW(LPCWSTR pszMessage)
  59. {
  60. DWORD cchSize = (lstrlen(pszMessage) + 1);
  61. LPSTR pszMessageAnsi = (LPSTR) LocalAlloc(LPTR, cchSize);
  62. if (pszMessageAnsi)
  63. {
  64. SHUnicodeToAnsi(pszMessage, pszMessageAnsi, cchSize);
  65. WriteToLogFileA(pszMessageAnsi);
  66. }
  67. }
  68. void CloseLogFile(void)
  69. {
  70. if (INVALID_HANDLE_VALUE != g_hLogFile)
  71. {
  72. HANDLE hLogFile = g_hLogFile;
  73. g_hLogFile = INVALID_HANDLE_VALUE;
  74. CloseHandle(hLogFile);
  75. }
  76. }