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.

428 lines
8.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1999
  6. //
  7. // File: log.cpp
  8. //
  9. // Contents: cert server logging
  10. //
  11. //--------------------------------------------------------------------------
  12. #include <pch.cpp>
  13. #pragma hdrstop
  14. #include "csprop.h"
  15. #include "initcert.h"
  16. #include "certmsg.h"
  17. #include "clibres.h"
  18. #define __dwFILE__ __dwFILE_CERTLIB_LOG_CPP__
  19. FILE *g_pfLog = NULL;
  20. BOOL g_fLogClosed = FALSE;
  21. BOOL g_fInitLogCritSecEnabled = FALSE;
  22. extern FNLOGMESSAGEBOX *g_pfnLogMessagBox = NULL;
  23. extern HINSTANCE g_hInstance;
  24. CRITICAL_SECTION g_InitLogCriticalSection;
  25. VOID
  26. fputsStripCRA(
  27. IN char const *psz,
  28. IN FILE *pf)
  29. {
  30. while ('\0' != *psz)
  31. {
  32. DWORD i;
  33. i = strcspn(psz, "\r");
  34. if (0 != i)
  35. {
  36. fprintf(pf, "%.*hs", i, psz);
  37. psz += i;
  38. }
  39. if ('\r' == *psz)
  40. {
  41. psz++;
  42. }
  43. }
  44. }
  45. VOID
  46. fputsUTF8(
  47. IN FILE *pf,
  48. IN WCHAR const *pwc,
  49. IN DWORD cwc)
  50. {
  51. CHAR *psz;
  52. if (myConvertWszToUTF8(&psz, pwc, cwc))
  53. {
  54. fprintf(pf, "%hs", psz);
  55. LocalFree(psz);
  56. }
  57. else
  58. {
  59. fprintf(pf, "%.*ws", cwc, pwc);
  60. }
  61. }
  62. VOID
  63. fputsStripCRW(
  64. IN WCHAR const *pwsz,
  65. IN FILE *pf)
  66. {
  67. while (L'\0' != *pwsz)
  68. {
  69. DWORD i;
  70. i = wcscspn(pwsz, L"\r");
  71. if (0 != i)
  72. {
  73. fputsUTF8(pf, pwsz, i);
  74. pwsz += i;
  75. }
  76. if ('\r' == *pwsz)
  77. {
  78. pwsz++;
  79. }
  80. }
  81. }
  82. VOID
  83. csiLog(
  84. IN DWORD dwFile,
  85. IN DWORD dwLine,
  86. IN HRESULT hrMsg,
  87. IN UINT idMsg,
  88. OPTIONAL IN WCHAR const *pwsz1,
  89. OPTIONAL IN WCHAR const *pwsz2,
  90. OPTIONAL IN DWORD const *pdw)
  91. {
  92. HRESULT hr;
  93. WCHAR const *pwszMsg = NULL; // don't free
  94. WCHAR const *pwszMsgNoCache = NULL;
  95. WCHAR const *pwszMessageText = NULL;
  96. WCHAR awchr[cwcHRESULTSTRING];
  97. BOOL fCritSecEntered = FALSE;
  98. if (0 != idMsg)
  99. {
  100. if (g_fLogClosed)
  101. {
  102. pwszMsgNoCache = myLoadResourceStringNoCache(g_hInstance, idMsg);
  103. pwszMsg = pwszMsgNoCache;
  104. }
  105. else
  106. {
  107. pwszMsg = myLoadResourceString(idMsg);
  108. }
  109. if (NULL == pwszMsg)
  110. {
  111. hr = myHLastError();
  112. _PrintError(hr, "myLoadResourceString");
  113. }
  114. }
  115. if (g_fInitLogCritSecEnabled)
  116. {
  117. EnterCriticalSection(&g_InitLogCriticalSection);
  118. fCritSecEntered = TRUE;
  119. }
  120. if (NULL != g_pfLog)
  121. {
  122. fprintf(g_pfLog, "%u.%u.%u", dwFile, dwLine, idMsg);
  123. if (NULL != pwszMsg)
  124. {
  125. fprintf(g_pfLog, ": ");
  126. fputsStripCRW(pwszMsg, g_pfLog);
  127. }
  128. if (NULL != pwsz1)
  129. {
  130. fprintf(g_pfLog, ": ");
  131. fputsStripCRW(pwsz1, g_pfLog);
  132. }
  133. if (NULL != pwsz2)
  134. {
  135. fprintf(g_pfLog, ": ");
  136. fputsStripCRW(pwsz2, g_pfLog);
  137. }
  138. if (NULL != pdw)
  139. {
  140. fprintf(g_pfLog, ": 0x%x(%d)", *pdw, *pdw);
  141. }
  142. if (hrMsg != S_OK)
  143. {
  144. pwszMessageText = myGetErrorMessageText(hrMsg, TRUE);
  145. if (NULL == pwszMessageText)
  146. {
  147. pwszMessageText = myHResultToStringRaw(awchr, hrMsg);
  148. }
  149. fprintf(g_pfLog, ": ");
  150. fputsStripCRW(pwszMessageText, g_pfLog);
  151. }
  152. fprintf(g_pfLog, "\n");
  153. fflush(g_pfLog);
  154. }
  155. //error:
  156. if (fCritSecEntered)
  157. {
  158. LeaveCriticalSection(&g_InitLogCriticalSection);
  159. }
  160. if (NULL != pwszMsgNoCache)
  161. {
  162. LocalFree(const_cast<WCHAR *>(pwszMsgNoCache));
  163. }
  164. if (NULL != pwszMessageText && awchr != pwszMessageText)
  165. {
  166. LocalFree(const_cast<WCHAR *>(pwszMessageText));
  167. }
  168. }
  169. VOID
  170. csiLogFileVersion(
  171. IN DWORD dwFile,
  172. IN DWORD dwLine,
  173. IN UINT idMsg,
  174. IN WCHAR const *pwszFile,
  175. IN char const *pszVersion)
  176. {
  177. HRESULT hr;
  178. WCHAR *pwszVersion = NULL;
  179. WCHAR const*pwsz;
  180. hr = myGetColumnDisplayName(wszPROPCERTCLIDLL_VERSION, &pwsz);
  181. _PrintIfErrorStr(hr, "myGetColumnDisplayName", wszPROPCERTCLIDLL_VERSION);
  182. csiLog(dwFile, dwLine, hr, idMsg, L"certcli.dll", pwsz, NULL);
  183. hr = S_OK;
  184. if (!myConvertSzToWsz(&pwszVersion, pszVersion, -1))
  185. {
  186. hr = E_OUTOFMEMORY;
  187. _PrintError(hr, "myConvertSzToWsz");
  188. }
  189. csiLog(dwFile, dwLine, hr, idMsg, pwszFile, pwszVersion, NULL);
  190. //error:
  191. if (NULL != pwszVersion)
  192. {
  193. LocalFree(pwszVersion);
  194. }
  195. }
  196. VOID
  197. csiLogTime(
  198. IN DWORD dwFile,
  199. IN DWORD dwLine,
  200. IN UINT idMsg)
  201. {
  202. HRESULT hr;
  203. WCHAR *pwszDate = NULL;
  204. SYSTEMTIME st;
  205. FILETIME ft;
  206. GetSystemTime(&st);
  207. if (!SystemTimeToFileTime(&st, &ft))
  208. {
  209. hr = myHLastError();
  210. _PrintError(hr, "SystemTimeToFileTime");
  211. }
  212. else
  213. {
  214. hr = myGMTFileTimeToWszLocalTime(&ft, TRUE, &pwszDate);
  215. _PrintIfError(hr, "myGMTFileTimeToWszLocalTime");
  216. }
  217. csiLog(dwFile, dwLine, S_OK, idMsg, pwszDate, NULL, NULL);
  218. //error:
  219. if (NULL != pwszDate)
  220. {
  221. LocalFree(pwszDate);
  222. }
  223. }
  224. VOID
  225. csiLogDWord(
  226. IN DWORD dwFile,
  227. IN DWORD dwLine,
  228. IN UINT idMsg,
  229. IN DWORD dwVal)
  230. {
  231. csiLog(dwFile, dwLine, S_OK, idMsg, NULL, NULL, &dwVal);
  232. }
  233. FNLOGSTRING csiLogString;
  234. VOID
  235. csiLogString(
  236. IN char const *psz)
  237. {
  238. BOOL fCritSecEntered = FALSE;
  239. if (NULL != g_pfLog)
  240. {
  241. if (g_fInitLogCritSecEnabled)
  242. {
  243. EnterCriticalSection(&g_InitLogCriticalSection);
  244. fCritSecEntered = TRUE;
  245. }
  246. fputsStripCRA(psz, g_pfLog);
  247. fflush(g_pfLog);
  248. }
  249. if (fCritSecEntered)
  250. {
  251. LeaveCriticalSection(&g_InitLogCriticalSection);
  252. }
  253. }
  254. FNLOGMESSAGEBOX csiLogMessagBox;
  255. VOID
  256. csiLogMessagBox(
  257. IN HRESULT hrMsg,
  258. IN UINT idMsg,
  259. IN WCHAR const *pwszTitle,
  260. IN WCHAR const *pwszMessage)
  261. {
  262. // Use file number 0 and the passed idMsg as the line number.
  263. csiLog(0, idMsg, hrMsg, IDS_ILOG_MESSAGEBOX, pwszTitle, pwszMessage, NULL);
  264. }
  265. VOID
  266. csiLogClose()
  267. {
  268. BOOL fCritSecEntered = FALSE;
  269. BOOL fDelete;
  270. g_fLogClosed = TRUE;
  271. if (NULL != g_pfLog)
  272. {
  273. CSILOGTIME(IDS_ILOG_END);
  274. }
  275. if (g_fInitLogCritSecEnabled)
  276. {
  277. EnterCriticalSection(&g_InitLogCriticalSection);
  278. fCritSecEntered = TRUE;
  279. }
  280. if (NULL != g_pfLog)
  281. {
  282. fclose(g_pfLog);
  283. g_pfLog = NULL;
  284. }
  285. fDelete = FALSE;
  286. if (fCritSecEntered)
  287. {
  288. if (g_fInitLogCritSecEnabled)
  289. {
  290. g_fInitLogCritSecEnabled = FALSE;
  291. fDelete = TRUE;
  292. }
  293. LeaveCriticalSection(&g_InitLogCriticalSection);
  294. }
  295. if (fDelete)
  296. {
  297. DeleteCriticalSection(&g_InitLogCriticalSection);
  298. }
  299. }
  300. char const szHeader[] = "\n========================================================================\n";
  301. VOID
  302. csiLogOpen(
  303. IN char const *pszFile)
  304. {
  305. HRESULT hr;
  306. UINT cch;
  307. char aszLogFile[MAX_PATH];
  308. BOOL fAppend = FALSE;
  309. BOOL fOk = FALSE;
  310. BOOL fCritSecEntered = FALSE;
  311. __try
  312. {
  313. if (!g_fInitLogCritSecEnabled)
  314. {
  315. InitializeCriticalSection(&g_InitLogCriticalSection);
  316. g_fInitLogCritSecEnabled = TRUE;
  317. }
  318. }
  319. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  320. {
  321. }
  322. if (g_fInitLogCritSecEnabled)
  323. {
  324. EnterCriticalSection(&g_InitLogCriticalSection);
  325. fCritSecEntered = TRUE;
  326. }
  327. if ('+' == *pszFile)
  328. {
  329. pszFile++;
  330. fAppend = TRUE;
  331. }
  332. cch = GetWindowsDirectoryA(aszLogFile, ARRAYSIZE(aszLogFile));
  333. if (0 != cch)
  334. {
  335. if (L'\\' != aszLogFile[cch - 1])
  336. {
  337. strcat(aszLogFile, "\\");
  338. }
  339. strcat(aszLogFile, pszFile);
  340. for (;;)
  341. {
  342. g_pfLog = fopen(aszLogFile, fAppend? "at" : "wt");
  343. if (NULL == g_pfLog)
  344. {
  345. _PrintError(E_FAIL, "fopen(Log)");
  346. }
  347. else
  348. {
  349. if (fAppend)
  350. {
  351. if (0 == fseek(g_pfLog, 0L, SEEK_END) &&
  352. CBLOGMAXAPPEND <= ftell(g_pfLog))
  353. {
  354. fclose(g_pfLog);
  355. g_pfLog = NULL;
  356. fAppend = FALSE;
  357. continue;
  358. }
  359. fwrite(szHeader, SZARRAYSIZE(szHeader), 1, g_pfLog);
  360. }
  361. fOk = TRUE;
  362. }
  363. break;
  364. }
  365. }
  366. if (fCritSecEntered)
  367. {
  368. LeaveCriticalSection(&g_InitLogCriticalSection);
  369. }
  370. if (fOk)
  371. {
  372. CSILOGTIME(IDS_ILOG_BEGIN);
  373. DbgLogStringInit(csiLogString);
  374. CSASSERT(NULL == g_pfnLogMessagBox);
  375. g_pfnLogMessagBox = csiLogMessagBox;
  376. }
  377. //error:
  378. ;
  379. }