Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-05

New preprocessor directive

11 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

Reply by email

Florian Klämpfl
to Michael Van Canneyt: As you probably know, it is possible to include source filename, line number, date etc.…
Michael Van Canneyt wrote 97 lines
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 :)

Reply by email

leledumbo
to Michael Van Canneyt: As you probably know, it is possible to include source filename, line number, date etc.…
Show quoted text 5 lines
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 .

Reply by email

Vojtěch Čihák
to leledumbo

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

Show quoted text 10 lines
Od: leledumbo Komu: 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!

Reply by email

Martin Frb
to Vojtěch Čihák
Vojt?ch ?ih?k wrote 5 lines
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150517/ce6d6533/attachment.html>

Reply by email

Graeme Geldenhuys
to Martin Frb: Simply use $ProcedureName() in your template EnterSomething('$ProcedureName()'); Of…
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/

Reply by email

silvioprog
to Michael Van Canneyt: As you probably know, it is possible to include source filename, line number, date etc.…

On Sun, May 17, 2015 at 5:50 AM, Michael Van Canneyt <

Show quoted text 122 lines
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.freepascal.org/pipermail/lazarus/attachments/20150524/4192d9a0/attachment.html>

Reply by email