Usage.cpp 10 KB

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