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.

267 lines
5.1 KiB

  1. /*
  2. * Copyright (c) 1998 Microsoft Corporation
  3. *
  4. * Module Name:
  5. *
  6. * logfile.cpp
  7. *
  8. * Abstract:
  9. *
  10. * This file contains code to log messages to a file.
  11. *
  12. * Author:
  13. *
  14. * Breen Hagan (BreenH) Oct-02-98
  15. *
  16. * Environment:
  17. *
  18. * User Mode
  19. */
  20. #define _LSOC_LOGFILE_CPP_
  21. #include "stdafx.h"
  22. #include "logfile.h"
  23. /*
  24. * Globals.
  25. */
  26. LogFile SetupLog;
  27. /*
  28. * Constants.
  29. */
  30. const UINT LOG_ENTRY_SIZE = 1024;
  31. const UINT S_SIZE = 1024;
  32. /*
  33. * Function prototypes.
  34. */
  35. DWORD TCharStringToAnsiString(LPCTSTR, LPSTR);
  36. /*
  37. * Class LogFile.
  38. */
  39. LogFile::LogFile(
  40. )
  41. {
  42. m_fInitialized = FALSE;
  43. m_szLogFile[0] = (TCHAR)NULL;
  44. m_szLogModule[0] = (TCHAR)NULL;
  45. }
  46. LogFile::~LogFile(
  47. )
  48. {
  49. }
  50. VOID
  51. LogFile::Close(
  52. VOID
  53. )
  54. {
  55. if (m_fInitialized) {
  56. LogMessage(_T(CRLF));
  57. LogMessage(_T("**"));
  58. LogMessage(_T("** Closing Message Log for %s"), m_szLogModule);
  59. LogMessage(_T("**"));
  60. LogMessage(_T(CRLF));
  61. LogMessage(_T(CRLF));
  62. CloseHandle(m_hFile);
  63. m_fInitialized = FALSE;
  64. }
  65. }
  66. DWORD
  67. LogFile::Initialize(
  68. IN LPCTSTR pszLogFile,
  69. IN LPCTSTR pszLogModule
  70. )
  71. {
  72. OSVERSIONINFO osVersion;
  73. TCHAR pszDate[S_SIZE];
  74. TCHAR pszTime[S_SIZE];
  75. //
  76. // Initializing the log file twice is "A Bad Thing."
  77. //
  78. if (m_fInitialized) {
  79. LogMessage(_T("LogFile::Initialize called twice!"));
  80. return(ERROR_SUCCESS);
  81. }
  82. //
  83. // Sanity checks. Pointless in a limited setting, but useful if this
  84. // file is copied to other projects.
  85. //
  86. if ((pszLogFile == NULL) || (pszLogFile[0] == (TCHAR)NULL)) {
  87. return(ERROR_INVALID_PARAMETER);
  88. }
  89. if ((pszLogModule == NULL) || (pszLogModule[0] == (TCHAR)NULL)) {
  90. return(ERROR_INVALID_PARAMETER);
  91. }
  92. if ((_tcslen(pszLogFile) > MAX_PATH) ||
  93. (_tcslen(pszLogModule) > MAX_PATH)) {
  94. return(ERROR_INVALID_PARAMETER);
  95. }
  96. //
  97. // Save the log file and module name.
  98. //
  99. _tcscpy(m_szLogFile, pszLogFile);
  100. _tcscpy(m_szLogModule, pszLogModule);
  101. //
  102. // Open or create the log file.
  103. //
  104. m_hFile = CreateFile(
  105. pszLogFile,
  106. GENERIC_WRITE,
  107. 0,
  108. NULL,
  109. OPEN_ALWAYS,
  110. 0,
  111. NULL
  112. );
  113. if (m_hFile == INVALID_HANDLE_VALUE) {
  114. return(GetLastError());
  115. }
  116. m_fInitialized = TRUE;
  117. SetFilePointer(m_hFile, 0, NULL, FILE_END);
  118. //
  119. // Get the current date and time for the log file.
  120. //
  121. _tstrdate(pszDate);
  122. _tstrtime(pszTime);
  123. LogMessage(_T("**"));
  124. LogMessage(_T("** Initializing Message Log for %s"), m_szLogModule);
  125. LogMessage(_T("** Date: %s Time: %s"), pszDate, pszTime);
  126. LogMessage(_T("**"));
  127. LogMessage(_T(CRLF));
  128. //
  129. // Log information on the OS version.
  130. //
  131. osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  132. if (GetVersionEx(&osVersion) != 0) {
  133. LogMessage(
  134. _T("Version: %lu.%lu.%lu Platform: %lu, %s"),
  135. osVersion.dwMajorVersion,
  136. osVersion.dwMinorVersion,
  137. osVersion.dwBuildNumber,
  138. osVersion.dwPlatformId,
  139. #ifdef DBG
  140. _T("Checked")
  141. #else
  142. _T("Free")
  143. #endif
  144. );
  145. }
  146. return(ERROR_SUCCESS);
  147. }
  148. /*
  149. * LogFile::LogMessage()
  150. *
  151. *
  152. */
  153. DWORD
  154. __cdecl
  155. LogFile::LogMessage(
  156. LPCTSTR pszFormat,
  157. ...
  158. )
  159. {
  160. CHAR cszOutput[LOG_ENTRY_SIZE];
  161. DWORD cBytes;
  162. DWORD cLength;
  163. TCHAR tszOutput[LOG_ENTRY_SIZE];
  164. va_list vaList;
  165. if (!m_fInitialized) {
  166. return(ERROR_INVALID_HANDLE);
  167. }
  168. SetLastError(ERROR_SUCCESS);
  169. va_start(vaList, pszFormat);
  170. _vstprintf(tszOutput, pszFormat, vaList);
  171. va_end(vaList);
  172. cLength = TCharStringToAnsiString(tszOutput, cszOutput);
  173. if (cLength != (DWORD)-1) {
  174. WriteFile(m_hFile, cszOutput, cLength * sizeof(char), &cBytes, NULL);
  175. WriteFile(m_hFile, CRLF, strlen(CRLF) * sizeof(char), &cBytes, NULL);
  176. }
  177. return(GetLastError());
  178. }
  179. /*
  180. *
  181. *
  182. *
  183. */
  184. DWORD
  185. TCharStringToAnsiString(
  186. LPCTSTR tszStr,
  187. LPSTR cszStr
  188. )
  189. {
  190. #ifdef UNICODE
  191. DWORD cLength;
  192. cLength = WideCharToMultiByte(
  193. CP_ACP,
  194. 0,
  195. tszStr,
  196. -1,
  197. NULL,
  198. 0,
  199. NULL,
  200. NULL
  201. );
  202. if ((cLength == 0) || (cLength > S_SIZE)) {
  203. return((DWORD)-1);
  204. }
  205. cLength = WideCharToMultiByte(
  206. CP_ACP,
  207. 0,
  208. tszStr,
  209. -1,
  210. cszStr,
  211. cLength,
  212. NULL,
  213. NULL
  214. );
  215. return(cLength);
  216. #else
  217. _tcscpy(cszStr, tszStr);
  218. return(_tcslen(cszStr));
  219. #endif
  220. }