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.

394 lines
11 KiB

  1. /****************************************************************************
  2. Copyright information : Copyright (c) 1998-1999 Microsoft Corporation
  3. File Name : ErrorLog.cpp
  4. Project Name : WMI Command Line
  5. Author Name : C. V. Nandi
  6. Date of Creation (dd/mm/yy) : 11th-January-2001
  7. Version Number : 1.0
  8. Brief Description : This file has all the global function definitions
  9. Revision History :
  10. Last Modified By : Ch. Sriramachandramurthy
  11. Last Modified Date : 12th-January-2001
  12. *****************************************************************************/
  13. // ErrorLog.cpp : implementation file
  14. #include "Precomp.h"
  15. #include "ErrorLog.h"
  16. /*------------------------------------------------------------------------
  17. Name :CErrorLog
  18. Synopsis :This function initializes the member variables when
  19. an object of the class type is instantiated
  20. Type :Constructor
  21. Input parameter :None
  22. Output parameters :None
  23. Return Type :None
  24. Global Variables :None
  25. Calling Syntax :None
  26. Notes :None
  27. ------------------------------------------------------------------------*/
  28. CErrorLog::CErrorLog()
  29. {
  30. m_eloErrLogOpt = NO_LOGGING;
  31. m_pszLogDir = NULL;
  32. m_bGetErrLogInfo = TRUE;
  33. m_bCreateLogFile = TRUE;
  34. m_hLogFile = NULL;
  35. }
  36. /*------------------------------------------------------------------------
  37. Name :~CErrorLog
  38. Synopsis :This function uninitializes the member variables
  39. when an object of the class type goes out of scope.
  40. Type :Destructor
  41. Input parameter :None
  42. Output parameters :None
  43. Return Type :None
  44. Global Variables :None
  45. Calling Syntax :None
  46. Notes :None
  47. ------------------------------------------------------------------------*/
  48. CErrorLog::~CErrorLog()
  49. {
  50. SAFEDELETE(m_pszLogDir);
  51. if ( m_hLogFile )
  52. CloseHandle(m_hLogFile);
  53. }
  54. /*------------------------------------------------------------------------
  55. Name :GetErrLogInfo
  56. Synopsis :This function reads the following information from
  57. the registry:
  58. 1. LoggingMode and
  59. 2. LogDirectory
  60. Type :Member Function
  61. Input parameter :None
  62. Output parameters :None
  63. Return Type :void
  64. Global Variables :None
  65. Calling Syntax :GetErrLogInfo()
  66. Notes :None
  67. ------------------------------------------------------------------------*/
  68. void CErrorLog::GetErrLogInfo()
  69. {
  70. HKEY hkKeyHandle = NULL;
  71. try
  72. {
  73. // Open the registry key
  74. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  75. _T("SOFTWARE\\Microsoft\\Wbem\\CIMOM"), 0,
  76. KEY_QUERY_VALUE, &hkKeyHandle ) == ERROR_SUCCESS )
  77. {
  78. DWORD dwBufSize = BUFFER512;
  79. TCHAR szKeyValue[BUFFER512] = NULL_STRING;
  80. _tcscpy(szKeyValue,CLI_TOKEN_NULL);
  81. // Query the "Logging" mode
  82. if ( RegQueryValueEx(hkKeyHandle,
  83. _T("Logging"), NULL, NULL,
  84. (LPBYTE)szKeyValue, &dwBufSize) == ERROR_SUCCESS )
  85. {
  86. if ( !_tcsicmp(szKeyValue, CLI_TOKEN_ONE) )
  87. m_eloErrLogOpt = ERRORS_ONLY;
  88. else if ( !_tcsicmp(szKeyValue, CLI_TOKEN_TWO) )
  89. m_eloErrLogOpt = EVERY_OPERATION;
  90. else
  91. m_eloErrLogOpt = NO_LOGGING;
  92. }
  93. _TCHAR *pszKeyValue = NULL;
  94. // Query for the content length of the "Logging Directory"
  95. if ( RegQueryValueEx(hkKeyHandle, _T("Logging Directory"), NULL,
  96. NULL, NULL, &dwBufSize) == ERROR_SUCCESS)
  97. {
  98. pszKeyValue = new _TCHAR [dwBufSize];
  99. if (pszKeyValue != NULL)
  100. {
  101. // Query the "Logging Directory"
  102. if ( RegQueryValueEx(hkKeyHandle, _T("Logging Directory"),
  103. NULL, NULL, (LPBYTE)pszKeyValue, &dwBufSize)
  104. == ERROR_SUCCESS)
  105. {
  106. SAFEDELETE(m_pszLogDir);
  107. m_pszLogDir = new _TCHAR [lstrlen(pszKeyValue) + 1];
  108. if (!m_pszLogDir)
  109. {
  110. SAFEDELETE(pszKeyValue);
  111. throw(OUT_OF_MEMORY);
  112. }
  113. lstrcpy(m_pszLogDir, pszKeyValue);
  114. }
  115. SAFEDELETE(pszKeyValue);
  116. }
  117. }
  118. // Query the "Log File Max Size"
  119. if ( RegQueryValueEx(hkKeyHandle,
  120. _T("Log File Max Size"), NULL, NULL,
  121. (LPBYTE)szKeyValue, &dwBufSize) == ERROR_SUCCESS )
  122. {
  123. m_llLogFileMaxSize = _ttol(szKeyValue);
  124. }
  125. // Close the registry key
  126. RegCloseKey(hkKeyHandle);
  127. }
  128. }
  129. catch(_com_error& e)
  130. {
  131. _com_issue_error(e.Error());
  132. }
  133. }
  134. /*------------------------------------------------------------------------
  135. Name :GetErrLogOption
  136. Synopsis :This function returns the logging mode
  137. Type :Member Function
  138. Input parameter :None
  139. Output parameters :None
  140. Return Type :
  141. ERRLOGOPT - typdefined variable
  142. Global Variables :None
  143. Calling Syntax :GetErrLogOption()
  144. Notes :None
  145. ------------------------------------------------------------------------*/
  146. ERRLOGOPT CErrorLog::GetErrLogOption()
  147. {
  148. if ( m_bGetErrLogInfo == TRUE )
  149. {
  150. GetErrLogInfo();
  151. m_bGetErrLogInfo = FALSE;
  152. }
  153. return m_eloErrLogOpt;
  154. }
  155. /*------------------------------------------------------------------------
  156. Name :CreateLogFile
  157. Synopsis :This function creates the WMIC.LOG file
  158. Type :Member Function
  159. Input parameter :None
  160. Output parameters :None
  161. Return Type :None
  162. Global Variables :None
  163. Calling Syntax :GetErrLogOption()
  164. Notes :None
  165. ------------------------------------------------------------------------*/
  166. void CErrorLog::CreateLogFile()
  167. {
  168. DWORD dwError = 0;
  169. try
  170. {
  171. if ( m_bGetErrLogInfo == TRUE )
  172. {
  173. GetErrLogInfo();
  174. m_bGetErrLogInfo = FALSE;
  175. }
  176. // Frame the file path.
  177. _bstr_t bstrFilePath = _bstr_t(m_pszLogDir);
  178. bstrFilePath += _bstr_t("WMIC.LOG");
  179. m_hLogFile = CreateFile(bstrFilePath,
  180. GENERIC_READ |GENERIC_WRITE,
  181. FILE_SHARE_READ | FILE_SHARE_WRITE,
  182. NULL,
  183. OPEN_ALWAYS,
  184. FILE_ATTRIBUTE_NORMAL,
  185. NULL);
  186. // If handle is invalid.
  187. if (m_hLogFile == INVALID_HANDLE_VALUE)
  188. {
  189. dwError = ::GetLastError();
  190. ::SetLastError(dwError);
  191. DisplayString(IDS_E_ERRLOG_OPENFAIL, CP_OEMCP,
  192. NULL, TRUE, TRUE);
  193. ::SetLastError(dwError);
  194. DisplayWin32Error();
  195. throw(dwError);
  196. }
  197. if ( SetFilePointer(m_hLogFile, 0, NULL, FILE_END)
  198. == INVALID_SET_FILE_POINTER &&
  199. dwError != NO_ERROR )
  200. {
  201. dwError = ::GetLastError();
  202. ::SetLastError(dwError);
  203. DisplayWin32Error();
  204. ::SetLastError(dwError);
  205. throw(dwError);
  206. }
  207. }
  208. catch(_com_error& e)
  209. {
  210. _com_issue_error(e.Error());
  211. }
  212. }
  213. /*------------------------------------------------------------------------
  214. Name :LogErrorOrOperation
  215. Synopsis :This function logs the error or operation result
  216. Type :Member Function
  217. Input parameter :
  218. hrErrNo - HRESULT code
  219. pszFileName - file name
  220. lLineNo - line number
  221. pszFunName - function name
  222. dwThreadId - thread id
  223. Output parameters :None
  224. Return Type :None
  225. Global Variables :None
  226. Calling Syntax :GetErrLogOption()
  227. Notes :None
  228. ------------------------------------------------------------------------*/
  229. void CErrorLog::LogErrorOrOperation(HRESULT hrErrNo, char* pszFileName,
  230. LONG lLineNo, _TCHAR* pszFunName,
  231. DWORD dwThreadId, DWORD dwError)
  232. {
  233. try
  234. {
  235. if ( pszFunName )
  236. {
  237. if ( (m_eloErrLogOpt == ERRORS_ONLY && FAILED(hrErrNo)) ||
  238. m_eloErrLogOpt == EVERY_OPERATION )
  239. {
  240. if ( m_bCreateLogFile == TRUE )
  241. {
  242. CreateLogFile();
  243. m_bCreateLogFile = FALSE;
  244. }
  245. SYSTEMTIME stSysTime;
  246. GetLocalTime(&stSysTime);
  247. CHAR szDate[BUFFER32];
  248. sprintf(szDate, "%.2d/%.2d/%.4d", stSysTime.wMonth,
  249. stSysTime.wDay,
  250. stSysTime.wYear);
  251. CHAR szTime[BUFFER32];
  252. sprintf(szTime, "%.2d:%.2d:%.2d:%.3d", stSysTime.wHour,
  253. stSysTime.wMinute,
  254. stSysTime.wSecond,
  255. stSysTime.wMilliseconds);
  256. CHString chsErrMsg;
  257. BOOL bWriteToFile = FALSE;
  258. if ( FAILED(hrErrNo) )
  259. {
  260. if (dwError)
  261. {
  262. chsErrMsg.Format(
  263. L"ERROR %s - FAILED! error# %d %s %s thread:%d [%s.%d]\r\n",
  264. CHString(pszFunName),dwError,CHString(szDate),
  265. CHString(szTime), dwThreadId,
  266. CHString(pszFileName), lLineNo);
  267. }
  268. else
  269. {
  270. chsErrMsg.Format(
  271. L"ERROR %s - FAILED! error# %x %s %s thread:%d [%s.%d]\r\n",
  272. CHString(pszFunName), hrErrNo, CHString(szDate),
  273. CHString(szTime), dwThreadId, CHString(pszFileName),
  274. lLineNo);
  275. }
  276. bWriteToFile = TRUE;
  277. }
  278. else if (_tcsnicmp(pszFunName,_T("COMMAND:"),8) == 0)
  279. {
  280. chsErrMsg.Format(
  281. L"SUCCESS %s - Succeeded %s %s thread:%d [%s.%d]\r\n",
  282. CHString(pszFunName), CHString(szDate),
  283. CHString(szTime), dwThreadId,
  284. CHString(pszFileName),lLineNo);
  285. bWriteToFile = TRUE;
  286. }
  287. _bstr_t bstrErrMsg = _bstr_t((LPCWSTR)chsErrMsg);
  288. CHAR *szErrMsg = (CHAR*)bstrErrMsg;
  289. if ( bWriteToFile == TRUE && szErrMsg != NULL)
  290. {
  291. DWORD dwNumberOfBytes = 0;
  292. LARGE_INTEGER liFileSize;
  293. if ( GetFileSizeEx(m_hLogFile, &liFileSize) == TRUE &&
  294. (liFileSize.QuadPart + strlen(szErrMsg)) >
  295. m_llLogFileMaxSize )
  296. {
  297. // Frame the file path.
  298. _bstr_t bstrLogFilePath = _bstr_t(m_pszLogDir);
  299. _bstr_t bstrCatalogFilePath = _bstr_t(m_pszLogDir);
  300. bstrLogFilePath += _bstr_t("WMIC.LOG");
  301. bstrCatalogFilePath += _bstr_t("WMIC.LO_");
  302. if(!CopyFile((LPTSTR)bstrLogFilePath,
  303. (LPTSTR)bstrCatalogFilePath,
  304. FALSE))
  305. {
  306. DWORD dwError = ::GetLastError();
  307. DisplayString(IDS_E_ERRLOG_WRITEFAIL, CP_OEMCP,
  308. NULL, TRUE, TRUE);
  309. ::SetLastError(dwError);
  310. DisplayWin32Error();
  311. ::SetLastError(dwError);
  312. throw(dwError);
  313. }
  314. // close wmic.log
  315. if ( m_hLogFile )
  316. {
  317. CloseHandle(m_hLogFile);
  318. m_hLogFile = 0;
  319. }
  320. m_hLogFile = CreateFile(bstrLogFilePath,
  321. GENERIC_READ |GENERIC_WRITE,
  322. FILE_SHARE_READ | FILE_SHARE_WRITE,
  323. NULL,
  324. CREATE_ALWAYS,
  325. FILE_ATTRIBUTE_NORMAL,
  326. NULL);
  327. // If handle is invalid.
  328. if (m_hLogFile == INVALID_HANDLE_VALUE)
  329. {
  330. dwError = ::GetLastError();
  331. ::SetLastError(dwError);
  332. DisplayString(IDS_E_ERRLOG_OPENFAIL, CP_OEMCP,
  333. NULL, TRUE, TRUE);
  334. ::SetLastError(dwError);
  335. DisplayWin32Error();
  336. throw(dwError);
  337. }
  338. }
  339. if (!WriteFile(m_hLogFile, szErrMsg, strlen(szErrMsg),
  340. &dwNumberOfBytes, NULL))
  341. {
  342. DWORD dwError = ::GetLastError();
  343. DisplayString(IDS_E_ERRLOG_WRITEFAIL, CP_OEMCP,
  344. NULL, TRUE, TRUE);
  345. ::SetLastError(dwError);
  346. DisplayWin32Error();
  347. ::SetLastError(dwError);
  348. throw(dwError);
  349. }
  350. }
  351. }
  352. }
  353. }
  354. catch(_com_error& e)
  355. {
  356. _com_issue_error(e.Error());
  357. }
  358. catch(CHeap_Exception)
  359. {
  360. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  361. }
  362. catch(DWORD dwError)
  363. {
  364. throw (dwError);
  365. }
  366. }