[Lazarus] New preprocessor directive

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

[Lazarus] New preprocessor directive

Michael Van Canneyt

Hello,

As you probably know, it is possible to include source filename, line number, date etc. in your source
with the following directives:
{$I %FILENAME%}
{$I %LINE%}
{$i %DATE%}

At my request, Florian added

{$I %CURRENTROUTINE%}

to the list of possibilities, which means you can do nifty things as:

program testcr;

{$mode objfpc}

Type
   TMyClass = Class(TObject)
   Public
     Procedure MyMethod;
   end;

Var
   Indent : Integer;

Procedure MethodEnter(Const AMethod : String);
begin
   Writeln(StringOfChar(' ',Indent),'Entering ',AMethod);
   Inc(Indent,2);
end;

Procedure MethodExit(Const AMethod : String);
begin
   Dec(Indent,2);
   if Indent<0 then Indent:=0;
   Writeln(StringOfChar(' ',Indent),'Exiting ',AMethod);
end;

Procedure Debug(Const AMsg : String);

begin
  Writeln(StringOfChar(' ',Indent),AMsg);
end;

Procedure DoSomething;

   Procedure DoNested;

   begin
     MethodEnter({$I %CURRENTROUTINE%});
     Debug('Nested handling in '+{$I %CURRENTROUTINE%});
     MethodExit({$I %CURRENTROUTINE%});
   end;

begin
   MethodEnter({$I %CURRENTROUTINE%});
   Debug('Doing something in '+{$I %CURRENTROUTINE%}+' at line '+{$I %LINE%});
   DoNested;
   MethodExit({$I %CURRENTROUTINE%});
end;

Procedure TMyClass.MyMethod;

   Procedure DoNested;

   begin
     MethodEnter({$I %CURRENTROUTINE%});
     Debug('Nested handling in '+{$I %CURRENTROUTINE%});
     MethodExit({$I %CURRENTROUTINE%});
   end;

begin
   MethodEnter({$I %CURRENTROUTINE%});
   Debug('Doing some things in '+{$I %CURRENTROUTINE%}+' at line '+{$I %LINE%});
   DoNested;
   MethodExit({$I %CURRENTROUTINE%});
end;


Var
   T : TMyClass;

begin
   MethodEnter({$I %CURRENTROUTINE%});
   DoSomething;
   T:=TMyClass.Create;
   try
     T.MyMethod;
   finally
     T.Free;
   end;
   MethodExit({$I %CURRENTROUTINE%});
end.

Which produces something like

Entering $main
   Entering DoSomething
     Doing something in DoSomething at line 44
     Entering DoNested
       Nested handling in DoNested
     Exiting DoNested
   Exiting DoSomething
   Entering MyMethod
     Doing some things in MyMethod at line 61
     Entering DoNested
       Nested handling in DoNested
     Exiting DoNested
   Exiting MyMethod
Exiting $main

Kudos to Florian.

Michael.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

Graeme Geldenhuys-3
On 2015-05-17 09:50, Michael Van Canneyt wrote:
> At my request, Florian added
>
> {$I %CURRENTROUTINE%}

Oh wow, that will be super handy! (For the fpprofiler too.) Many thanks
for that one.

Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

Florian Klämpfl
In reply to this post by Michael Van Canneyt
Am 17.05.2015 um 10:50 schrieb Michael Van Canneyt:

