[Lazarus] New preprocessor directive

silvioprog silvioprog at gmail.com
Mon May 25 03:53:54 CEST 2015


On Sun, May 17, 2015 at 5:50 AM, Michael Van Canneyt <michael at freepascal.org
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20150524/4192d9a0/attachment-0003.html>


More information about the Lazarus mailing list