Quick.Debug.Utils.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. if aLogger = nil then raise Exception.Create('Debugger logger cannot be nil!');
  167. fLogger := aLogger;
  168. fShowTime := False;
  169. end;
  170. class procedure TDebugger.Trace(aOwner: TObject; const aMsg: string);
  171. begin
  172. if aOwner <> nil then fLogger.Trace(Format('[TRACE] %s -> %s',[aOwner.ClassName,aMsg]))
  173. else fLogger.Trace(Format('[TRACE] -> %s',[aMsg]))
  174. end;
  175. class procedure TDebugger.Trace(aOwner: TObject; const aMsg: string; aParams: array of const);
  176. begin
  177. Self.Trace(aOwner,Format(aMsg,aParams));
  178. end;
  179. class procedure TDebugger.Trace(const aMsg: string);
  180. begin
  181. fLogger.Trace(Format('[TRACE] %s',[aMsg]));
  182. end;
  183. class procedure TDebugger.Trace(const aMsg: string; aParams: array of const);
  184. begin
  185. Self.Trace(Format(aMsg,aParams));
  186. end;
  187. class procedure TDebugger.Trace(const aMsg: string; const aObject: TObject);
  188. begin
  189. Self.Trace(aMsg + ' ' + fSerializer.ObjectToJson(aObject));
  190. end;
  191. { TDebugConsoleLogger }
  192. constructor TDebugConsoleLogger.Create;
  193. begin
  194. fFormatSettings.DateSeparator := '/';
  195. fFormatSettings.TimeSeparator := ':';
  196. fFormatSettings.ShortDateFormat := 'DD-MM-YYY HH:NN:SS.ZZZ';
  197. fFormatSettings.ShortTimeFormat := 'HH:NN:SS';
  198. {$IFNDEF NEXTGEN}
  199. Console.LogVerbose := LOG_ALL;
  200. {$ENDIF}
  201. fShowTime := True;
  202. end;
  203. procedure TDebugConsoleLogger.Critical(const aMsg: string; aParams: array of const);
  204. begin
  205. cout(FormatMsg(aMsg),aParams,TLogEventType.etCritical);
  206. end;
  207. procedure TDebugConsoleLogger.Critical(const aMsg: string);
  208. begin
  209. cout(FormatMsg(aMsg),TLogEventType.etCritical);
  210. end;
  211. procedure TDebugConsoleLogger.Debug(const aMsg: string; aParams: array of const);
  212. begin
  213. cout(FormatMsg(aMsg),aParams,TLogEventType.etDebug);
  214. end;
  215. procedure TDebugConsoleLogger.Debug(const aMsg: string);
  216. begin
  217. cout(FormatMsg(aMsg),TLogEventType.etDebug);
  218. end;
  219. procedure TDebugConsoleLogger.Done(const aMsg: string; aParams: array of const);
  220. begin
  221. cout(FormatMsg(aMsg),aParams,TLogEventType.etDone);
  222. end;
  223. procedure TDebugConsoleLogger.Done(const aMsg: string);
  224. begin
  225. cout(FormatMsg(aMsg),TLogEventType.etDone);
  226. end;
  227. procedure TDebugConsoleLogger.Error(const aMsg: string);
  228. begin
  229. cout(FormatMsg(aMsg),TLogEventType.etError);
  230. end;
  231. procedure TDebugConsoleLogger.Error(const aMsg: string; aParams: array of const);
  232. begin
  233. cout(FormatMsg(aMsg),aParams,TLogEventType.etError);
  234. end;
  235. procedure TDebugConsoleLogger.&Except(const aMsg: string; aValues: array of const);
  236. begin
  237. cout(FormatMsg(aMsg),aValues,TLogEventType.etException);
  238. end;
  239. procedure TDebugConsoleLogger.&Except(const aMsg, aException, aStackTrace: string);
  240. begin
  241. cout(FormatMsg(aMsg),TLogEventType.etException);
  242. end;
  243. procedure TDebugConsoleLogger.&Except(const aMsg: string; aValues: array of const; const aException, aStackTrace: string);
  244. begin
  245. cout(FormatMsg(aMsg),aValues,TLogEventType.etException);
  246. end;
  247. function TDebugConsoleLogger.FormatMsg(const aMsg: string): string;
  248. begin
  249. if fShowTime then Result := DateTimeToStr(Now(),fFormatSettings) + ' ' + aMsg
  250. else Result := aMsg
  251. end;
  252. procedure TDebugConsoleLogger.Info(const aMsg: string; aParams: array of const);
  253. begin
  254. cout(FormatMsg(aMsg),aParams,TLogEventType.etInfo);
  255. end;
  256. procedure TDebugConsoleLogger.Info(const aMsg: string);
  257. begin
  258. cout(FormatMsg(aMsg),TLogEventType.etInfo);
  259. end;
  260. procedure TDebugConsoleLogger.Succ(const aMsg: string; aParams: array of const);
  261. begin
  262. cout(FormatMsg(aMsg),aParams,TLogEventType.etSuccess);
  263. end;
  264. procedure TDebugConsoleLogger.Succ(const aMsg: string);
  265. begin
  266. cout(FormatMsg(aMsg),TLogEventType.etSuccess);
  267. end;
  268. procedure TDebugConsoleLogger.Trace(const aMsg: string; aParams: array of const);
  269. begin
  270. cout(FormatMsg(aMsg),aParams,TLogEventType.etTrace);
  271. end;
  272. procedure TDebugConsoleLogger.Trace(const aMsg: string);
  273. begin
  274. cout(FormatMsg(aMsg),TLogEventType.etTrace);
  275. end;
  276. procedure TDebugConsoleLogger.Warn(const aMsg: string; aParams: array of const);
  277. begin
  278. cout(FormatMsg(aMsg),aParams,TLogEventType.etWarning);
  279. end;
  280. procedure TDebugConsoleLogger.Warn(const aMsg: string);
  281. begin
  282. cout(FormatMsg(aMsg),TLogEventType.etWarning);
  283. end;
  284. { TDebugFunctionEnter }
  285. constructor TDebugMethodEnter.Create(aLogger: ILogger; const aCallerMethod: string);
  286. begin
  287. fLogger := aLogger;
  288. fCallerMethod := aCallerMethod;
  289. end;
  290. destructor TDebugMethodEnter.Destroy;
  291. begin
  292. if fTimeIt then
  293. begin
  294. fChrono.Stop;
  295. fLogger.Debug(Format('[EXIT] >> %s in %s',[fCallerMethod,fChrono.ElapsedTime]));
  296. end
  297. else fLogger.Debug(Format('[EXIT] >> %s',[fCallerMethod]));
  298. inherited;
  299. end;
  300. procedure TDebugMethodEnter.TimeIt;
  301. begin
  302. fTimeIt := True;
  303. fChrono := TChronometer.Create(True);
  304. end;
  305. { TDebugMethodChrono }
  306. constructor TDebugMethodChrono.Create(aLogger: ILogger; const aCallerMethod, aMsg : string);
  307. begin
  308. fLogger := aLogger;
  309. fCallerMethod := aCallerMethod;
  310. fMsg := aMsg;
  311. fChrono := TChronometer.Create(True);
  312. end;
  313. destructor TDebugMethodChrono.Destroy;
  314. begin
  315. if fChrono.IsRunning then
  316. begin
  317. fChrono.Stop;
  318. fLogger.Trace(Format('[CHRONO] %s -> %s = %s',[fCallerMethod,fMsg,fChrono.ElapsedTime]));
  319. end;
  320. inherited;
  321. end;
  322. procedure TDebugMethodChrono.BreakPoint(const aMsg: string);
  323. begin
  324. fChrono.BreakPoint;
  325. fLogger.Trace(Format('[CHRONO] %s -> %s = %s',[fCallerMethod,aMsg,fChrono.ElapsedTime_BreakPoint]));
  326. end;
  327. procedure TDebugMethodChrono.Stop;
  328. begin
  329. fChrono.Stop;
  330. fLogger.Trace(Format('[CHRONO] %s -> %s = %s',[fCallerMethod,fMsg,fChrono.ElapsedTime]));
  331. end;
  332. end.