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

Login interface type


#1

Is there a way to log interface types:

IItem = interface(IPMInterface)
[’{48D952DE-28A0-4594-9F45-52435B2107A8}’]
function get_Value: string;

property Value: string
  read get_Value;

end;

procedure Button1Click;
var
I: IItem;

begin
I := TItem.Create(‘Testvalue’);
SiMain.Log???
end;


#2

Hello Kees,

Sorry for the delay.

Logging interfaces is currently not directly supported. There’s a general LogObject method for logging objects but there’s currently no LogInterface method. LogObject uses RTTI to extract the required information but I’m not so sure if this is also possible with interfaces. We will look into it and I added this feature to our feature request list.

In the meantime, there are two things you can do. First, you could log objects of concrete classes instead of using interfaces. In your case, this would mean declaring the local variable of type TItem instead of IItem. Then you could use the LogObject method to automatically log published properties (as long as TItem is compiled with RTTI, this can be enabled with the {$M+} compiler directive).

Secondly, you could create custom logging routines for your interfaces, i.e. creating a LogItem routine which takes an IItem variable and then extracts and logs its properties. This can look as follows:

[code]procedure LogItem(const ASession: TSiSession; const ALevel: TSiLevel;
const ATitle: WideString; const AItem: IItem);
var
LContext: TSiValueListViewerContext;
begin
if ASession.IsOn(ALevel) then
begin
LContext := TSiValueListViewerContext.Create;
try
LContext.AppendKeyValue(‘Value’, AItem.Value);

  { Log other properties ... }

  ASession.LogCustomContext(ALevel, ATitle, ltText, LContext);
finally
  LContext.Free;
end;

end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
LItem: IItem;
begin
LItem := TItem.Create;
LogItem(SiMain, lvMessage, ‘Logging an item’, LItem);
end;
[/code]

Both workarounds are not ideal but we will look into it whether interface variables can automatically be logged.