Recent

Author Topic: function/procedure name  (Read 5128 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
function/procedure name
« on: October 10, 2017, 11:15:35 pm »
Hi guys, I was wondering if there is a way to know the name of the function I'm in. For example if I have this code:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.MyProcedure();
  3. begin
  4.         ShowMessage('Name: ' + [name of procedure]);
  5. end;
  6.  
  7.  

Result of show: Name: MyProcedure
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

zamtmn

  • Hero Member
  • *****
  • Posts: 594
Re: function/procedure name
« Reply #1 on: October 10, 2017, 11:17:41 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MyProcedure();
  2. begin
  3.         ShowMessage('Name: ' +{$I %CURRENTROUTINE%});
  4. end;

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: function/procedure name
« Reply #2 on: October 10, 2017, 11:33:42 pm »
It does not work on mac os x
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

zamtmn

  • Hero Member
  • *****
  • Posts: 594
Re: function/procedure name
« Reply #3 on: October 10, 2017, 11:36:59 pm »
I don't know in which version fpc it appeared, in trunk it work

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: function/procedure name
« Reply #4 on: October 11, 2017, 07:06:17 am »
Fpc 3.0.0 does not have it.

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: function/procedure name
« Reply #5 on: October 11, 2017, 07:35:07 am »
It does not work on mac os x
Should be available in FPC 3.1+
For now I'm using a Self.ClassName, however, it shows only class (TForm in your case), not the procedure. Thou very useful if you have a wide range of classes.
I usually just hardcode names of the procedures in the log output, e.g. dLog(LogLabelError,Self.ClassName,'DLabel.PrepareTextImage','Warning! Label width is not initialized! Trying to w='+IntToStr(Base.w));
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: function/procedure name
« Reply #6 on: October 11, 2017, 08:16:23 am »
It is possible to use GetLineInfo to determine the name of the method/funct/proc, see also this thread.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: function/procedure name
« Reply #7 on: October 11, 2017, 03:53:07 pm »
Until now I have not found the way to do it. It will mean that I will change how to deal with the problem. Thanks so much
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

mas steindorff

  • Hero Member
  • *****
  • Posts: 532
Re: function/procedure name
« Reply #8 on: October 11, 2017, 08:20:09 pm »
It may be useful to check into the exception class.  I remember there being some way to automatically load the function name into a "raise" but it's been too long for me to remember the call.
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: function/procedure name
« Reply #9 on: October 11, 2017, 08:56:44 pm »
Until now I have not found the way to do it. It will mean that I will change how to deal with the problem. Thanks so much
What exactly in Bruce44's example routine is not working for you ? Remember to compile with -gl in order to include lineinfo. Are you perhaps targeting a platform that does not support stack-traces ?

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: function/procedure name
« Reply #10 on: October 11, 2017, 10:14:43 pm »
Until now I have not found the way to do it. It will mean that I will change how to deal with the problem. Thanks so much
What exactly in Bruce44's example routine is not working for you ? Remember to compile with -gl in order to include lineinfo. Are you perhaps targeting a platform that does not support stack-traces ?
Mac OS X
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: function/procedure name
« Reply #11 on: October 11, 2017, 10:20:52 pm »
@xinyman:
Are you perhaps using dwarf debug ? In that case GetLineInfo from unit lnfodwrf' should be able to do the trick.

Note that Bruce44's example routine does not actually use the returned function name in his example. You would have to add that yourself (it was just a simple example shown by Bruce44).

Edit: my results on a windows machine:
« Last Edit: October 11, 2017, 11:10:09 pm by molly »

 

TinyPortal © 2005-2018