Quick.Debug.Utils.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. { ***************************************************************************
  2. Copyright (c) 2016-2020 Kike Pérez
  3. Unit : Quick.Debug.Utils
  4. Description : Debug Utils
  5. Author : Kike Pérez
  6. Version : 1.9
  7. Created : 05/06/2020
  8. Modified : 28/06/2020
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Debug.Utils;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. System.SysUtils,
  26. Quick.Logger.Intf,
  27. Quick.Commons,
  28. {$IFNDEF NEXTGEN}
  29. Quick.Console,
  30. {$ELSE}
  31. {$IFNDEF DELPHILINUX}
  32. FMX.Types,
  33. {$ENDIF}
  34. {$ENDIF}
  35. Quick.Chrono;
  36. type
  37. TDebugConsoleLogger = class(TInterfacedObject,ILogger)
  38. private
  39. fShowTime : Boolean;
  40. fFormatSettings : TFormatSettings;
  41. function FormatMsg(const aMsg : string) : string;
  42. public
  43. constructor Create;
  44. property ShowTime : Boolean read fShowTime write fShowTime;
  45. property FormatSettings : TFormatSettings read fFormatSettings write fFormatSettings;
  46. procedure Info(const aMsg : string); overload;
  47. procedure Info(const aMsg : string; aParams : array of const); overload;
  48. procedure Succ(const aMsg : string); overload;
  49. procedure Succ(const aMsg : string; aParams : array of const); overload;
  50. procedure Done(const aMsg : string); overload;
  51. procedure Done(const aMsg : string; aParams : array of const); overload;
  52. procedure Warn(const aMsg : string); overload;
  53. procedure Warn(const aMsg : string; aParams : array of const); overload;
  54. procedure Error(const aMsg : string); overload;
  55. procedure Error(const aMsg : string; aParams : array of const); overload;
  56. procedure Critical(const aMsg : string); overload;
  57. procedure Critical(const aMsg : string; aParams : array of const); overload;
  58. procedure Trace(const aMsg : string); overload;
  59. procedure Trace(const aMsg : string; aParams : array of const); overload;
  60. procedure Debug(const aMsg : string); overload;
  61. procedure Debug(const aMsg : string; aParams : array of const); overload;
  62. procedure &Except(const aMsg : string; aValues : array of const); overload;
  63. procedure &Except(const aMsg, aException, aStackTrace : string); overload;
  64. procedure &Except(const aMsg : string; aValues: array of const; const aException, aStackTrace: string); overload;
  65. end;
  66. IDebugMethodEnter = interface
  67. ['{3BE4E8C2-CBCF-43BC-B3BE-0A0235C49BF1}']
  68. procedure TimeIt;
  69. end;
  70. TDebugMethodEnter = class(TInterfacedObject,IDebugMethodEnter)
  71. private
  72. fLogger : ILogger;
  73. fCallerMethod : string;
  74. fTimeIt : Boolean;
  75. fChrono : IChronometer;
  76. public
  77. constructor Create(aLogger : ILogger; const aCallerMethod : string);
  78. destructor Destroy; override;
  79. procedure TimeIt;
  80. end;
  81. IDebugMehtodChrono = interface
  82. ['{3DDD5389-D55A-4DEA-81FA-980CF41ACE38}']
  83. procedure BreakPoint(const aMessage : string);
  84. procedure Stop;
  85. end;
  86. TDebugMethodChrono = class(TInterfacedObject,IDebugMehtodChrono)
  87. private
  88. fLogger : ILogger;
  89. fCallerMethod : string;
  90. fMsg : string;
  91. fChrono : IChronometer;
  92. public
  93. constructor Create(aLogger: ILogger; const aCallerMethod, aMsg : string);
  94. destructor Destroy; override;
  95. procedure BreakPoint(const aMsg : string);
  96. procedure Stop;
  97. end;
  98. TDebugger = class
  99. private class var
  100. fLogger : ILogger;
  101. fShowTime : Boolean;
  102. public
  103. class constructor Create;
  104. class destructor Destroy;
  105. class procedure SetLogger(aLogger : ILogger);
  106. class property ShowTime : Boolean read fShowTime write fShowTime;
  107. class function NewChrono(aStarted : Boolean) : IChronometer;
  108. class function TimeIt(aOwner : TObject; const aFunctionName, aDescription : string) : IDebugMehtodChrono;
  109. class function Log : ILogger;
  110. class procedure Trace(aOwner : TObject; const aMsg : string); overload;
  111. class procedure Trace(aOwner : TObject; const aMsg : string; aParams : array of const); overload;
  112. class procedure Trace(const aMsg : string); overload;
  113. class procedure Trace(const aMsg : string; aParams : array of const); overload;
  114. class function Enter(aOwner : TObject; const aFunctionName: string) : IDebugMethodEnter;
  115. end;
  116. {$IFDEF NEXTGEN}
  117. procedure cout(const cMsg : string; params : array of const; cEventType : TLogEventType); overload;
  118. procedure cout(const cMsg : string; cEventType : TLogEventType); overload;
  119. {$ENDIF}
  120. implementation
  121. {$IFDEF NEXTGEN}
  122. procedure cout(const cMsg : string; params : array of const; cEventType : TLogEventType);
  123. begin
  124. FMX.Types.Log.d(Format(cMsg,params));
  125. end;
  126. procedure cout(const cMsg : string; cEventType : TLogEventType); overload;
  127. begin
  128. FMX.Types.Log.d(cMsg);
  129. end;
  130. {$ENDIF}
  131. { TDebugger }
  132. class constructor TDebugger.Create;
  133. begin
  134. fLogger := TDebugConsoleLogger.Create;
  135. fShowTime := True;
  136. end;
  137. class destructor TDebugger.Destroy;
  138. begin
  139. end;
  140. class function TDebugger.TimeIt(aOwner : TObject; const aFunctionName, aDescription: string): IDebugMehtodChrono;
  141. begin
  142. if aOwner <> nil then Result := TDebugMethodChrono.Create(fLogger,Format('%s.%s',[aOwner.ClassName,aFunctionName]),aDescription)
  143. else Result := TDebugMethodChrono.Create(fLogger,Format('%s',[aFunctionName]),aDescription);
  144. end;
  145. class function TDebugger.Enter(aOwner : TObject; const aFunctionName: string) : IDebugMethodEnter;
  146. begin
  147. fLogger.Debug(Format('[ENTER] >> %s.%s',[aOwner.ClassName,aFunctionName]));
  148. Result := TDebugMethodEnter.Create(fLogger,Format('%s.%s',[aOwner.ClassName,aFunctionName]));
  149. end;
  150. class function TDebugger.NewChrono(aStarted : Boolean) : IChronometer;
  151. begin
  152. Result := TChronometer.Create(aStarted);
  153. end;
  154. class function TDebugger.Log: ILogger;
  155. begin
  156. Result := fLogger;
  157. end;
  158. class procedure TDebugger.SetLogger(aLogger: ILogger);
  159. begin
  160. fLogger := aLogger;
  161. fShowTime := False;
  162. end;
  163. class procedure TDebugger.Trace(aOwner: TObject; const aMsg: string);
  164. begin
  165. fLogger.Trace(Format('[TRACE] %s -> %s',[aOwner.ClassName,aMsg]));
  166. end;
  167. class procedure TDebugger.Trace(aOwner: TObject; const aMsg: string; aParams: array of const);
  168. begin
  169. Self.Trace(aOwner,Format(aMsg,aParams));
  170. end;
  171. class procedure TDebugger.Trace(const aMsg: string);
  172. begin
  173. fLogger.Trace(Format('[TRACE] %s',[aMsg]));
  174. end;
  175. class procedure TDebugger.Trace(const aMsg: string; aParams: array of const);
  176. begin
  177. Self.Trace(Format(aMsg,aParams));
  178. end;
  179. { TDebugConsoleLogger }
  180. constructor TDebugConsoleLogger.Create;
  181. begin
  182. fFormatSettings.DateSeparator := '/';
  183. fFormatSettings.TimeSeparator := ':';
  184. fFormatSettings.ShortDateFormat := 'DD-MM-YYY HH:NN:SS.ZZZ';
  185. fFormatSettings.ShortTimeFormat := 'HH:NN:SS';
  186. {$IFNDEF NEXTGEN}
  187. Console.LogVerbose := LOG_ALL;
  188. {$ENDIF}
  189. fShowTime := True;
  190. end;
  191. procedure TDebugConsoleLogger.Critical(const aMsg: string; aParams: array of const);
  192. begin
  193. cout(FormatMsg(aMsg),aParams,TLogEventType.etCritical);
  194. end;
  195. procedure TDebugConsoleLogger.Critical(const aMsg: string);
  196. begin
  197. cout(FormatMsg(aMsg),TLogEventType.etCritical);
  198. end;
  199. procedure TDebugConsoleLogger.Debug(const aMsg: string; aParams: array of const);
  200. begin
  201. cout(FormatMsg(aMsg),aParams,TLogEventType.etDebug);
  202. end;
  203. procedure TDebugConsoleLogger.Debug(const aMsg: string);
  204. begin
  205. cout(FormatMsg(aMsg),TLogEventType.etDebug);
  206. end;
  207. procedure TDebugConsoleLogger.Done(const aMsg: string; aParams: array of const);
  208. begin
  209. cout(FormatMsg(aMsg),aParams,TLogEventType.etDone);
  210. end;
  211. procedure TDebugConsoleLogger.Done(const aMsg: string);
  212. begin
  213. cout(FormatMsg(aMsg),TLogEventType.etDone);
  214. end;
  215. procedure TDebugConsoleLogger.Error(const aMsg: string);
  216. begin
  217. cout(FormatMsg(aMsg),TLogEventType.etError);
  218. end;
  219. procedure TDebugConsoleLogger.Error(const aMsg: string; aParams: array of const);
  220. begin
  221. cout(FormatMsg(aMsg),aParams,TLogEventType.etError);
  222. end;
  223. procedure TDebugConsoleLogger.&Except(const aMsg: string; aValues: array of const);
  224. begin
  225. cout(FormatMsg(aMsg),aValues,TLogEventType.etException);
  226. end;
  227. procedure TDebugConsoleLogger.&Except(const aMsg, aException, aStackTrace: string);
  228. begin
  229. cout(FormatMsg(aMsg),TLogEventType.etException);
  230. end;
  231. procedure TDebugConsoleLogger.&Except(const aMsg: string; aValues: array of const; const aException, aStackTrace: string);
  232. begin
  233. cout(FormatMsg(aMsg),aValues,TLogEventType.etException);
  234. end;
  235. function TDebugConsoleLogger.FormatMsg(const aMsg: string): string;
  236. begin
  237. if fShowTime then Result := DateTimeToStr(Now(),fFormatSettings) + ' ' + aMsg
  238. else Result := aMsg
  239. end;
  240. procedure TDebugConsoleLogger.Info(const aMsg: string; aParams: array of const);
  241. begin
  242. cout(FormatMsg(aMsg),aParams,TLogEventType.etInfo);
  243. end;
  244. procedure TDebugConsoleLogger.Info(const aMsg: string);
  245. begin
  246. cout(FormatMsg(aMsg),TLogEventType.etInfo);
  247. end;
  248. procedure TDebugConsoleLogger.Succ(const aMsg: string; aParams: array of const);
  249. begin
  250. cout(FormatMsg(aMsg),aParams,TLogEventType.etSuccess);
  251. end;
  252. procedure TDebugConsoleLogger.Succ(const aMsg: string);
  253. begin
  254. cout(FormatMsg(aMsg),TLogEventType.etSuccess);
  255. end;
  256. procedure TDebugConsoleLogger.Trace(const aMsg: string; aParams: array of const);
  257. begin
  258. cout(FormatMsg(aMsg),aParams,TLogEventType.etTrace);
  259. end;
  260. procedure TDebugConsoleLogger.Trace(const aMsg: string);
  261. begin
  262. cout(FormatMsg(aMsg),TLogEventType.etTrace);
  263. end;
  264. procedure TDebugConsoleLogger.Warn(const aMsg: string; aParams: array of const);
  265. begin
  266. cout(FormatMsg(aMsg),aParams,TLogEventType.etWarning);
  267. end;
  268. procedure TDebugConsoleLogger.Warn(const aMsg: string);
  269. begin
  270. cout(FormatMsg(aMsg),TLogEventType.etWarning);
  271. end;
  272. { TDebugFunctionEnter }
  273. constructor TDebugMethodEnter.Create(aLogger: ILogger; const aCallerMethod: string);
  274. begin
  275. fLogger := aLogger;
  276. fCallerMethod := aCallerMethod;
  277. end;
  278. destructor TDebugMethodEnter.Destroy;
  279. begin
  280. if fTimeIt then
  281. begin
  282. fChrono.Stop;
  283. fLogger.Debug(Format('[EXIT] >> %s in %s',[fCallerMethod,fChrono.ElapsedTime]));
  284. end
  285. else fLogger.Debug(Format('[EXIT] >> %s',[fCallerMethod]));
  286. inherited;
  287. end;
  288. procedure TDebugMethodEnter.TimeIt;
  289. begin
  290. fTimeIt := True;
  291. fChrono := TChronometer.Create(True);
  292. end;
  293. { TDebugMethodChrono }
  294. constructor TDebugMethodChrono.Create(aLogger: ILogger; const aCallerMethod, aMsg : string);
  295. begin
  296. fLogger := aLogger;
  297. fCallerMethod := aCallerMethod;
  298. fMsg := aMsg;
  299. fChrono := TChronometer.Create(True);
  300. end;
  301. destructor TDebugMethodChrono.Destroy;
  302. begin
  303. if fChrono.IsRunning then
  304. begin
  305. fChrono.Stop;
  306. fLogger.Trace(Format('[CHRONO] %s -> %s = %s',[fCallerMethod,fMsg,fChrono.ElapsedTime]));
  307. end;
  308. inherited;
  309. end;
  310. procedure TDebugMethodChrono.BreakPoint(const aMsg: string);
  311. begin
  312. fChrono.BreakPoint;
  313. fLogger.Trace(Format('[CHRONO] %s -> %s = %s',[fCallerMethod,aMsg,fChrono.ElapsedTime_BreakPoint]));
  314. end;
  315. procedure TDebugMethodChrono.Stop;
  316. begin
  317. fChrono.Stop;
  318. fLogger.Trace(Format('[CHRONO] %s -> %s = %s',[fCallerMethod,fMsg,fChrono.ElapsedTime]));
  319. end;
  320. end.