Recent

Author Topic: Name of Current Routine  (Read 2228 times)

Kevan S. Hashemi

  • New Member
  • *
  • Posts: 25
Name of Current Routine
« on: May 16, 2020, 06:37:31 am »
Is there a function in FPC that returns the name of the current routine? For example:

Code: Pascal  [Select][+][-]
  1. function example:integer;
  2. begin
  3.   written('This is routine ',CurrentRoutineName);
  4. end;

Which, when executed produces:

Code: Bash  [Select][+][-]
  1. This is routine example

I'm translating from GNU Pascal (GPC) to FPC, and GPC has CurrentRoutineName, which I want to replace, if possible.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: Name of Current Routine
« Reply #1 on: May 16, 2020, 07:19:06 am »
Code: Pascal  [Select][+][-]
  1. program test;
  2. {$MODE OBJFPC}
  3.  
  4. procedure myCheck;
  5. begin
  6.  WriteLn('Name is: ', {$I %CURRENTROUTINE%});
  7. end;
  8.  
  9. begin
  10.  WriteLn('Hello');
  11.  myCheck;
  12.  WriteLn('Bye');
  13. end.
  14.  

Works in trunk :) but not 3.0.4 :(

MarkMLl

  • Hero Member
  • *****
  • Posts: 8306
Re: Name of Current Routine
« Reply #2 on: May 16, 2020, 12:16:16 pm »
Thanks for that Trev. Apparently described at https://wiki.freepascal.org/$include but with limited indication of what versions of the compiler support the various directives.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jamie

  • Hero Member
  • *****
  • Posts: 6811
Re: Name of Current Routine
« Reply #3 on: May 16, 2020, 12:20:37 pm »
If this is for debugging purposes why not use the Assert ?
The only true wisdom is knowing you know nothing

julkas

  • Guest
Re: Name of Current Routine
« Reply #4 on: May 16, 2020, 12:41:02 pm »
See -
1. https://forum.lazarus.freepascal.org/index.php?topic=19588.0
2. https://stackoverflow.com/questions/2817699/how-to-get-the-name-of-the-current-procedure-function-in-delphi-as-a-string
3. https://blog.matthewskelton.net/2012/01/29/assert-based-error-reporting-in-delphi/

My choice for FPC 3.0.4/Delphi -
Code: Pascal  [Select][+][-]
  1. function example: integer;
  2. {$IFDEF TRACE}
  3.   const
  4.     currMethodName = 'example';
  5. {$ENDIF}       
  6. begin
  7.   {$IFDEF TRACE}
  8.   WriteLn('This is routine - ', currMethodName);
  9.   {$ENDIF}
  10. end;
  11.  
You can automate typing with Code Tools (macro $ProcedureName).
« Last Edit: May 16, 2020, 01:37:33 pm by julkas »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8306
Re: Name of Current Routine
« Reply #5 on: May 16, 2020, 01:04:39 pm »
If this is for debugging purposes why not use the Assert ?

Sometimes you want stuff to be quietly logged rather than blasting through an exception sequence.

However you make a good point that sometimes it would be related to whether assertions were enabled, in which case something like

Code: [Select]
{$ifopt C+ Predicated on assertions generating code }
  WriteLn('Name is: ', {$I %CURRENTROUTINE%});
{$endif }

would be in order.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Kevan S. Hashemi

  • New Member
  • *
  • Posts: 25
Re: Name of Current Routine
« Reply #6 on: May 16, 2020, 03:52:02 pm »
Thank you all. It looks like {$I %CURRENTROUTINE%} would do the trick, and will be available in next version of FPC. In the meantime, I believe a regular expression will allow me to replace the phrase CurrentRoutineName with the actual current routine name, so I am content.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Name of Current Routine
« Reply #7 on: May 16, 2020, 05:00:37 pm »
One alternative is to add a "code template" using the macro $ProcedureName(); with that in place you could type, say, "cpname", press Ctrl+J and have the proc name inserted wherever you want..

See the wiki page: IDE Window: Code Templates
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018