Jump to content
Sign in to follow this  
Mike Torrettinni

Parse and Log method parameters

Recommended Posts

For debugging purpose I'm thinking of creating a simple parses that will parse method arguments and create simple Log call function. Something like this:

 

The parser would have simple form with Memo control, that I would copy paste a method definition into it, for example:

procedure MethodA(const Param1: string; var Param2, Param3: integer; Param4: string);

And parser would parse method name and parameters and based on the type create Log line:

LogMethod('MethodA', 'Str: Param1 = ' + Param1 + ' Int: Param2 = ' + Param2.toString + ' Int: Param3 = ' + Param3.toString + ' Str: Param4 = ' + Param4);

And LogMethod makes simple output of the line. I would just add this log line into first line in the MethodA.

 

The purpose is that I can enable logging parameters with a simple copy paste, and no need to manually create LogMethod calls.

 

Is there anything like similar already available? 

Edited by Mike Torrettinni

Share this post


Link to post

I wonder whether the string types also have a .toTtring method(helper). That would make writing such a tool so much simpler.

Share this post


Link to post
14 minutes ago, dummzeuch said:

I wonder whether the string types also have a .toTtring method(helper). That would make writing such a tool so much simpler.

Yes, parser needs to identify the type and convert to string as needed, either with .toString or by custom methods VarToStr... it will have to deal with custom types, too. and if you have functions to convert every type to string, then is just a matter of calling correct conversion.

 

 

 

Share this post


Link to post

You hardly need to print parameter names every time IMHO. So you can use general

LogMethod('MethodA', [par1, par2, par3]);

 

and create a code template for this. There would remain some manual work though

Edited by Fr0sT.Brutal

Share this post


Link to post

You could also use several overloaded Log methods, with 1, 2, 3, ... strings.

Share this post


Link to post
5 hours ago, Fr0sT.Brutal said:

You hardly need to print parameter names every time IMHO. So you can use general


LogMethod('MethodA', [par1, par2, par3]);

 

and create a code template for this. There would remain some manual work though

OK, if I skip the names, what would the LogMethod parameter type be to accept [par1, par2, par3]? array of what? They can be different types.

Share this post


Link to post
4 hours ago, Rollo62 said:

You could also use several overloaded Log methods, with 1, 2, 3, ... strings.

I'm not sure what you have in mind... if I have methods with parameters like:

(string, string, integer)

(integer, integer, integer, string)

.... and all other options of number and type of parameters.

Share this post


Link to post
3 minutes ago, Fr0sT.Brutal said:

array of const 😉 Just like Format(). Inside the method you get array of TVarData IIRC

Aha, I never used array of const, yet.

Share this post


Link to post
  function VarToStr(const VarRec: TVarRec): string;
  begin
    Result := ''; // compiler happy

	case VarRec.VType of
          vtInteger:
            Result := IntToStr(VarRec.VInteger);
          vtInt64:
            Result := IntToStr(VarRec.VInt64^);
          vtString:
            Result := string(VarRec.VString^);
          vtPChar:
            Result := string(VarRec.VPChar);
          vtPWideChar:
            Result := string(VarRec.VPWideChar);
          vtAnsiString:
            Result := string(AnsiString(VarRec.VAnsiString));
          vtWideString:
            Result := string(WideString(VarRec.VWideString));
          vtUnicodeString:
            Result := string(VarRec.VUnicodeString);
          vtChar:
            Result := Char(VarRec.VChar);
          vtWideChar:
            Result := VarRec.VWideChar;
        end;
    end;
  end;

Something like what you need. Extracted from bigger function and edited in browser so bugs are possible

Edited by Fr0sT.Brutal
  • Thanks 1

Share this post


Link to post

Great, thanks! I assume this doesn't work for array parameters or custom types, like: 

Param1: array of string; or

Param1: TCustomType; or

Param1: TArray<TCustomType>;

Share this post


Link to post
1 hour ago, Mike Torrettinni said:

I'm not sure what you have in mind... if I have methods with parameters like:

(string, string, integer)

(integer, integer, integer, string)

.... and all other options of number and type of parameters.

You were asking for options, and if the signatures are well defined and limited thats a possible option too.

So that meant you could use overloads for the most common ones.
E.g. I try use mostly compatible signatures, also for different domains in my projects.

The array of const can catch then all the rest.

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×