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.

359 lines
6.3 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 "initcert.h"
  15. #include "certmsg.h"
  16. #include "clibres.h"
  17. #define __dwFILE__ __dwFILE_CERTLIB_LOG_CPP__
  18. FILE *g_pfLog = NULL;
  19. BOOL g_fInitLogCritSecEnabled = FALSE;
  20. extern FNLOGMESSAGEBOX *g_pfnLogMessagBox = NULL;
  21. CRITICAL_SECTION g_InitLogCriticalSection;
  22. VOID
  23. fputsStripCRA(
  24. IN char const *psz,
  25. IN FILE *pf)
  26. {
  27. while ('\0' != *psz)
  28. {
  29. DWORD i;
  30. i = strcspn(psz, "\r");
  31. if (0 != i)
  32. {
  33. fprintf(pf, "%.*hs", i, psz);
  34. psz += i;
  35. }
  36. if ('\r' == *psz)
  37. {
  38. psz++;
  39. }
  40. }
  41. }
  42. VOID
  43. fputsStripCRW(
  44. IN WCHAR const *pwsz,
  45. IN FILE *pf)
  46. {
  47. while (L'\0' != *pwsz)
  48. {
  49. DWORD i;
  50. i = wcscspn(pwsz, L"\r");
  51. if (0 != i)
  52. {
  53. fprintf(pf, "%.*ws", i, pwsz);
  54. pwsz += i;
  55. }
  56. if ('\r' == *pwsz)
  57. {
  58. pwsz++;
  59. }
  60. }
  61. }
  62. VOID
  63. csiLog(
  64. IN DWORD dwFile,
  65. IN DWORD dwLine,
  66. IN HRESULT hrMsg,
  67. IN UINT idMsg,
  68. OPTIONAL IN WCHAR const *pwsz1,
  69. OPTIONAL IN WCHAR const *pwsz2,
  70. OPTIONAL IN DWORD const *pdw)
  71. {
  72. HRESULT hr;
  73. WCHAR const *pwszMsg = NULL; // don't free
  74. WCHAR const *pwszMessageText = NULL;
  75. WCHAR awchr[cwcHRESULTSTRING];
  76. BOOL fCritSecEntered = FALSE;
  77. if (0 != idMsg)
  78. {
  79. pwszMsg = myLoadResourceString(idMsg);
  80. if (NULL == pwszMsg)
  81. {
  82. hr = myHLastError();
  83. _PrintError(hr, "myLoadResourceString");
  84. }
  85. }
  86. if (NULL == pwszMsg)
  87. {
  88. pwszMsg = L"";
  89. }
  90. if (g_fInitLogCritSecEnabled)
  91. {
  92. EnterCriticalSection(&g_InitLogCriticalSection);
  93. fCritSecEntered = TRUE;
  94. }
  95. if (NULL != g_pfLog)
  96. {
  97. fprintf(g_pfLog, "%u.%u.%u: ", dwFile, dwLine, idMsg);
  98. fputsStripCRW(pwszMsg, g_pfLog);
  99. if (NULL != pwsz1)
  100. {
  101. fprintf(g_pfLog, ": ");
  102. fputsStripCRW(pwsz1, g_pfLog);
  103. }
  104. if (NULL != pwsz2)
  105. {
  106. fprintf(g_pfLog, ": ");
  107. fputsStripCRW(pwsz2, g_pfLog);
  108. }
  109. if (NULL != pdw)
  110. {
  111. fprintf(g_pfLog, ": 0x%x(%d)", *pdw, *pdw);
  112. }
  113. if (hrMsg != S_OK)
  114. {
  115. pwszMessageText = myGetErrorMessageText(hrMsg, TRUE);
  116. if (NULL == pwszMessageText)
  117. {
  118. pwszMessageText = myHResultToStringRaw(awchr, hrMsg);
  119. }
  120. fprintf(g_pfLog, ": ");
  121. fputsStripCRW(pwszMessageText, g_pfLog);
  122. }
  123. fprintf(g_pfLog, "\n");
  124. fflush(g_pfLog);
  125. }
  126. //error:
  127. if (fCritSecEntered)
  128. {
  129. LeaveCriticalSection(&g_InitLogCriticalSection);
  130. }
  131. if (NULL != pwszMessageText && awchr != pwszMessageText)
  132. {
  133. LocalFree(const_cast<WCHAR *>(pwszMessageText));
  134. }
  135. }
  136. VOID
  137. csiLogTime(
  138. IN DWORD dwFile,
  139. IN DWORD dwLine,
  140. IN UINT idMsg)
  141. {
  142. HRESULT hr;
  143. WCHAR *pwszDate = NULL;
  144. SYSTEMTIME st;
  145. FILETIME ft;
  146. GetSystemTime(&st);
  147. if (!SystemTimeToFileTime(&st, &ft))
  148. {
  149. hr = myHLastError();
  150. _PrintError(hr, "SystemTimeToFileTime");
  151. }
  152. else
  153. {
  154. hr = myGMTFileTimeToWszLocalTime(&ft, TRUE, &pwszDate);
  155. _PrintIfError(hr, "myGMTFileTimeToWszLocalTime");
  156. }
  157. csiLog(dwFile, dwLine, S_OK, idMsg, pwszDate, NULL, NULL);
  158. //error:
  159. if (NULL != pwszDate)
  160. {
  161. LocalFree(pwszDate);
  162. }
  163. }
  164. VOID
  165. csiLogDWord(
  166. IN DWORD dwFile,
  167. IN DWORD dwLine,
  168. IN UINT idMsg,
  169. IN DWORD dwVal)
  170. {
  171. csiLog(dwFile, dwLine, S_OK, idMsg, NULL, NULL, &dwVal);
  172. }
  173. FNLOGSTRING csiLogString;
  174. VOID
  175. csiLogString(
  176. IN char const *psz)
  177. {
  178. BOOL fCritSecEntered = FALSE;
  179. if (NULL != g_pfLog)
  180. {
  181. if (g_fInitLogCritSecEnabled)
  182. {
  183. EnterCriticalSection(&g_InitLogCriticalSection);
  184. fCritSecEntered = TRUE;
  185. }
  186. fputsStripCRA(psz, g_pfLog);
  187. fflush(g_pfLog);
  188. }
  189. if (fCritSecEntered)
  190. {
  191. LeaveCriticalSection(&g_InitLogCriticalSection);
  192. }
  193. }
  194. FNLOGMESSAGEBOX csiLogMessagBox;
  195. VOID
  196. csiLogMessagBox(
  197. IN HRESULT hrMsg,
  198. IN UINT idMsg,
  199. IN WCHAR const *pwszTitle,
  200. IN WCHAR const *pwszMessage)
  201. {
  202. // Use file number 0 and the passed idMsg as the line number.
  203. csiLog(0, idMsg, hrMsg, IDS_ILOG_MESSAGEBOX, pwszTitle, pwszMessage, NULL);
  204. }
  205. VOID
  206. csiLogClose()
  207. {
  208. BOOL fCritSecEntered = FALSE;
  209. BOOL fDelete;
  210. if (NULL != g_pfLog)
  211. {
  212. CSILOGTIME(IDS_ILOG_END);
  213. }
  214. if (g_fInitLogCritSecEnabled)
  215. {
  216. EnterCriticalSection(&g_InitLogCriticalSection);
  217. fCritSecEntered = TRUE;
  218. }
  219. if (NULL != g_pfLog)
  220. {
  221. fclose(g_pfLog);
  222. g_pfLog = NULL;
  223. }
  224. fDelete = FALSE;
  225. if (fCritSecEntered)
  226. {
  227. if (g_fInitLogCritSecEnabled)
  228. {
  229. g_fInitLogCritSecEnabled = FALSE;
  230. fDelete = TRUE;
  231. }
  232. LeaveCriticalSection(&g_InitLogCriticalSection);
  233. }
  234. if (fDelete)
  235. {
  236. DeleteCriticalSection(&g_InitLogCriticalSection);
  237. }
  238. }
  239. char const szHeader[] = "\n========================================================================\n";
  240. VOID
  241. csiLogOpen(
  242. IN char const *pszFile)
  243. {
  244. HRESULT hr;
  245. UINT cch;
  246. char aszLogFile[MAX_PATH];
  247. BOOL fAppend = FALSE;
  248. BOOL fOk = FALSE;
  249. BOOL fCritSecEntered = FALSE;
  250. __try
  251. {
  252. if (!g_fInitLogCritSecEnabled)
  253. {
  254. InitializeCriticalSection(&g_InitLogCriticalSection);
  255. g_fInitLogCritSecEnabled = TRUE;
  256. }
  257. }
  258. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  259. {
  260. }
  261. if (g_fInitLogCritSecEnabled)
  262. {
  263. EnterCriticalSection(&g_InitLogCriticalSection);
  264. fCritSecEntered = TRUE;
  265. }
  266. if ('+' == *pszFile)
  267. {
  268. pszFile++;
  269. fAppend = TRUE;
  270. }
  271. cch = GetWindowsDirectoryA(aszLogFile, ARRAYSIZE(aszLogFile));
  272. if (0 != cch)
  273. {
  274. if (L'\\' != aszLogFile[cch - 1])
  275. {
  276. strcat(aszLogFile, "\\");
  277. }
  278. strcat(aszLogFile, pszFile);
  279. while (TRUE)
  280. {
  281. g_pfLog = fopen(aszLogFile, fAppend? "at" : "wt");
  282. if (NULL == g_pfLog)
  283. {
  284. _PrintError(E_FAIL, "fopen(Log)");
  285. }
  286. else
  287. {
  288. if (fAppend)
  289. {
  290. if (0 == fseek(g_pfLog, 0L, SEEK_END) &&
  291. CBLOGMAXAPPEND <= ftell(g_pfLog))
  292. {
  293. fclose(g_pfLog);
  294. g_pfLog = NULL;
  295. fAppend = FALSE;
  296. continue;
  297. }
  298. fprintf(g_pfLog, szHeader);
  299. }
  300. fOk = TRUE;
  301. }
  302. break;
  303. }
  304. }
  305. if (fCritSecEntered)
  306. {
  307. LeaveCriticalSection(&g_InitLogCriticalSection);
  308. }
  309. if (fOk)
  310. {
  311. CSILOGTIME(IDS_ILOG_BEGIN);
  312. DBGLOGSTRINGINIT(csiLogString);
  313. CSASSERT(NULL == g_pfnLogMessagBox);
  314. g_pfnLogMessagBox = csiLogMessagBox;
  315. }
  316. //error:
  317. ;
  318. }