LogUtils.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. unit LogUtils;
  2. interface
  3. uses
  4. Classes;
  5. type
  6. TLogUtils = class
  7. private
  8. FLogFolder: string;
  9. FLogFile: string;
  10. FExeVersion: string;
  11. public
  12. constructor Create;
  13. destructor Destroy; override;
  14. procedure AppendLogTo(const ALog: string);
  15. end;
  16. TTimeLog = class
  17. private
  18. FEndTime: TDateTime;
  19. FBeginTime: TDateTime;
  20. FName: string;
  21. public
  22. constructor Create(const AName: string);
  23. procedure EndLog;
  24. property Name: string read FName;
  25. property BeginTime: TDateTime read FBeginTime;
  26. property EndTime: TDateTime read FEndTime;
  27. end;
  28. TTimeLogManager = class
  29. private
  30. FLogFolder: string;
  31. FLogFile: string;
  32. FLogList: TList;
  33. function FindTimeLog(const AName: string): TTimeLog;
  34. function AddTimeLog(const AName: string): TTimeLog;
  35. function GetTimeLog(const AName: string): TTimeLog;
  36. procedure AppendLogTo(const ALog: string);
  37. public
  38. constructor Create;
  39. destructor Destroy; override;
  40. procedure ClearTimeLog;
  41. procedure BeginTime(const AName: string);
  42. procedure EndTime(const AName: string);
  43. end;
  44. implementation
  45. uses SysUtils, UtilMethods, Math, ZhAPI;
  46. { TLog }
  47. procedure TLogUtils.AppendLogTo(const ALog: string);
  48. var
  49. f: TextFile;
  50. begin
  51. try
  52. AssignFile(f, FLogFile);
  53. if FileExists(FLogFile) then
  54. Append(f)
  55. else
  56. Rewrite(f);
  57. Writeln(f, Format('[Version: %s] %s %s', [FExeVersion, DateTimeToStr(Now), ALog]));
  58. Flush(f);
  59. finally
  60. CloseFile(f);
  61. end;
  62. end;
  63. constructor TLogUtils.Create;
  64. var
  65. vFormatSetting: TFormatSettings;
  66. begin
  67. vFormatSetting.ShortDateFormat := 'yyyy/MM/dd';
  68. vFormatSetting.DateSeparator := '-';
  69. FLogFolder := GetAppFilePath + 'log';
  70. if not DirectoryExists(FLogFolder) then
  71. CreateDirectoryInDeep(FLogFolder);
  72. FLogFile := FLogFolder + '\' + DateToStr(Date, vFormatSetting) + '.log';
  73. FExeVersion := GetExeFileVersion(ParamStr(0));
  74. end;
  75. destructor TLogUtils.Destroy;
  76. begin
  77. inherited;
  78. end;
  79. { TTimeLog }
  80. constructor TTimeLog.Create(const AName: string);
  81. begin
  82. FName := AName;
  83. FBeginTime := TickCount;
  84. end;
  85. procedure TTimeLog.EndLog;
  86. begin
  87. FEndTime := TickCount;
  88. end;
  89. { TTimeLogManager }
  90. function TTimeLogManager.AddTimeLog(const AName: string): TTimeLog;
  91. begin
  92. Result := TTimeLog.Create(AName);
  93. FLogList.Add(Result);
  94. end;
  95. procedure TTimeLogManager.AppendLogTo(const ALog: string);
  96. var
  97. f: TextFile;
  98. begin
  99. try
  100. AssignFile(f, FLogFile);
  101. if FileExists(FLogFile) then
  102. Append(f)
  103. else
  104. Rewrite(f);
  105. Writeln(f, ALog);
  106. Flush(f);
  107. finally
  108. CloseFile(f);
  109. end;
  110. end;
  111. procedure TTimeLogManager.BeginTime(const AName: string);
  112. var
  113. vLog: TTimeLog;
  114. begin
  115. vLog := GetTimeLog(AName);
  116. end;
  117. procedure TTimeLogManager.ClearTimeLog;
  118. begin
  119. ClearObjects(FLogList);
  120. end;
  121. constructor TTimeLogManager.Create;
  122. var
  123. vFormatSetting: TFormatSettings;
  124. begin
  125. vFormatSetting.ShortDateFormat := 'yyyy/MM/dd';
  126. vFormatSetting.DateSeparator := '-';
  127. FLogFolder := GetAppFilePath + 'log';
  128. if not DirectoryExists(FLogFolder) then
  129. CreateDirectoryInDeep(FLogFolder);
  130. FLogFile := FLogFolder + '\' + DateToStr(Date, vFormatSetting) + '.log';
  131. FLogList := TList.Create;
  132. end;
  133. destructor TTimeLogManager.Destroy;
  134. begin
  135. ClearTimeLog;
  136. FLogList.Free;
  137. inherited;
  138. end;
  139. procedure TTimeLogManager.EndTime(const AName: string);
  140. var
  141. vLog: TTimeLog;
  142. begin
  143. vLog := FindTimeLog(AName);
  144. if Assigned(vLog) then
  145. begin
  146. vLog.EndLog;
  147. AppendLogTo(Format('%s: %fms', [vLog.Name, vLog.EndTime - vLog.BeginTime]));
  148. end
  149. else
  150. AppendLogTo(Format('Time Log %s not Assigned', [AName]));
  151. end;
  152. function TTimeLogManager.FindTimeLog(const AName: string): TTimeLog;
  153. var
  154. i: Integer;
  155. begin
  156. Result := nil;
  157. for i := 0 to FLogList.Count - 1 do
  158. begin
  159. if (TTimeLog(FLogList.Items[i])).Name = AName then
  160. begin
  161. Result := TTimeLog(FLogList.Items[i]);
  162. Break;
  163. end;
  164. end;
  165. end;
  166. function TTimeLogManager.GetTimeLog(const AName: string): TTimeLog;
  167. var
  168. vLog: TTimeLog;
  169. begin
  170. vLog := FindTimeLog(AName);
  171. if not Assigned(vLog) then
  172. vLog := AddTimeLog(AName);
  173. end;
  174. end.