>
> Hello,
>
> As you probably know, it is possible to include source filename, line number, date etc. in your source
> with the following directives:
> {$I %FILENAME%}
> {$I %LINE%}
> {$i %DATE%}
>
> At my request, Florian added
>
> {$I %CURRENTROUTINE%}
>
> to the list of possibilities, which means you can do nifty things as:
>
> program testcr;
>
> {$mode objfpc}
>
> Type
>   TMyClass = Class(TObject)
>   Public
>     Procedure MyMethod;
>   end;
>
> Var
>   Indent : Integer;
>
> Procedure MethodEnter(Const AMethod : String);
> begin
>   Writeln(StringOfChar(' ',Indent),'Entering ',AMethod);
>   Inc(Indent,2);
> end;
>
> Procedure MethodExit(Const AMethod : String);
> begin
>   Dec(Indent,2);
>   if Indent<0 then Indent:=0;
>   Writeln(StringOfChar(' ',Indent),'Exiting ',AMethod);
> end;
>
> Procedure Debug(Const AMsg : String);
>
> begin
>  Writeln(StringOfChar(' ',Indent),AMsg);
> end;
>
> Procedure DoSomething;
>
>   Procedure DoNested;
>
>   begin
>     MethodEnter({$I %CURRENTROUTINE%});
>     Debug('Nested handling in '+{$I %CURRENTROUTINE%});
>     MethodExit({$I %CURRENTROUTINE%});
>   end;
>
> begin
>   MethodEnter({$I %CURRENTROUTINE%});
>   Debug('Doing something in '+{$I %CURRENTROUTINE%}+' at line '+{$I %LINE%});
>   DoNested;
>   MethodExit({$I %CURRENTROUTINE%});
> end;
>
> Procedure TMyClass.MyMethod;
>
>   Procedure DoNested;
>
>   begin
>     MethodEnter({$I %CURRENTROUTINE%});
>     Debug('Nested handling in '+{$I %CURRENTROUTINE%});
>     MethodExit({$I %CURRENTROUTINE%});
>   end;
>
> begin
>   MethodEnter({$I %CURRENTROUTINE%});
>   Debug('Doing some things in '+{$I %CURRENTROUTINE%}+' at line '+{$I %LINE%});
>   DoNested;
>   MethodExit({$I %CURRENTROUTINE%});
> end;
>
>
> Var
>   T : TMyClass;
>
> begin
>   MethodEnter({$I %CURRENTROUTINE%});
>   DoSomething;
>   T:=TMyClass.Create;
>   try
>     T.MyMethod;
>   finally
>     T.Free;
>   end;
>   MethodExit({$I %CURRENTROUTINE%});
> end.
>

At least it looks as ugly as possible so nobody will use this too much :)


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

Florian Klämpfl
In reply to this post by Graeme Geldenhuys-3
Am 17.05.2015 um 10:54 schrieb Graeme Geldenhuys:
> On 2015-05-17 09:50, Michael Van Canneyt wrote:
>> At my request, Florian added
>>
>> {$I %CURRENTROUTINE%}
>
> Oh wow, that will be super handy! (For the fpprofiler too.)

A profiler should use debug info imo.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

Michael Van Canneyt
In reply to this post by Florian Klämpfl


On Sun, 17 May 2015, Florian Klämpfl wrote:

> Am 17.05.2015 um 10:50 schrieb Michael Van Canneyt:
>>
>> Hello,
>>
>>
>
> At least it looks as ugly as possible so nobody will use this too much :)

Agreed that it is ugly, but indispensibe when programming webservers.

Michael.
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

leledumbo
Administrator
In reply to this post by Michael Van Canneyt
> At my request, Florian added
>
> {$I %CURRENTROUTINE%}
>
> to the list of possibilities, which means you can do nifty things as:

I usually use hardcoded strings for that, so this makes my coding life easier :)
Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

vojtech.cihak

Add I created code template for it: "icr" and it writes {$I %CURRENTROUTINE%} itself :-)

 

V.

______________________________________________________________
> Od: leledumbo <[hidden email]>
> Komu: <[hidden email]>
> Datum: 17.05.2015 13:54
> Předmět: Re: [Lazarus] New preprocessor directive
>

> At my request, Florian added
>
> {$I %CURRENTROUTINE%}
>
> to the list of possibilities, which means you can do nifty things as:

I usually use hardcoded strings for that, so this makes my coding life
easier :)
Thanks!



--
View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-New-preprocessor-directive-tp4042241p4042246.html
Sent from the Free Pascal - Lazarus mailing list archive at Nabble.com.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

Martin Frb
On 17/05/2015 13:22, Vojtěch Čihák wrote:

