Skip to content

Commit

Permalink
* [CnDebug] Add a Generic Routine to Log EnumType.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxiao committed Oct 24, 2018
1 parent e344d8f commit 887eaf3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions Examples/DebugSender/UnitOutput.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ object FormSend: TFormSend
KeyPreview = True
OldCreateOrder = False
Position = poScreenCenter
OnClick = FormClick
OnCreate = FormCreate
OnKeyDown = FormKeyDown
PixelsPerInch = 96
Expand Down
13 changes: 13 additions & 0 deletions Examples/DebugSender/UnitOutput.pas
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

interface

{$I CnPack.inc}

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, ActnList, UnitThread, Buttons,
Expand Down Expand Up @@ -164,6 +166,7 @@ TFormSend = class(TForm)
procedure btnEvaluateScreenClick(Sender: TObject);
procedure btnFindComponentClick(Sender: TObject);
procedure btnFindControlClick(Sender: TObject);
procedure FormClick(Sender: TObject);
private
{ Private declarations }
FTimeStamp: Boolean;
Expand Down Expand Up @@ -656,4 +659,14 @@ procedure TFormSend.DebuggerFindControl(Sender: TObject;
(Sender as TCnDebugger).LogMsg('On Find Control: ' + AControl.Name);
end;

procedure TFormSend.FormClick(Sender: TObject);
begin
{$IFDEF SUPPORT_ENHANCED_RTTI}
if rgMethod.ItemIndex = 1 then
CnDebugger.TraceEnumType<TAnchorKind>
else
CnDebugger.LogEnumType<TAnchorKind>;
{$ENDIF}
end;

end.
72 changes: 66 additions & 6 deletions Source/Common/CnDebug.pas
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,8 @@ interface

uses
SysUtils, Classes, Windows, TypInfo, Controls, Graphics, Registry, Messages, Forms
{$IFDEF USE_JCL}
,JclDebug, JclHookExcept
{$ENDIF USE_JCL}
;
{$IFDEF SUPPORT_ENHANCED_RTTI}, Rtti {$ENDIF}
{$IFDEF USE_JCL},JclDebug, JclHookExcept {$ENDIF USE_JCL};

const
CnMaxTagLength = 8; // 不可改变
Expand Down Expand Up @@ -300,7 +298,9 @@ TCnDebugger = class(TObject)
procedure SetUseAppend(const Value: Boolean);
function GetMessageCount: Integer;
function GetPostedMessageCount: Integer;

{$IFDEF SUPPORT_ENHANCED_RTTI}
function GetEnumTypeStr<T>: string;
{$ENDIF}
procedure InternalFindComponent(AComponent: TComponent);
procedure InternalFindControl(AControl: TControl);
protected
Expand Down Expand Up @@ -404,8 +404,10 @@ TCnDebugger = class(TObject)
procedure LogRawString(const Value: string);
procedure LogRawAnsiString(const Value: AnsiString);
procedure LogRawWideString(const Value: WideString);

procedure LogStrings(Strings: TStrings; const AMsg: string = '');
{$IFDEF SUPPORT_ENHANCED_RTTI}
procedure LogEnumType<T>(const AMsg: string = '');
{$ENDIF}
procedure LogException(E: Exception; const AMsg: string = '');
procedure LogMemDump(AMem: Pointer; Size: Integer);
procedure LogVirtualKey(AKey: Word);
Expand Down Expand Up @@ -481,6 +483,9 @@ TCnDebugger = class(TObject)
procedure TraceRawAnsiString(const Value: AnsiString);
procedure TraceRawWideString(const Value: WideString);
procedure TraceStrings(Strings: TStrings; const AMsg: string = '');
{$IFDEF SUPPORT_ENHANCED_RTTI}
procedure TraceEnumType<T>(const AMsg: string = '');
{$ENDIF}
procedure TraceException(E: Exception; const AMsg: string = '');
procedure TraceMemDump(AMem: Pointer; Size: Integer);
procedure TraceVirtualKey(AKey: Word);
Expand Down Expand Up @@ -1619,6 +1624,20 @@ procedure TCnDebugger.LogEnter(const AProcName, ATag: string);
{$ENDIF}
end;

{$IFDEF SUPPORT_ENHANCED_RTTI}

procedure TCnDebugger.LogEnumType<T>(const AMsg: string);
begin
{$IFDEF DEBUG}
if AMsg = '' then
LogMsg('EnumType: ' + GetEnumTypeStr<T>)
else
LogFmt('%s %s', [AMsg, GetEnumTypeStr<T>]);
{$ENDIF}
end;

{$ENDIF}

procedure TCnDebugger.LogException(E: Exception; const AMsg: string);
begin
{$IFDEF DEBUG}
Expand Down Expand Up @@ -2339,6 +2358,20 @@ procedure TCnDebugger.TraceEnter(const AProcName, ATag: string);
{$ENDIF}
end;

{$IFDEF SUPPORT_ENHANCED_RTTI}

procedure TCnDebugger.TraceEnumType<T>(const AMsg: string);
begin
{$IFDEF DEBUG}
if AMsg = '' then
TraceMsg('EnumType: ' + GetEnumTypeStr<T>)
else
TraceFmt('%s %s', [AMsg, GetEnumTypeStr<T>]);
{$ENDIF}
end;

{$ENDIF}

procedure TCnDebugger.TraceException(E: Exception; const AMsg: string);
begin
if not Assigned(E) then
Expand Down Expand Up @@ -3511,6 +3544,33 @@ function TCnDebugger.FormatConstArray(Args: array of const): string;
end;
end;

{$IFDEF SUPPORT_ENHANCED_RTTI}

function TCnDebugger.GetEnumTypeStr<T>: string;
var
Rtx: TRttiContext;
Rt: TRttiType;
Rot: TRttiOrdinalType;
I: Integer;
begin
Result := '';
Rt := Rtx.GetType(TypeInfo(T));
if Rt.IsOrdinal then
begin
Rot := Rt.AsOrdinal;
for I := Rot.MinValue to Rot.MaxValue do
begin
if Result = '' then
Result := GetEnumName(TypeInfo(T), I)
else
Result := Result + ', ' + GetEnumName(TypeInfo(T), I);
end;
Result := '(' + Result + ')';
end;
end;

{$ENDIF}

procedure TCnDebugger.LogClass(const AClass: TClass; const AMsg: string);
begin
{$IFDEF DEBUG}
Expand Down

0 comments on commit 887eaf3

Please sign in to comment.