Usage.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. //---------------------------------------------------------------------------
  11. const UnicodeString LastInternalExceptionCounter(L"LastInternalException2");
  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. ResetLastInternalException();
  161. }
  162. //---------------------------------------------------------------------------
  163. void __fastcall TUsage::UpdateCurrentVersion()
  164. {
  165. TGuard Guard(FCriticalSection);
  166. int CompoundVersion = FConfiguration->CompoundVersion;
  167. int PrevVersion = StrToIntDef(Get(L"CurrentVersion"), 0);
  168. if (PrevVersion != CompoundVersion)
  169. {
  170. Set(L"Installed", StandardTimestamp());
  171. }
  172. if (PrevVersion != 0)
  173. {
  174. if (PrevVersion < CompoundVersion)
  175. {
  176. Inc(L"Upgrades");
  177. }
  178. else if (PrevVersion > CompoundVersion)
  179. {
  180. Inc(L"Downgrades");
  181. }
  182. if (PrevVersion != CompoundVersion)
  183. {
  184. ResetLastInternalException();
  185. }
  186. }
  187. Set(L"CurrentVersion", CompoundVersion);
  188. }
  189. //---------------------------------------------------------------------------
  190. void __fastcall TUsage::ResetLastInternalException()
  191. {
  192. TGuard Guard(FCriticalSection);
  193. int Index = FValues->IndexOfName(LastInternalExceptionCounter);
  194. if (Index >= 0)
  195. {
  196. FValues->Delete(Index);
  197. }
  198. }
  199. //---------------------------------------------------------------------------
  200. void __fastcall TUsage::Inc(const UnicodeString & Key, int Increment)
  201. {
  202. if (Collect)
  203. {
  204. TGuard Guard(FCriticalSection);
  205. Inc(Key, FPeriodCounters, Increment);
  206. Inc(Key, FLifetimeCounters, Increment);
  207. }
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TUsage::Inc(const UnicodeString & Key, TCounters & Counters, int Increment)
  211. {
  212. TCounters::iterator i = Counters.find(Key);
  213. if (i != Counters.end())
  214. {
  215. i->second += Increment;
  216. }
  217. else
  218. {
  219. Counters.insert(std::make_pair(Key, Increment));
  220. }
  221. }
  222. //---------------------------------------------------------------------------
  223. void __fastcall TUsage::SetMax(const UnicodeString & Key, int Value)
  224. {
  225. if (Collect)
  226. {
  227. TGuard Guard(FCriticalSection);
  228. SetMax(Key, Value, FPeriodCounters);
  229. SetMax(Key, Value, FLifetimeCounters);
  230. }
  231. }
  232. //---------------------------------------------------------------------------
  233. void __fastcall TUsage::SetMax(const UnicodeString & Key, int Value,
  234. TCounters & Counters)
  235. {
  236. TCounters::iterator i = Counters.find(Key);
  237. if (i != Counters.end())
  238. {
  239. if (Value > i->second)
  240. {
  241. i->second = Value;
  242. }
  243. }
  244. else
  245. {
  246. Counters.insert(std::make_pair(Key, Value));
  247. }
  248. }
  249. //---------------------------------------------------------------------------
  250. void __fastcall TUsage::SetCollect(bool value)
  251. {
  252. TGuard Guard(FCriticalSection);
  253. if (Collect != value)
  254. {
  255. FCollect = value;
  256. if (!FCollect)
  257. {
  258. FPeriodCounters.clear();
  259. FLifetimeCounters.clear();
  260. FValues->Clear();
  261. }
  262. else
  263. {
  264. Default();
  265. }
  266. }
  267. }
  268. //---------------------------------------------------------------------------
  269. UnicodeString __fastcall TUsage::Serialize() const
  270. {
  271. TGuard Guard(FCriticalSection);
  272. UnicodeString Result;
  273. AddToList(Result, FValues->DelimitedText, L"&");
  274. Serialize(Result, L"Period", FPeriodCounters);
  275. Serialize(Result, L"Lifetime", FLifetimeCounters);
  276. return Result;
  277. }
  278. //---------------------------------------------------------------------------
  279. void __fastcall TUsage::Serialize(UnicodeString& List,
  280. const UnicodeString & Name, const TCounters & Counters) const
  281. {
  282. TCounters::const_iterator i = Counters.begin();
  283. while (i != Counters.end())
  284. {
  285. AddToList(List, FORMAT(L"%s%s=%d", (Name, i->first, i->second)), L"&");
  286. i++;
  287. }
  288. }
  289. //---------------------------------------------------------------------------
  290. int __fastcall TUsage::CalculateCounterSize(__int64 Size)
  291. {
  292. const int SizeCounterFactor = 10240;
  293. return (Size <= 0) ? 0 : (Size < SizeCounterFactor ? 1 : Size / SizeCounterFactor);
  294. }