Add I created code template for it: "icr" and it writes {$I %CURRENTROUTINE%} itself :-)

 



Simply use $ProcedureName() in your template

  EnterSomething('$ProcedureName()');

Of course it does not update if you rename the procedure, but otherwise it is fine.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

Graeme Geldenhuys-3
In reply to this post by Florian Klämpfl
On 2015-05-17 10:22, Florian Klämpfl wrote:
>
> A profiler should use debug info imo.

True, but the fpprofiler is a quick and easy one (though basic), if no
others are available.


Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

Graeme Geldenhuys-3
In reply to this post by Martin Frb
On 2015-05-17 14:23, Martin Frb wrote:
> Simply use $ProcedureName() in your template
>
>    EnterSomething('$ProcedureName()');


That is what I've been using up to now, but is problematic if you switch
between IDE's or programmer editors to do coding (I do this often). At
least the new directive will be universally supported.

Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] New preprocessor directive

silvioprog
In reply to this post by Michael Van Canneyt
On Sun, May 17, 2015 at 5:50 AM, Michael Van Canneyt <[hidden email]> wrote:

Hello,

As you probably know, it is possible to include source filename, line number, date etc. in your source
with the following directives:
{$I %FILENAME%}
{$I %LINE%}
{$i %DATE%}

At my request, Florian added

{$I %CURRENTROUTINE%}

to the list of possibilities, which means you can do nifty things as:

program testcr;

{$mode objfpc}

Type
  TMyClass = Class(TObject)
  Public
    Procedure MyMethod;
  end;

Var
  Indent : Integer;

Procedure MethodEnter(Const AMethod : String);
begin
  Writeln(StringOfChar(' ',Indent),'Entering ',AMethod);
  Inc(Indent,2);
end;

Procedure MethodExit(Const AMethod : String);
begin
  Dec(Indent,2);
  if Indent<0 then Indent:=0;
  Writeln(StringOfChar(' ',Indent),'Exiting ',AMethod);
end;

Procedure Debug(Const AMsg : String);

begin
 Writeln(StringOfChar(' ',Indent),AMsg);
end;

Procedure DoSomething;

  Procedure DoNested;

  begin
    MethodEnter({$I %CURRENTROUTINE%});
    Debug('Nested handling in '+{$I %CURRENTROUTINE%});
    MethodExit({$I %CURRENTROUTINE%});
  end;

begin
  MethodEnter({$I %CURRENTROUTINE%});
  Debug('Doing something in '+{$I %CURRENTROUTINE%}+' at line '+{$I %LINE%});
  DoNested;
  MethodExit({$I %CURRENTROUTINE%});
end;

Procedure TMyClass.MyMethod;

  Procedure DoNested;

  begin
    MethodEnter({$I %CURRENTROUTINE%});
    Debug('Nested handling in '+{$I %CURRENTROUTINE%});
    MethodExit({$I %CURRENTROUTINE%});
  end;

begin
  MethodEnter({$I %CURRENTROUTINE%});
  Debug('Doing some things in '+{$I %CURRENTROUTINE%}+' at line '+{$I %LINE%});
  DoNested;
  MethodExit({$I %CURRENTROUTINE%});
end;


Var
  T : TMyClass;

begin
  MethodEnter({$I %CURRENTROUTINE%});
  DoSomething;
  T:=TMyClass.Create;
  try
    T.MyMethod;
  finally
    T.Free;
  end;
  MethodExit({$I %CURRENTROUTINE%});
end.

Which produces something like

Entering $main
  Entering DoSomething
    Doing something in DoSomething at line 44
    Entering DoNested
      Nested handling in DoNested
    Exiting DoNested
  Exiting DoSomething
  Entering MyMethod
    Doing some things in MyMethod at line 61
    Entering DoNested
      Nested handling in DoNested
    Exiting DoNested
  Exiting MyMethod
Exiting $main

Kudos to Florian.

Michael.

Thanks for this nice feature! =)

I'll use it in a new logger class that I'm implementing.

(y)

--
Silvio Clécio
My public projects - github.com/silvioprog

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus