Usage.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //---------------------------------------------------------------------------
  2. #include <CorePCH.h>
  3. #pragma hdrstop
  4. #include <Configuration.h>
  5. #include <Usage.h>
  6. #include <FileInfo.h>
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. //---------------------------------------------------------------------------
  10. const UnicodeString LastInternalExceptionCounter(L"LastInternalException2");
  11. const UnicodeString LastUpdateExceptionCounter(L"LastUpdateException");
  12. //---------------------------------------------------------------------------
  13. __fastcall TUsage::TUsage(TConfiguration * Configuration)
  14. {
  15. FCriticalSection = new TCriticalSection();
  16. FConfiguration = Configuration;
  17. FValues = new TStringList();
  18. FValues->Delimiter = L'&';
  19. FValues->StrictDelimiter = true;
  20. FCollect = true;
  21. Default();
  22. }
  23. //---------------------------------------------------------------------------
  24. __fastcall TUsage::~TUsage()
  25. {
  26. delete FValues;
  27. delete FCriticalSection;
  28. }
  29. //---------------------------------------------------------------------------
  30. void __fastcall TUsage::Default()
  31. {
  32. TGuard Guard(FCriticalSection);
  33. FPeriodCounters.clear();
  34. FLifetimeCounters.clear();
  35. FValues->Clear();
  36. if (Collect) // optimization
  37. {
  38. Set(L"FirstUse", StandardTimestamp());
  39. Set(L"FirstVersion", IntToStr(FConfiguration->CompoundVersion));
  40. UpdateLastReport();
  41. }
  42. }
  43. //---------------------------------------------------------------------------
  44. void __fastcall TUsage::Load(THierarchicalStorage * Storage)
  45. {
  46. TGuard Guard(FCriticalSection);
  47. Default();
  48. if (Storage->OpenSubKey(L"Values", false))
  49. {
  50. TStrings * Names = new TStringList();
  51. try
  52. {
  53. Storage->GetValueNames(Names);
  54. for (int Index = 0; Index < Names->Count; Index++)
  55. {
  56. UnicodeString Name = Names->Strings[Index];
  57. Set(Name, Storage->ReadString(Name, L""));
  58. }
  59. Storage->CloseSubKey();
  60. }
  61. __finally
  62. {
  63. delete Names;
  64. }
  65. }
  66. Load(Storage, L"PeriodCounters", FPeriodCounters);
  67. Load(Storage, L"LifetimeCounters", FLifetimeCounters);
  68. }
  69. //---------------------------------------------------------------------------
  70. void __fastcall TUsage::Load(THierarchicalStorage * Storage,
  71. const UnicodeString& Name, TCounters & Counters)
  72. {
  73. if (Storage->OpenSubKey(Name, false))
  74. {
  75. TStrings * Names = new TStringList();
  76. try
  77. {
  78. Storage->GetValueNames(Names);
  79. for (int Index = 0; Index < Names->Count; Index++)
  80. {
  81. UnicodeString Name = Names->Strings[Index];
  82. Counters.insert(
  83. std::make_pair(Name, Storage->ReadInteger(Name, 0)));
  84. }
  85. Storage->CloseSubKey();
  86. }
  87. __finally
  88. {
  89. delete Names;
  90. }
  91. }
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TUsage::Save(THierarchicalStorage * Storage) const
  95. {
  96. TGuard Guard(FCriticalSection);
  97. if (Storage->OpenSubKey(L"Values", true))
  98. {
  99. Storage->ClearValues();
  100. Storage->WriteValues(FValues, true);
  101. Storage->CloseSubKey();
  102. }
  103. Save(Storage, L"PeriodCounters", FPeriodCounters);
  104. Save(Storage, L"LifetimeCounters", FLifetimeCounters);
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TUsage::Save(THierarchicalStorage * Storage,
  108. const UnicodeString & Name, const TCounters & Counters) const
  109. {
  110. if (Storage->OpenSubKey(Name, true))
  111. {
  112. Storage->ClearValues();
  113. TCounters::const_iterator i = Counters.begin();
  114. while (i != Counters.end())
  115. {
  116. Storage->WriteInteger(i->first, i->second);
  117. i++;
  118. }
  119. Storage->CloseSubKey();
  120. }
  121. }
  122. //---------------------------------------------------------------------------
  123. void __fastcall TUsage::Set(const UnicodeString & Key, const UnicodeString & Value)
  124. {
  125. if (Collect)
  126. {
  127. TGuard Guard(FCriticalSection);
  128. FValues->Values[Key] = Value;
  129. }
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TUsage::Set(const UnicodeString & Key, int Value)
  133. {
  134. Set(Key, IntToStr(Value));
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TUsage::Set(const UnicodeString & Key, bool Value)
  138. {
  139. Set(Key, Value ? 1 : 0);
  140. }
  141. //---------------------------------------------------------------------------
  142. UnicodeString __fastcall TUsage::Get(const UnicodeString & Key)
  143. {
  144. TGuard Guard(FCriticalSection);
  145. UnicodeString Result = FValues->Values[Key];
  146. Result.Unique();
  147. return Result;
  148. }
  149. //---------------------------------------------------------------------------
  150. void __fastcall TUsage::UpdateLastReport()
  151. {
  152. Set(L"LastReport", StandardTimestamp());
  153. }
  154. //---------------------------------------------------------------------------
  155. void __fastcall TUsage::Reset()
  156. {
  157. TGuard Guard(FCriticalSection);
  158. UpdateLastReport();
  159. FPeriodCounters.clear();
  160. ResetLastExceptions();
  161. }
  162. //---------------------------------------------------------------------------
  163. void __fastcall TUsage::UpdateCurrentVersion()
  164. {
  165. TGuard Guard(FCriticalSection);
  166. int CompoundVersion = FConfiguration->CompoundVersion;
  167. DebugAssert(ZeroBuildNumber(CompoundVersion) == CompoundVersion);
  168. // ZeroBuildNumber for compatibility with versions that stored build number into the compound version
  169. int PrevVersion = ZeroBuildNumber(StrToIntDef(Get(L"CurrentVersion"), 0));
  170. if (PrevVersion != CompoundVersion)
  171. {
  172. Set(L"Installed", StandardTimestamp());
  173. }
  174. if (PrevVersion != 0)
  175. {
  176. if (PrevVersion < CompoundVersion)
  177. {
  178. Inc(L"Upgrades");
  179. }
  180. else if (PrevVersion > CompoundVersion)
  181. {
  182. Inc(L"Downgrades");
  183. }
  184. if (PrevVersion != CompoundVersion)
  185. {
  186. ResetLastExceptions();
  187. }
  188. }
  189. Set(L"CurrentVersion", CompoundVersion);
  190. }
  191. //---------------------------------------------------------------------------
  192. void __fastcall TUsage::ResetValue(const UnicodeString & Key)
  193. {
  194. int Index = FValues->IndexOfName(Key);
  195. if (Index >= 0)
  196. {
  197. FValues->Delete(Index);
  198. }
  199. }
  200. //---------------------------------------------------------------------------
  201. void __fastcall TUsage::ResetLastExceptions()
  202. {
  203. TGuard Guard(FCriticalSection);
  204. ResetValue(LastInternalExceptionCounter);
  205. ResetValue(LastUpdateExceptionCounter);
  206. }
  207. //---------------------------------------------------------------------------
  208. int __fastcall TUsage::Inc(const UnicodeString & Key, int Increment)
  209. {
  210. int Result;
  211. if (Collect)
  212. {
  213. TGuard Guard(FCriticalSection);
  214. Inc(Key, FPeriodCounters, Increment);
  215. Result = Inc(Key, FLifetimeCounters, Increment);
  216. }
  217. else
  218. {
  219. Result = -1;
  220. }
  221. return Result;
  222. }
  223. //---------------------------------------------------------------------------
  224. int __fastcall TUsage::Inc(const UnicodeString & Key, TCounters & Counters, int Increment)
  225. {
  226. int Result;
  227. TCounters::iterator i = Counters.find(Key);
  228. if (i != Counters.end())
  229. {
  230. i->second += Increment;
  231. Result = i->second;
  232. }
  233. else
  234. {
  235. Counters.insert(std::make_pair(Key, Increment));
  236. Result = Increment;
  237. }
  238. return Result;
  239. }
  240. //---------------------------------------------------------------------------
  241. void __fastcall TUsage::SetMax(const UnicodeString & Key, int Value)
  242. {
  243. if (Collect)
  244. {
  245. TGuard Guard(FCriticalSection);
  246. SetMax(Key, Value, FPeriodCounters);
  247. SetMax(Key, Value, FLifetimeCounters);
  248. }
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall TUsage::IncAndSetMax(const UnicodeString & IncKey, const UnicodeString & MaxKey, int Value)
  252. {
  253. Inc(IncKey, Value);
  254. SetMax(MaxKey, Value);
  255. }
  256. //---------------------------------------------------------------------------
  257. void __fastcall TUsage::SetMax(const UnicodeString & Key, int Value,
  258. TCounters & Counters)
  259. {
  260. TCounters::iterator i = Counters.find(Key);
  261. if (i != Counters.end())
  262. {
  263. if (Value > i->second)
  264. {
  265. i->second = Value;
  266. }
  267. }
  268. else
  269. {
  270. Counters.insert(std::make_pair(Key, Value));
  271. }
  272. }
  273. //---------------------------------------------------------------------------
  274. void __fastcall TUsage::SetCollect(bool value)
  275. {
  276. TGuard Guard(FCriticalSection);
  277. if (Collect != value)
  278. {
  279. FCollect = value;
  280. if (!FCollect)
  281. {
  282. FPeriodCounters.clear();
  283. FLifetimeCounters.clear();
  284. FValues->Clear();
  285. }
  286. else
  287. {
  288. Default();
  289. }
  290. }
  291. }
  292. //---------------------------------------------------------------------------
  293. UnicodeString __fastcall TUsage::Serialize(const UnicodeString & Delimiter, const UnicodeString & Filter) const
  294. {
  295. TGuard Guard(FCriticalSection);
  296. UnicodeString Result;
  297. UnicodeString FilterUpper = Filter.UpperCase();
  298. for (int Index = 0; Index < FValues->Count; Index++)
  299. {
  300. Serialize(Result, FValues->Names[Index], FValues->ValueFromIndex[Index], Delimiter, FilterUpper);
  301. }
  302. Serialize(Result, L"Period", FPeriodCounters, Delimiter, FilterUpper);
  303. Serialize(Result, L"Lifetime", FLifetimeCounters, Delimiter, FilterUpper);
  304. return Result;
  305. }
  306. //---------------------------------------------------------------------------
  307. void __fastcall TUsage::Serialize(
  308. UnicodeString & List, const UnicodeString & Name, const TCounters & Counters,
  309. const UnicodeString & Delimiter, const UnicodeString & FilterUpper) const
  310. {
  311. TCounters::const_iterator i = Counters.begin();
  312. while (i != Counters.end())
  313. {
  314. Serialize(List, Name + i->first, IntToStr(i->second), Delimiter, FilterUpper);
  315. i++;
  316. }
  317. }
  318. //---------------------------------------------------------------------------
  319. void __fastcall TUsage::Serialize(
  320. UnicodeString & List, const UnicodeString & Name, const UnicodeString & Value,
  321. const UnicodeString & Delimiter, const UnicodeString & FilterUpper) const
  322. {
  323. if (FilterUpper.IsEmpty() ||
  324. (Name.UpperCase().Pos(FilterUpper) > 0) ||
  325. (Value.UpperCase().Pos(FilterUpper) > 0))
  326. {
  327. AddToList(List, FORMAT(L"%s=%s", (Name, Value)), Delimiter);
  328. }
  329. }
  330. //---------------------------------------------------------------------------
  331. int __fastcall TUsage::CalculateCounterSize(__int64 Size)
  332. {
  333. const int SizeCounterFactor = 10240;
  334. return (Size <= 0) ? 0 : (Size < SizeCounterFactor ? 1 : static_cast<int>(Size / SizeCounterFactor));
  335. }