Recent

Author Topic: how to define {$I %FILE%} {$I %LINE%}... as a default param in a function call  (Read 3517 times)

Bruce44

  • New Member
  • *
  • Posts: 15
Hello
For my system message logging tool, I'm trying to write something equivalent to what I'm used to in C

the define :
#define SYSLOG(msg) sys_log (msg, __FILE__, __LINE__, ...)

and the call :
SYSLOG ('my message')

thus __FILE__, __LINE__ are automatically handled with good values.


In Pascal of course, for the moment, I can write something like :
sys_log (msg, {$I %FILE%}, {$I %LINE%}, ...)

but for each log call, i had to add always the same directives {$I %FILE%}, {$I %LINE%}, ...)

How can I simplify this ?

molly

  • Hero Member
  • *****
  • Posts: 2330
afiak that is not possible to realize with $i directive.

Use getlineinfo instead.

To make it easier for yourself, inline your syslog function so that you don't have to trace back. You can then use Get_pc_addr to use as codepointer for getlineinfo().

e.g. you put all functionality inside your sys_log function and get rid of the necessity to feed it the line and file parameters.

functionprocedure sys_log(msg: string); inline;
begin
  use getlineinfo to retrieve what you want
  write your message to whatever you want to write to using the obtained information from getlineinfo()
end;

and call it with:
begin
  sys_log('hello this is a message');
  do some stuff
  sys_log('a message after i've done some stuff');
end;

That should read procedure, not function  :'(
« Last Edit: July 30, 2016, 11:25:05 am by molly »

Bruce44

  • New Member
  • *
  • Posts: 15
Thanx Molly for your answer.

So, it is no more necessary to pass the {$I %FILE%}, {$I %LINE%} parameters
The following code works (almost) well :

uses lineinfo;

procedure syslog_add (s_message : shortstring);
var
   p_code   : CodePointer;
   s_func, s_source  : shortstring;
   l_line      : longint;
begin
   p_code :=  get_caller_addr (get_frame);
   l_line := -1;
   s_source := '';
   s_func := '';
   if (p_code <> nil) then
      if (GetLineInfo (longint(p_code), s_func, s_source, l_line) = true) then
   begin
   ....
   end;                                           
...

I can get the source file of the calling procedure and the line number from which it has been called.

But it seems to work only if syslog_add is called from the main program. Not from a class method (GetLineInfo return false).
But maybe the reason is elsewhere
I will investigate...

molly

  • Hero Member
  • *****
  • Posts: 2330
But maybe the reason is elsewhere
yes  ;)

Quote
I will investigate...
No need, see below.

To make it easier for yourself, inline your syslog function so that you don't have to trace back. You can then use Get_pc_addr to use as codepointer for getlineinfo().

procedure sys_log(msg: string); inline;

In theory that should work as the inlined procedure becomes part of the method itself.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
That actually really works in practise too ;) But it trashes when compiled w/o -gl so care should be taken. Anyway: a release build in C or C++ crashes too.. Maybe there is the misunderstanding: those macro's rely on compiled in debug info. (absolute nono for production code imo)

You can only do so much with a compiled language. Any of the languages mentioned in this thread.
« Last Edit: August 02, 2016, 04:41:22 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

molly

  • Hero Member
  • *****
  • Posts: 2330
That actually really works in practise too ;)
Yes, i know ;)

Its needed to use the inline construction _or_ you'd need to trace back the callers address. Dealers choice  :D

Quote
But it trashes when compiled w/o -gl so care should be taken.
Yeah, i probably should have mentioned that from the beginning. Proper Ifdefs should be in place if you want to do everything safely.

Actually, i only allow for this construction to compile in debug mode.

Since unit lineinfo is required anyhows. It can be be called safely, if you check the result and act accordingly (as far as tested by me).

Quote
You can only do so much with a compiled language. Any of the languages mentioned in this thread.
Yups. And with that i also ask would one really require/need such traces in production code ?

ah, an edit:
Maybe there is the misunderstanding: those macro's rely on compiled in debug info. (absolute nono for production code imo)
Following is purely theoretical as i haven't tested: You could perhaps compile with external debug file info, make the proper checks for the calls and supply the external debug file to users that require such a trace. I'm not sure if that's a good idea either but should (in theory) be possible.
« Last Edit: August 02, 2016, 05:13:56 pm by molly »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Ok I rephrase "those macro's rely on compiled in debug info." into "Those macro's rely on associated debug info." That can be either internal or as an external file but still needs (basically) the same code  since it is governed by the linker, not the compiler..
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018