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.

420 lines
12 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #include "stdafx.h"
  11. #include "Common.h"
  12. #include "AtlTraceModuleManager.h"
  13. namespace ATL
  14. {
  15. void NotifyTool();
  16. #if 0
  17. static bool SetSettings(CAtlTraceSettings *pTraceSettings, UINT nLevel, UINT nStatus)
  18. {
  19. ATLASSERT(pTraceSettings);
  20. if(!pTraceSettings)
  21. return false;
  22. pTraceSettings->m_nLevel = nLevel;
  23. switch(nStatus)
  24. {
  25. case 0:
  26. pTraceSettings->m_eStatus = CAtlTraceSettings::Inherit;
  27. break;
  28. case 1:
  29. pTraceSettings->m_eStatus = CAtlTraceSettings::Enabled;
  30. break;
  31. case 2:
  32. default:
  33. pTraceSettings->m_eStatus = CAtlTraceSettings::Disabled;
  34. break;
  35. }
  36. return true;
  37. }
  38. static bool GetSettings(const CAtlTraceSettings &rTraceSettings, UINT *pnStatus)
  39. {
  40. ATLASSERT(pnStatus);
  41. if(!pnStatus)
  42. return false;
  43. switch(rTraceSettings.m_eStatus)
  44. {
  45. case CAtlTraceSettings::Inherit:
  46. *pnStatus = 0;
  47. break;
  48. case CAtlTraceSettings::Enabled:
  49. *pnStatus = 1;
  50. break;
  51. case CAtlTraceSettings::Disabled:
  52. default:
  53. *pnStatus = 2;
  54. break;
  55. }
  56. return true;
  57. }
  58. BOOL __stdcall AtlTraceLoadSettingsA(const CHAR *pszFileName, BOOL bForceLoad)
  59. {
  60. CAtlAllocatorLock lock(&g_Allocator);
  61. if(!lock.Locked())
  62. return FALSE;
  63. CHAR szFileName[_MAX_PATH];
  64. if(!pszFileName)
  65. {
  66. CHAR szDrive[_MAX_DRIVE];
  67. CHAR szDir[_MAX_DIR];
  68. CHAR szFName[_MAX_FNAME];
  69. CHAR szExt[_MAX_EXT];
  70. ::GetModuleFileNameA(NULL, szFileName, MAX_PATH);
  71. _splitpath(szFileName, szDrive, szDir, szFName, szExt);
  72. strcpy(szExt, ".trc");
  73. _makepath(szFileName, szDrive, szDir, szFName, szExt);
  74. pszFileName = szFileName;
  75. }
  76. if(pszFileName)
  77. {
  78. if(-1 != GetFileAttributesA(pszFileName))
  79. {
  80. // file exists
  81. CHAR szSection[MAX_PATH], szKey[MAX_PATH], szValue[MAX_PATH];
  82. CHAR szName[MAX_PATH];
  83. UINT nModules, nCategories, nStatus, nLevel;
  84. UINT nModule, nCategory;
  85. CAtlTraceProcess *pProcess;
  86. CAtlTraceModule *pModule;
  87. CAtlTraceCategory *pCategory;
  88. CHAR *pszProcess = "Process";
  89. CHAR cEnabled, cFuncAndCategoryNames, cFileNameAndLineInfo;
  90. pProcess = g_Allocator.GetProcess();
  91. ATLASSERT(pProcess);
  92. if(!pProcess)
  93. return FALSE;
  94. if(!pProcess->m_bLoaded || bForceLoad)
  95. {
  96. pProcess->m_bLoaded = true;
  97. ::GetPrivateProfileStringA(pszProcess, "Info", "", szValue, MAX_PATH, pszFileName);
  98. if(5 != sscanf(szValue, "ModuleCount:%u, Level:%u, Enabled:%c, "
  99. "FuncAndCategoryNames:%c, FileNameAndLineNo:%c", &nModules, &pProcess->m_nLevel, &cEnabled,
  100. &cFuncAndCategoryNames, &cFileNameAndLineInfo))
  101. {
  102. return FALSE;
  103. }
  104. pProcess->m_bEnabled = cEnabled != 'f';
  105. pProcess->m_bFuncAndCategoryNames = cFuncAndCategoryNames != 'f';
  106. pProcess->m_bFileNameAndLineNo = cFileNameAndLineInfo != 'f';
  107. // REVIEW: conversion in loop
  108. USES_CONVERSION;
  109. for(UINT i = 0; i < nModules; i++)
  110. {
  111. sprintf(szKey, "Module%d", i);
  112. ::GetPrivateProfileStringA(pszProcess, szKey, "", szSection, MAX_PATH, pszFileName);
  113. ::GetPrivateProfileStringA(szSection, "Name", "", szName, MAX_PATH, pszFileName);
  114. if(!g_Allocator.FindModule(A2W(szName), &nModule))
  115. continue;
  116. pModule = g_Allocator.GetModule(nModule);
  117. ATLASSERT(pModule);
  118. if(!pModule)
  119. return FALSE;
  120. ::GetPrivateProfileStringA(szSection, "Settings", "", szValue, MAX_PATH, pszFileName);
  121. if(3 != sscanf(szValue, "CategoryCount:%u, Level:%u, Status:%u", &nCategories, &nLevel, &nStatus))
  122. return FALSE;
  123. SetSettings(pModule, nLevel, nStatus);
  124. for(UINT j = 0; j < nCategories; j++)
  125. {
  126. sprintf(szKey, "Category%d", j);
  127. ::GetPrivateProfileStringA(szSection, szKey, "", szValue, MAX_PATH, pszFileName);
  128. if(4 != sscanf(szValue, "Category:%u, Level:%u, Status:%u, Name:%s", &nCategory, &nLevel, &nStatus, szName))
  129. return FALSE;
  130. UINT iCategory = pModule->m_nFirstCategory;
  131. while( iCategory != UINT( -1 ) )
  132. {
  133. pCategory = g_Allocator.GetCategoryByIndex(iCategory);
  134. if( lstrcmpA(W2A(pCategory->Name()), szName) == 0 )
  135. {
  136. SetSettings(pCategory, nLevel, nStatus);
  137. }
  138. iCategory = pCategory->m_nNext;
  139. }
  140. }
  141. }
  142. NotifyTool();
  143. }
  144. }
  145. }
  146. return TRUE;
  147. }
  148. BOOL __stdcall AtlTraceSaveSettingsA(const CHAR *pszFileName)
  149. {
  150. CAtlAllocatorLock lock(&g_Allocator);
  151. if(!lock.Locked())
  152. return FALSE;
  153. ATLASSERT(pszFileName);
  154. if(!pszFileName)
  155. return FALSE;
  156. BOOL bRetVal = FALSE;
  157. CHAR szKey[MAX_PATH], szValue[MAX_PATH];
  158. UINT nCategories, nStatus;
  159. CAtlTraceProcess *pProcess;
  160. CAtlTraceModule *pModule;
  161. CAtlTraceCategory *pCategory;
  162. LPCSTR pszProcess = "Process";
  163. pProcess = g_Allocator.GetProcess();
  164. ATLASSERT(pProcess);
  165. if(!pProcess)
  166. return FALSE;
  167. bRetVal = TRUE;
  168. sprintf(szValue, "ModuleCount:%u, Level:%u, Enabled:%c, "
  169. "FuncAndCategoryNames:%c, FileNameAndLineNo:%c", pProcess->ModuleCount(), pProcess->m_nLevel,
  170. pProcess->m_bEnabled ? 't' : 'f', pProcess->m_bFuncAndCategoryNames ? 't' : 'f',
  171. pProcess->m_bFileNameAndLineNo ? 't' : 'f');
  172. ::WritePrivateProfileStringA(pszProcess, "Info", szValue, pszFileName);
  173. // REVIEW: conversion in loop
  174. USES_CONVERSION;
  175. for(UINT i = 0; i < pProcess->ModuleCount(); i++)
  176. {
  177. pModule = g_Allocator.GetModule(i);
  178. ATLASSERT(pModule);
  179. if(!pModule)
  180. return FALSE;
  181. sprintf(szKey, "Module%d", i);
  182. ::WritePrivateProfileStringA(pszProcess, szKey, W2A(pModule->Name()), pszFileName);
  183. GetSettings(*pModule, &nStatus);
  184. nCategories = g_Allocator.GetCategoryCount(i);
  185. ::WritePrivateProfileStringA(W2A(pModule->Name()), "Name", W2A(pModule->Path()), pszFileName);
  186. sprintf(szValue, "CategoryCount:%u, Level:%u, Status:%u", nCategories, pModule->m_nLevel, nStatus);
  187. ::WritePrivateProfileStringA(W2A(pModule->Name()), "Settings", szValue, pszFileName);
  188. if(g_Allocator.IsValidCategoryIndex(pModule->m_nFirstCategory))
  189. {
  190. int j = 0;
  191. UINT nCategory = pModule->m_nFirstCategory;
  192. while( nCategory != UINT( -1 ) )
  193. {
  194. pCategory = g_Allocator.GetCategoryByIndex(nCategory);
  195. GetSettings(*pCategory, &nStatus);
  196. sprintf(szKey, "Category%d", j++);
  197. sprintf(szValue, "Category:%u, Level:%u, Status:%u, Name:%S",
  198. 0, pCategory->m_nLevel, nStatus, pCategory->Name());
  199. ::WritePrivateProfileStringA(W2A(pModule->Name()), szKey, szValue, pszFileName);
  200. nCategory = pCategory->m_nNext;
  201. }
  202. }
  203. }
  204. return bRetVal;
  205. }
  206. BOOL __stdcall AtlTraceLoadSettingsU(const WCHAR *pszFileName, BOOL bForceLoad)
  207. {
  208. CAtlAllocatorLock lock(&g_Allocator);
  209. if(!lock.Locked())
  210. return FALSE;
  211. WCHAR szFileName[MAX_PATH];
  212. if(!pszFileName)
  213. {
  214. WCHAR szDrive[_MAX_DRIVE];
  215. WCHAR szDir[_MAX_DIR];
  216. WCHAR szFName[_MAX_FNAME];
  217. WCHAR szExt[_MAX_EXT];
  218. ::GetModuleFileNameW(NULL, szFileName, MAX_PATH);
  219. _wsplitpath(szFileName, szDrive, szDir, szFName, szExt);
  220. wcscpy(szExt, L".trc");
  221. _wmakepath(szFileName, szDrive, szDir, szFName, szExt);
  222. pszFileName = szFileName;
  223. }
  224. if(pszFileName)
  225. {
  226. if(-1 != GetFileAttributesW(pszFileName))
  227. {
  228. // file exists
  229. WCHAR szSection[MAX_PATH], szKey[MAX_PATH], szValue[MAX_PATH];
  230. WCHAR szName[MAX_PATH];
  231. UINT nModules, nCategories, nStatus, nLevel;
  232. UINT nModule, nCategory;
  233. CAtlTraceProcess *pProcess;
  234. CAtlTraceModule *pModule;
  235. CAtlTraceCategory *pCategory;
  236. LPCWSTR pszProcess = L"Process";
  237. WCHAR cEnabled, cFuncAndCategoryNames, cFileNameAndLineInfo;
  238. pProcess = g_Allocator.GetProcess();
  239. ATLASSERT(pProcess);
  240. if(!pProcess)
  241. return FALSE;
  242. if(!pProcess->m_bLoaded || bForceLoad)
  243. {
  244. pProcess->m_bLoaded = true;
  245. ::GetPrivateProfileStringW(pszProcess, L"Info", L"", szValue, MAX_PATH, pszFileName);
  246. if(5 != swscanf(szValue, L"ModuleCount:%u, Level:%u, Enabled:%c, "
  247. L"FuncAndCategoryNames:%c, FileNameAndLineNo:%c", &nModules, &pProcess->m_nLevel, &cEnabled,
  248. &cFuncAndCategoryNames, &cFileNameAndLineInfo))
  249. {
  250. return FALSE;
  251. }
  252. pProcess->m_bEnabled = cEnabled != L'f';
  253. pProcess->m_bFuncAndCategoryNames = cFuncAndCategoryNames != L'f';
  254. pProcess->m_bFileNameAndLineNo = cFileNameAndLineInfo != L'f';
  255. for(UINT i = 0; i < nModules; i++)
  256. {
  257. swprintf(szKey, L"Module%d", i);
  258. ::GetPrivateProfileStringW(pszProcess, szKey, L"", szSection, MAX_PATH, pszFileName);
  259. ::GetPrivateProfileStringW(szSection, L"Name", L"", szName, MAX_PATH, pszFileName);
  260. if(!g_Allocator.FindModule(szName, &nModule))
  261. continue;
  262. pModule = g_Allocator.GetModule(nModule);
  263. ATLASSERT(pModule);
  264. if(!pModule)
  265. return FALSE;
  266. ::GetPrivateProfileStringW(szSection, L"Settings", L"", szValue, MAX_PATH, pszFileName);
  267. if(3 != swscanf(szValue, L"CategoryCount:%u, Level:%u, Status:%u", &nCategories, &nLevel, &nStatus))
  268. return FALSE;
  269. SetSettings(pModule, nLevel, nStatus);
  270. for(UINT j = 0; j < nCategories; j++)
  271. {
  272. swprintf(szKey, L"Category%d", j);
  273. ::GetPrivateProfileStringW(szSection, szKey, L"", szValue, MAX_PATH, pszFileName);
  274. if(4 != swscanf(szValue, L"Category:%u, Level:%u, Status:%u, Name:%s", &nCategory, &nLevel, &nStatus, szName))
  275. return FALSE;
  276. UINT iCategory = pModule->m_nFirstCategory;
  277. while( iCategory != UINT( -1 ) )
  278. {
  279. pCategory = g_Allocator.GetCategoryByIndex(iCategory);
  280. if( lstrcmpW(pCategory->Name(), szName) == 0 )
  281. {
  282. SetSettings(pCategory, nLevel, nStatus);
  283. }
  284. iCategory = pCategory->m_nNext;
  285. }
  286. }
  287. }
  288. NotifyTool();
  289. }
  290. }
  291. }
  292. return TRUE;
  293. }
  294. BOOL __stdcall AtlTraceSaveSettingsU(const WCHAR *pszFileName)
  295. {
  296. CAtlAllocatorLock lock(&g_Allocator);
  297. if(!lock.Locked())
  298. return FALSE;
  299. ATLASSERT(pszFileName);
  300. if(!pszFileName)
  301. return FALSE;
  302. BOOL bRetVal = FALSE;
  303. WCHAR szKey[MAX_PATH], szValue[MAX_PATH];
  304. UINT nCategories, nStatus;
  305. CAtlTraceProcess *pProcess;
  306. CAtlTraceModule *pModule;
  307. CAtlTraceCategory *pCategory;
  308. LPCWSTR pszProcess = L"Process";
  309. pProcess = g_Allocator.GetProcess();
  310. ATLASSERT(pProcess);
  311. if(!pProcess)
  312. return FALSE;
  313. bRetVal = TRUE;
  314. swprintf(szValue, L"ModuleCount:%u, Level:%u, Enabled:%c, "
  315. L"FuncAndCategoryNames:%c, FileNameAndLineNo:%c", pProcess->ModuleCount(), pProcess->m_nLevel,
  316. pProcess->m_bEnabled ? L't' : L'f', pProcess->m_bFuncAndCategoryNames ? L't' : L'f',
  317. pProcess->m_bFileNameAndLineNo ? L't' : L'f');
  318. ::WritePrivateProfileStringW(pszProcess, L"Info", szValue, pszFileName);
  319. for(UINT i = 0; i < pProcess->ModuleCount(); i++)
  320. {
  321. pModule = g_Allocator.GetModule(i);
  322. ATLASSERT(pModule);
  323. if(!pModule)
  324. return FALSE;
  325. swprintf(szKey, L"Module%d", i);
  326. ::WritePrivateProfileStringW(pszProcess, szKey, pModule->Name(), pszFileName);
  327. GetSettings(*pModule, &nStatus);
  328. nCategories = g_Allocator.GetCategoryCount(i);
  329. ::WritePrivateProfileStringW(pModule->Name(), L"Name", pModule->Path(), pszFileName);
  330. swprintf(szValue, L"CategoryCount:%u, Level:%u, Status:%u", nCategories, pModule->m_nLevel, nStatus);
  331. ::WritePrivateProfileStringW(pModule->Name(), L"Settings", szValue, pszFileName);
  332. if(g_Allocator.IsValidCategoryIndex(pModule->m_nFirstCategory))
  333. {
  334. int j = 0;
  335. UINT nCategory = pModule->m_nFirstCategory;
  336. while( nCategory != UINT( -1 ) )
  337. {
  338. pCategory = g_Allocator.GetCategoryByIndex(nCategory);
  339. GetSettings(*pCategory, &nStatus);
  340. swprintf(szKey, L"Category%d", j++);
  341. swprintf(szValue, L"Category:%u, Level:%u, Status:%u, Name:%s",
  342. 0, pCategory->m_nLevel, nStatus, pCategory->Name());
  343. ::WritePrivateProfileStringW(pModule->Name(), szKey, szValue, pszFileName);
  344. nCategory = pCategory->m_nNext;
  345. }
  346. }
  347. }
  348. return bRetVal;
  349. }
  350. #endif
  351. }; // namespace ATL