it just occurred to me that EnterMethod/LeaveMethod could use a feature (OK, not a feature but rather a known but undocumented behaviour) of the Delphi compiler for automatically doing a LeaveMethod call:
IAutoLeaveCaller = interface
TAutoLeaveCaller = class(TInterfacedObject, IAutoLeaveCaller)
constructor Create(_Session: TSiSession; _Level: TSiLevel; const _MethodName: WideString);
destructor Destroy; override;
constructor TAutoLeaveCaller.Create(_Session: TSiSession; _Level: TSiLevel; const _MethodName: WideString);
FSession := _Session;
FLevel := _Level;
FMethod := _MethodName;
Which is used like this:
TAutoLeaveCaller.Create(FSession, lvVerbose, ‘TSomething.DoWhatever’);
What this does is creating an implicit interface variable which will be kept until exiting the method, in which case it will automatically be freed.
This would solve the problem of having to insert LeaveMethod wherever the mehod exits (eg. calling exit or raising an exception). Of course, that’s even worse than adding try…finally in terms of performance, but very convenient to code.
Of course by wrapping it into a TSiSession-Method it could be made even easier to use.