Delphi 10.2.2: Is there an easy way to get the name of the current method into a variable?

Delphi 10.2.2: Is there an easy way to get the name of the current method into a variable?

Why?

I put a lot of debugging in my code. Specifically in event/methods, typically, I follow this template. (SEE BELOW)

It basically involves copy/pasting the method name into a CONST and then passing that CONST into my debug-logging system. However, as you can image, copy/pasting this is pretty stupid.

Last I looked, I found some pretty complicated RTTI functions, but didn't quite get them to work. I just grep'd the code, I've done this around 900x, so I'm hoping to find something that is reusuable and preferably can be just called once for the method. NOTE: I was hoping to find something that would also work for non-method functions.

procedure TFMyForm.DoSomethingImportant(Sender : TObject);
const
METHOD_NAME = 'TFMyForm.DoSomethingImportant';
var
MyVariable : string;
begin
_LoggingDebug.Send(format('%s()::START', [METHOD_NAME]));

// * Do Stuff and Log Stuff
MyVariable := 'sdkjskjds';
_LoggingDebug.Send(format('%s()::MyVariable', [METHOD_NAME, MyVariable]));

_LoggingDebug.Send(format('%s()::END', [METHOD_NAME]));
end;

Comments

  1. Paul TOTH It's about not using magic strings and thus being stable when you rename or refactor things.

    ReplyDelete
  2. Stefan Glienke sorry I don't understand your sentence ... I guess that the compiler (or the Parser, I don't know how the Delphi compiler is designed) do know where it is, so can easly replace specific keywords at compile time.

    ReplyDelete
  3. Paul TOTH I guess I misunderstood your sentence and you were referring to the work/time of the compiler engineer to implement them? In that case I agree with you.

    ReplyDelete

Post a Comment