Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

LogEnumeratedType


#1

I did not see anything in the help resembling logEnumeratedType. Am I missing something or should this be considered a feature request?


#2

Hello Greg,

There’s currently no special method for logging an enum. However, logging enums is still possible, for example with the LogString method:

[code]type
TEnum = (enFoo, enBar);

procedure TForm1.Button1Click(Sender: TObject);
begin
Si.Enabled := True;
SiMain.LogString(‘Enum’, GetEnumName(TypeInfo(TEnum), Ord(enFoo)));
end;
[/code]

In order to compile this, you need to add the TypInfo unit to your uses clause. Another way to log enums is to create a session helper class with a LogEnum method (I think you need at least Delphi 2006 for helper classes). This can look as follows:

[code]TSessionHelper = class helper for TSiSession
public
procedure LogEnum(const AName: WideString; const ATypeInfo: PTypeInfo;
const AValue: Integer); overload;
procedure LogEnum(const ALevel: TSiLevel; const AName: WideString;
const ATypeInfo: PTypeInfo; const AValue: Integer); overload;
end;

{ TSessionHelper }

procedure TSessionHelper.LogEnum(const AName: WideString;
const ATypeInfo: PTypeInfo; const AValue: Integer);
begin
LogEnum(Parent.DefaultLevel, AName, ATypeInfo, AValue);
end;

procedure TSessionHelper.LogEnum(const ALevel: TSiLevel;
const AName: WideString; const ATypeInfo: PTypeInfo;
const AValue: Integer);
var
LEnum: WideString;
begin
if IsOn(ALevel) then
begin
LEnum := GetEnumName(ATypeInfo, AValue);
LogString(AName, LEnum);
end;
end;
[/code]

Then you can just call LogEnum like this: