1

Topic: LogEnumeratedType

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

2

Re: LogEnumeratedType

Hello Greg,

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

type
  TEnum = (enFoo, enBar);

procedure TForm1.Button1Click(Sender: TObject);
begin
  Si.Enabled := True;
  SiMain.LogString('Enum', GetEnumName(TypeInfo(TEnum), Ord(enFoo)));
end;

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:

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;

Then you can just call LogEnum like this:

SiMain.LogEnum('Enum', TypeInfo(TEnum), Ord(enFoo));