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.

258 lines
5.0 KiB

  1. /**************************************************************************************************
  2. FILENAME: LogFile.cpp
  3. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  4. */
  5. #include "stdafx.h"
  6. #include <windows.h>
  7. #define THIS_MODULE 'L'
  8. #include "LogFile.h"
  9. #include "stdio.h"
  10. #define g_szInitialHeader "\r\n------------------------ New Log ---------------------\r\n"
  11. static HANDLE hLog = NULL;
  12. #include "secattr.h"
  13. /**************************************************************************************************
  14. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  15. ROUTINE DESCRIPTION:
  16. GLOBALS:
  17. INPUT:
  18. RETURN:
  19. TRUE - Success
  20. FALSE - Failure (indicates that the error log could not be created)
  21. */
  22. BOOL
  23. InitializeLogFile(
  24. IN TCHAR* pLogName
  25. )
  26. {
  27. DWORD dwBytes = 0;
  28. SECURITY_ATTRIBUTES saSecurityAttributes;
  29. SECURITY_DESCRIPTOR sdSecurityDescriptor;
  30. ZeroMemory(&sdSecurityDescriptor, sizeof(SECURITY_DESCRIPTOR));
  31. if (!pLogName) {
  32. return FALSE;
  33. }
  34. saSecurityAttributes.nLength = sizeof (saSecurityAttributes);
  35. saSecurityAttributes.lpSecurityDescriptor = &sdSecurityDescriptor;
  36. saSecurityAttributes.bInheritHandle = FALSE;
  37. if (!ConstructSecurityAttributes(&saSecurityAttributes, esatFile, FALSE)) {
  38. return FALSE;
  39. }
  40. // Make sure that we can Create/Open the Log file
  41. hLog = CreateFile(
  42. pLogName,
  43. GENERIC_READ|GENERIC_WRITE,
  44. FILE_SHARE_READ|FILE_SHARE_WRITE,
  45. &saSecurityAttributes,
  46. OPEN_ALWAYS,
  47. FILE_ATTRIBUTE_NORMAL,
  48. NULL);
  49. CleanupSecurityAttributes(&saSecurityAttributes);
  50. ZeroMemory(&sdSecurityDescriptor, sizeof(SECURITY_DESCRIPTOR));
  51. if (hLog == INVALID_HANDLE_VALUE){
  52. hLog = NULL;
  53. return FALSE;
  54. }
  55. //
  56. // Add our error string
  57. //
  58. WriteFile(hLog,
  59. g_szInitialHeader,
  60. (strlen(g_szInitialHeader) * sizeof(char)),
  61. &dwBytes,
  62. NULL
  63. );
  64. return TRUE;
  65. }
  66. /**************************************************************************************************
  67. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  68. ROUTINE DESCRIPTION:
  69. GLOBALS:
  70. INPUT:
  71. None
  72. RETURN:
  73. None
  74. */
  75. void
  76. ExitLogFile(
  77. )
  78. {
  79. if(hLog){
  80. // CloseHandle(hLog);
  81. hLog = NULL;
  82. }
  83. return;
  84. }
  85. BOOL
  86. IsLoggingAvailable()
  87. {
  88. return (hLog ? TRUE : FALSE);
  89. }
  90. //
  91. // Logs the message to the Defrag log file.
  92. //
  93. VOID
  94. LogMessage(
  95. IN CONST char Module,
  96. IN CONST ULONG Line,
  97. IN CONST ULONG MesgLevel,
  98. IN CONST PCSTR Message
  99. )
  100. {
  101. LARGE_INTEGER Counter;
  102. DWORD dwBytes = 0;
  103. char buffer[4196];
  104. if (hLog) {
  105. if (!QueryPerformanceCounter(&Counter)) {
  106. Counter.QuadPart = 0;
  107. }
  108. sprintf(buffer, "[%I64d:%d%c%04lu] %s\r\n",
  109. Counter.QuadPart, MesgLevel, Module, Line, Message);
  110. //
  111. // Move to the end of file
  112. //
  113. SetFilePointer(hLog, 0L, NULL, FILE_END);
  114. //
  115. // Add our error string
  116. //
  117. WriteFile(hLog,
  118. buffer,
  119. (strlen(buffer) * sizeof(char)),
  120. &dwBytes,
  121. NULL
  122. );
  123. // DbgPrintEx(DPFLTR_SETUP_ID, MesgLevel, buffer);
  124. OutputDebugStringA(buffer);
  125. }
  126. }
  127. BOOL
  128. DebugMessage(
  129. IN CONST char Module,
  130. IN CONST ULONG Line,
  131. IN CONST ULONG MesgLevel,
  132. IN PCSTR FormatString,
  133. ...)
  134. /*++
  135. Description:
  136. This prints a debug message AND makes the appropriate entries in
  137. the log and error files.
  138. Arguments:
  139. Line pass in __LINE__
  140. MesgLevel DPFLTR_ levels
  141. FormatString Formatted Message String to be printed.
  142. Returns:
  143. --*/
  144. {
  145. char str[4096]; // the message better fit in this
  146. va_list arglist;
  147. if (hLog) {
  148. va_start(arglist, FormatString);
  149. wvsprintfA(str, FormatString, arglist);
  150. va_end(arglist);
  151. LogMessage(Module, Line, MesgLevel, str);
  152. }
  153. return TRUE;
  154. }
  155. /**************************************************************************************************
  156. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  157. ROUTINE DESCRIPTION:
  158. GLOBAL VARIABLES:
  159. INPUT:
  160. IN LPTSTR pMessage - message string.
  161. RETURN:
  162. TRUE if success, otherwise false
  163. */
  164. BOOL
  165. WriteStringToLogFileFunction(
  166. IN TCHAR* pMessage
  167. )
  168. {
  169. //If the log isn't enabled, don't write to it.
  170. if(!hLog){
  171. return FALSE;
  172. }
  173. DWORD dwNumBytesWritten;
  174. char cString[1024];
  175. #ifdef _UNICODE
  176. int numBytes = WideCharToMultiByte(CP_OEMCP,
  177. 0,
  178. pMessage,
  179. -1,
  180. cString,
  181. sizeof(cString),
  182. NULL,
  183. NULL);
  184. if (numBytes == 0){
  185. strcpy(cString, "WideCharToMultiByte() failed in WriteStringToLogFile()\r\n");
  186. WriteFile(hLog, cString, strlen(cString), &dwNumBytesWritten, NULL);
  187. }
  188. #else
  189. strcpy(cString, pMessage);
  190. #endif
  191. // Write data out to file
  192. strcat(cString, "\r\n");
  193. #ifdef DFRGNTFS
  194. Trace(log, cString);
  195. #endif
  196. return TRUE;
  197. }