浏览代码

[debugUtils] trace: serialize objects

Exilon 5 年之前
父节点
当前提交
1b9a1e84d8
共有 1 个文件被更改,包括 15 次插入2 次删除
  1. 15 2
      Quick.Debug.Utils.pas

+ 15 - 2
Quick.Debug.Utils.pas

@@ -7,7 +7,7 @@
   Author      : Kike Pérez
   Version     : 1.9
   Created     : 05/06/2020
-  Modified    : 28/06/2020
+  Modified    : 07/07/2020
 
   This file is part of QuickLib: https://github.com/exilon/QuickLib
 
@@ -36,6 +36,7 @@ interface
 uses
   System.SysUtils,
   Quick.Logger.Intf,
+  Quick.Serializer.Intf,
   Quick.Commons,
   {$IFNDEF NEXTGEN}
   Quick.Console,
@@ -117,6 +118,7 @@ type
   TDebugger = class
   private class var
     fLogger : ILogger;
+    fSerializer : ISerializer;
     fShowTime : Boolean;
   public
     class constructor Create;
@@ -130,6 +132,7 @@ type
     class procedure Trace(aOwner : TObject; const aMsg : string; aParams : array of const); overload;
     class procedure Trace(const aMsg : string); overload;
     class procedure Trace(const aMsg : string; aParams : array of const); overload;
+    class procedure Trace(const aMsg : string; const aObject : TObject); overload;
     class function Enter(aOwner : TObject; const aFunctionName: string) : IDebugMethodEnter;
   end;
 
@@ -140,6 +143,9 @@ type
 
 implementation
 
+uses
+  Quick.Json.Serializer;
+
 
 {$IFDEF NEXTGEN}
 procedure cout(const cMsg : string; params : array of const; cEventType : TLogEventType);
@@ -158,6 +164,7 @@ end;
 
 class constructor TDebugger.Create;
 begin
+  fSerializer := TJsonSerializer.Create(TSerializeLevel.slPublicProperty);
   fLogger := TDebugConsoleLogger.Create;
   fShowTime := True;
 end;
@@ -197,7 +204,8 @@ end;
 
 class procedure TDebugger.Trace(aOwner: TObject; const aMsg: string);
 begin
-  fLogger.Trace(Format('[TRACE] %s -> %s',[aOwner.ClassName,aMsg]));
+  if aOwner <> nil then fLogger.Trace(Format('[TRACE] %s -> %s',[aOwner.ClassName,aMsg]))
+    else fLogger.Trace(Format('[TRACE] -> %s',[aMsg]))
 end;
 
 class procedure TDebugger.Trace(aOwner: TObject; const aMsg: string; aParams: array of const);
@@ -215,6 +223,11 @@ begin
   Self.Trace(Format(aMsg,aParams));
 end;
 
+class procedure TDebugger.Trace(const aMsg: string; const aObject: TObject);
+begin
+  Self.Trace(aMsg + ' ' + fSerializer.ObjectToJson(aObject));
+end;
+
 { TDebugConsoleLogger }
 
 constructor TDebugConsoleLogger.Create;