Recent

Author Topic: [solved] how to get class and method name from inside the method  (Read 2980 times)

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1200
hello i want to get information about where in program an error occurs for instance
Code: Pascal  [Select][+][-]
  1. PROCEDURE TWINCONTROL_HELPER.CREATE_THIS_CONTROL(CONST TYPE_TO_CREATE:TGUI_CONTROL_CLASS;
  2.             VAR THIS_CONTROL: TCONTROL; CONST THE_NAME: SHORTSTRING);
  3. BEGIN
  4. IF (THIS_CONTROL <> NIL) OR (TYPE_TO_CREATE = TCONTROL)
  5.    THEN begin
  6.         ShowMessage('error in -> '+'PROCEDURE TWINCONTROL_HELPER.CREATE_THIS_CONTROL');
  7.         EXIT;
  8.         END;
  9.  END;
is there any way to get the "PROCEDURE TWINCONTROL_HELPER.CREATE_THIS_CONTROL" string by some other means so i can sent it to an external error showing procedure such as

PROCEDURE SHOW_ERROR (CONST ERROR_MESSAGE);
BEGIN
showmessage(error_message);
halt;
END;
« Last Edit: April 19, 2024, 03:53:58 pm by Joanna »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

BlueIcaro

  • Hero Member
  • *****
  • Posts: 803
    • Blog personal
Re: how to get class and method name from inside the method
« Reply #1 on: April 19, 2024, 10:16:33 am »
Hello,  you can use {$I %CURRENTROUTINE%}, more info: https://www.freepascal.org/docs-html/prog/progsu41.html#x48-470001.2.41

Here there is a example: https://forum.lazarus.freepascal.org/index.php?topic=49837.0

/BlueIcaro

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1200
Re: how to get class and method name from inside the method
« Reply #2 on: April 19, 2024, 10:41:48 am »
Thanks blueicaro
That works great once I add classname +’.’

I also wonder if there is a way to get a pointer to procedure I’m in and then send the pointer to another error message procedure which would be able to extract procedure name and make error message
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

cdbc

  • Hero Member
  • *****
  • Posts: 1645
    • http://www.cdbc.dk
Re: how to get class and method name from inside the method
« Reply #3 on: April 19, 2024, 10:52:22 am »
Hi
Try playing with:
Code: Pascal  [Select][+][-]
  1. lptr:= MethodAddress(Classname+.+{$I %CURRENTROUTINE%});
  2.  
Note: When you have this pointer, you can also use 'MethodName();' with it...
edit: I dunno if the 'ClassName'-part is necessary   %)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1200
Re: how to get class and method name from inside the method
« Reply #4 on: April 19, 2024, 03:53:05 pm »
Thanks cdbc. It seems like autocomplete doesn’t work well inside a twincontrol helper  but it compiles ok.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: [solved] how to get class and method name from inside the method
« Reply #5 on: April 19, 2024, 11:34:44 pm »
maybe this can give you some ideas of storing method pointers somewhere too!
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   caption :=TForm1.MethodName(TMethod(Button1.OnClick).Code);
  4. end;              
  5.  
  6.  

 with that, you could test the address in the event to see what method it is going to.

 in this case, it goes to itself!  :o
The only true wisdom is knowing you know nothing

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1200
Re: [solved] how to get class and method name from inside the method
« Reply #6 on: April 20, 2024, 12:14:58 pm »
What does the .code part do?
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Thaddy

  • Hero Member
  • *****
  • Posts: 16145
  • Censorship about opinions does not belong here.
Re: [solved] how to get class and method name from inside the method
« Reply #7 on: April 20, 2024, 12:18:15 pm »
That is the pointer/entry of the code to be executed. The .data part gives the data with which that code should be executed.
If I smell bad code it usually is bad code and that includes my own code.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1200
Re: [solved] how to get class and method name from inside the method
« Reply #8 on: April 20, 2024, 05:39:26 pm »
Interesting,
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

 

TinyPortal © 2005-2018