Quick.Debug.Utils.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 : 07/07/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.Serializer.Intf,
  28. Quick.Commons,
  29. {$IFNDEF NEXTGEN}
  30. Quick.Console,
  31. {$ELSE}
  32. {$IFNDEF DELPHILINUX}
  33. FMX.Types,
  34. {$ENDIF}
  35. {$ENDIF}
  36. Quick.Chrono;
  37. type
  38. TDebugConsoleLogger = class(TInterfacedObject,ILogger)
  39. private
  40. fShowTime : Boolean;
  41. fFormatSettings : TFormatSettings;
  42. function FormatMsg(const aMsg : string) : string;
  43. public
  44. constructor Create;
  45. property ShowTime : Boolean read fShowTime write fShowTime;
  46. property FormatSettings : TFormatSettings read fFormatSettings write fFormatSettings;
  47. procedure Info(const aMsg : string); overload;
  48. procedure Info(const aMsg : string; aParams : array of const); overload;
  49. procedure Succ(const aMsg : string); overload;
  50. procedure Succ(const aMsg : string; aParams : array of const); overload;
  51. procedure Done(const aMsg : string); overload;
  52. procedure Done(const aMsg : string; aParams : array of const); overload;
  53. procedure Warn(const aMsg : string); overload;
  54. procedure Warn(const aMsg : string; aParams : array of const); overload;
  55. procedure Error(const aMsg : string); overload;
  56. procedure Error(const aMsg : string; aParams : array of const); overload;
  57. procedure Critical(const aMsg : string); overload;
  58. procedure Critical(const aMsg : string; aParams : array of const); overload;
  59. procedure Trace(const aMsg : string); overload;
  60. procedure Trace(const aMsg : string; aParams : array of const); overload;
  61. procedure Debug(const aMsg : string); overload;
  62. procedure Debug(const aMsg : string; aParams : array of const); overload;
  63. procedure &Except(const aMsg : string; aValues : array of const); overload;
  64. procedure &Except(const aMsg, aException, aStackTrace : string); overload;
  65. procedure &Except(const aMsg : string; aValues: array of const; const aException, aStackTrace: string); overload;
  66. end;
  67. IDebugMethodEnter = interface
  68. ['{3BE4E8C2-CBCF-43BC-B3BE-0A0235C49BF1}']
  69. procedure TimeIt;
  70. end;
  71. TDebugMethodEnter = class(TInterfacedObject,IDebugMethodEnter)
  72. private
  73. fLogger : ILogger;
  74. fCallerMethod : string;
  75. fTimeIt : Boolean;
  76. fChrono : IChronometer;
  77. public
  78. constructor Create(aLogger : ILogger; const aCallerMethod : string);
  79. destructor Destroy; override;
  80. procedure TimeIt;
  81. end;
  82. IDebugMehtodChrono = interface
  83. ['{3DDD5389-D55A-4DEA-81FA-980CF41ACE38}']
  84. procedure BreakPoint(const aMessage : string);
  85. procedure Stop;
  86. end;
  87. TDebugMethodChrono = class(TInterfacedObject,IDebugMehtodChrono)
  88. private
  89. fLogger : ILogger;
  90. fCallerMethod : string;
  91. fMsg : string;
  92. fChrono : IChronometer;
  93. public
  94. constructor Create(aLogger: ILogger; const aCallerMethod, aMsg : string);
  95. destructor Destroy; override;
  96. procedure BreakPoint(const aMsg : string);
  97. procedure Stop;
  98. end;
  99. TDebugger = class
  100. private class var
  101. fLogger : ILogger;
  102. fSerializer : ISerializer;
  103. fShowTime : Boolean;
  104. public
  105. class constructor Create;
  106. class destructor Destroy;
  107. class procedure SetLogger(aLogger : ILogger);
  108. class property ShowTime : Boolean read fShowTime write fShowTime;
  109. class function NewChrono(aStarted : Boolean) : IChronometer;
  110. class function TimeIt(aOwner : TObject; const aFunctionName, aDescription : string) : IDebugMehtodChrono;
  111. class function Log : ILogger;
  112. class procedure Trace(aOwner : TObject; const aMsg : string); overload;
  113. class procedure Trace(aOwner : TObject; const aMsg : string; aParams : array of const); overload;
  114. class procedure Trace(const aMsg : string); overload;
  115. class procedure Trace(const aMsg : string; aParams : array of const); overload;
  116. class procedure Trace(const aMsg : string; const aObject : TObject); overload;
  117. class function Enter(aOwner : TObject; const aFunctionName: string) : IDebugMethodEnter;
  118. end;
  119. {$IFDEF NEXTGEN}
  120. procedure cout(const cMsg : string; params : array of const; cEventType : TLogEventType); overload;
  121. procedure cout(const cMsg : string; cEventType : TLogEventType); overload;
  122. {$ENDIF}
  123. implementation
  124. uses
  125. Quick.Json.Serializer;
  126. {$IFDEF NEXTGEN}
  127. procedure cout(const cMsg : string; params : array of const; cEventType : TLogEventType);
  128. begin
  129. FMX.Types.Log.d(Format(cMsg,params));
  130. end;
  131. procedure cout(const cMsg : string; cEventType : TLogEventType); overload;
  132. begin
  133. FMX.Types.Log.d(cMsg);
  134. end;
  135. {$ENDIF}
  136. { TDebugger }
  137. class constructor TDebugger.Create;
  138. begin
  139. fSerializer := TJsonSerializer.Create(TSerializeLevel.slPublicProperty);
  140. fLogger := TDebugConsoleLogger.Create;
  141. fShowTime := True;
  142. end;
  143. class destructor TDebugger.Destroy;
  144. begin
  145. end;
  146. class function TDebugger.TimeIt(aOwner : TObject; const aFunctionName, aDescription: string): IDebugMehtodChrono;
  147. begin
  148. if aOwner <> nil then Result := TDebugMethodChrono.Create(fLogger,Format('%s.%s',[aOwner.ClassName,aFunctionName]),aDescription)
  149. else Result := TDebugMethodChrono.Create(fLogger,Format('%s',[aFunctionName]),aDescription);
  150. end;
  151. class function TDebugger.Enter(aOwner : TObject; const aFunctionName: string) : IDebugMethodEnter;
  152. begin
  153. fLogger.Debug(Format('[ENTER] >> %s.%s',[aOwner.ClassName,aFunctionName]));
  154. Result := TDebugMethodEnter.Create(fLogger,Format('%s.%s',[aOwner.ClassName,aFunctionName]));
  155. end;
  156. class function TDebugger.NewChrono(aStarted : Boolean) : IChronometer;
  157. begin
  158. Result := TChronometer.Create(aStarted);
  159. end;
  160. class function TDebugger.Log: ILogger;
  161. begin
  162. Result := fLogger;
  163. end;
  164. class procedure TDebugger.SetLogger(aLogger: ILogger);
  165. begin
  166. fLogger := aLogger;
  167. fShowTime := False;
  168. end;
  169. class procedure TDebugger.Trace(aOwner: TObject; const aMsg: string);
  170. begin
  171. if aOwner <> nil then fLogger.Trace(Format('[TRACE] %s -> %s',[aOwner.ClassName,aMsg]))
  172. else fLogger.Trace(Format('[TRACE] -> %s',[aMsg]))
  173. end;
  174. class procedure TDebugger.Trace(aOwner: TObject; const aMsg: string; aParams: array of const);
  175. begin
  176. Self.Trace(aOwner,Format(aMsg,aParams));
  177. end;
  178. class procedure TDebugger.Trace(const aMsg: string);
  179. begin
  180. fLogger.Trace(Format('[TRACE] %s',[aMsg]));
  181. end;
  182. class procedure TDebugger.Trace(const aMsg: string; aParams: array of const);
  183. begin
  184. Self.Trace(Format(aMsg,aParams));
  185. end;
  186. class procedure TDebugger.Trace(const aMsg: string; const aObject: TObject);
  187. begin
  188. Self.Trace(aMsg + ' ' + fSerializer.ObjectToJson(aObject));
  189. end;
  190. { TDebugConsoleLogger }
  191. constructor TDebugConsoleLogger.Create;
  192. begin
  193. fFormatSettings.DateSeparator := '/';
  194. fFormatSettings.TimeSeparator := ':';
  195. fFormatSettings.ShortDateFormat := 'DD-MM-YYY HH:NN:SS.ZZZ';
  196. fFormatSettings.ShortTimeFormat := 'HH:NN:SS';
  197. {$IFNDEF NEXTGEN}
  198. Console.LogVerbose := LOG_ALL;
  199. {$ENDIF}
  200. fShowTime := True;
  201. end;
  202. procedure TDebugConsoleLogger.Critical(const aMsg: string; aParams: array of const);
  203. begin
  204. cout(FormatMsg(aMsg),aParams,TLogEventType.etCritical);
  205. end;
  206. procedure TDebugConsoleLogger.Critical(const aMsg: string);
  207. begin
  208. cout(FormatMsg(aMsg),TLogEventType.etCritical);
  209. end;
  210. procedure TDebugConsoleLogger.Debug(const aMsg: string; aParams: array of const);
  211. begin
  212. cout(FormatMsg(aMsg),aParams,TLogEventType.etDebug);
  213. end;
  214. procedure TDebugConsoleLogger.Debug(const aMsg: string);
  215. begin
  216. cout(FormatMsg(aMsg),TLogEventType.etDebug);
  217. end;
  218. procedure TDebugConsoleLogger.Done(const aMsg: string; aParams: array of const);
  219. begin
  220. cout(FormatMsg(aMsg),aParams,TLogEventType.etDone);
  221. end;
  222. procedure TDebugConsoleLogger.Done(const aMsg: string);
  223. begin
  224. cout(FormatMsg(aMsg),TLogEventType.etDone);
  225. end;
  226. procedure TDebugConsoleLogger.Error(const aMsg: string);
  227. begin
  228. cout(FormatMsg(aMsg),TLogEventType.etError);
  229. end;
  230. procedure TDebugConsoleLogger.Error(const aMsg: string; aParams: array of const);
  231. begin
  232. cout(FormatMsg(aMsg),aParams,TLogEventType.etError);
  233. end;
  234. procedure TDebugConsoleLogger.&Except(const aMsg: string; aValues: array of const);
  235. begin
  236. cout(FormatMsg(aMsg),aValues,TLogEventType.etException);
  237. end;
  238. procedure TDebugConsoleLogger.&Except(const aMsg, aException, aStackTrace: string);
  239. begin
  240. cout(FormatMsg(aMsg),TLogEventType.etException);
  241. end;
  242. procedure TDebugConsoleLogger.&Except(const aMsg: string; aValues: array of const; const aException, aStackTrace: string);
  243. begin
  244. cout(FormatMsg(aMsg),aValues,TLogEventType.etException);
  245. end;
  246. function TDebugConsoleLogger.FormatMsg(const aMsg: string): string;
  247. begin
  248. if fShowTime then Result := DateTimeToStr(Now(),fFormatSettings) + ' ' + aMsg
  249. else Result := aMsg
  250. end;
  251. procedure TDebugConsoleLogger.Info(const aMsg: string; aParams: array of const);
  252. begin
  253. cout(FormatMsg(aMsg),aParams,TLogEventType.etInfo);
  254. end;
  255. procedure TDebugConsoleLogger.Info(const aMsg: string);
  256. begin
  257. cout(FormatMsg(aMsg),TLogEventType.etInfo);
  258. end;
  259. procedure TDebugConsoleLogger.Succ(const aMsg: string; aParams: array of const);
  260. begin
  261. cout(FormatMsg(aMsg),aParams,TLogEventType.etSuccess);
  262. end;
  263. procedure TDebugConsoleLogger.Succ(const aMsg: string);
  264. begin
  265. cout(FormatMsg(aMsg),TLogEventType.etSuccess);
  266. end;
  267. procedure TDebugConsoleLogger.Trace(const aMsg: string; aParams: array of const);
  268. begin
  269. cout(FormatMsg(aMsg),aParams,TLogEventType.etTrace);
  270. end;
  271. procedure TDebugConsoleLogger.Trace(const aMsg: string);
  272. begin
  273. cout(FormatMsg(aMsg),TLogEventType.etTrace);
  274. end;
  275. procedure TDebugConsoleLogger.Warn(const aMsg: string; aParams: array of const);
  276. begin
  277. cout(FormatMsg(aMsg),aParams,TLogEventType.etWarning);
  278. end;
  279. procedure TDebugConsoleLogger.Warn(const aMsg: string);
  280. begin
  281. cout(FormatMsg(aMsg),TLogEventType.etWarning);
  282. end;
  283. { TDebugFunctionEnter }
  284. constructor TDebugMethodEnter.Create(aLogger: ILogger; const aCallerMethod: string);
  285. begin
  286. fLogger := aLogger;
  287. fCallerMethod := aCallerMethod;
  288. end;
  289. destructor TDebugMethodEnter.Destroy;
  290. begin
  291. if fTimeIt then
  292. begin
  293. fChrono.Stop;
  294. fLogger.Debug(Format('[EXIT] >> %s in %s',[fCallerMethod,fChrono.ElapsedTime]));
  295. end
  296. else fLogger.Debug(Format('[EXIT] >> %s',[fCallerMethod]));
  297. inherited;
  298. end;
  299. procedure TDebugMethodEnter.TimeIt;
  300. begin
  301. fTimeIt := True;
  302. fChrono := TChronometer.Create(True);
  303. end;
  304. { TDebugMethodChrono }
  305. constructor TDebugMethodChrono.Create(aLogger: ILogger; const aCallerMethod, aMsg : string);
  306. begin
  307. fLogger := aLogger;
  308. fCallerMethod := aCallerMethod;
  309. fMsg := aMsg;
  310. fChrono := TChronometer.Create(True);
  311. end;
  312. destructor TDebugMethodChrono.Destroy;
  313. begin
  314. if fChrono.IsRunning then
  315. begin
  316. fChrono.Stop;
  317. fLogger.Trace(Format('[CHRONO] %s -> %s = %s',[fCallerMethod,fMsg,fChrono.ElapsedTime]));
  318. end;
  319. inherited;
  320. end;
  321. procedure TDebugMethodChrono.BreakPoint(const aMsg: string);
  322. begin
  323. fChrono.BreakPoint;
  324. fLogger.Trace(Format('[CHRONO] %s -> %s = %s',[fCallerMethod,aMsg,fChrono.ElapsedTime_BreakPoint]));
  325. end;
  326. procedure TDebugMethodChrono.Stop;
  327. begin
  328. fChrono.Stop;
  329. fLogger.Trace(Format('[CHRONO] %s -> %s = %s',[fCallerMethod,fMsg,fChrono.ElapsedTime]));
  330. end;
  331. end.