Mike Torrettinni 198 Posted October 29, 2019 (edited) 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 October 29, 2019 by Mike Torrettinni Share this post Link to post
dummzeuch 1505 Posted October 29, 2019 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
Mike Torrettinni 198 Posted October 29, 2019 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
Fr0sT.Brutal 900 Posted October 30, 2019 (edited) 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 October 30, 2019 by Fr0sT.Brutal Share this post Link to post
Rollo62 536 Posted October 30, 2019 You could also use several overloaded Log methods, with 1, 2, 3, ... strings. Share this post Link to post
Mike Torrettinni 198 Posted October 30, 2019 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
Mike Torrettinni 198 Posted October 30, 2019 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
Fr0sT.Brutal 900 Posted October 30, 2019 array of const 😉 Just like Format(). Inside the method you get array of TVarData IIRC Share this post Link to post
Mike Torrettinni 198 Posted October 30, 2019 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
Fr0sT.Brutal 900 Posted October 30, 2019 (edited) 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 October 30, 2019 by Fr0sT.Brutal 1 Share this post Link to post
Mike Torrettinni 198 Posted October 30, 2019 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
Fr0sT.Brutal 900 Posted October 30, 2019 Yep, sure. You'll have to call LogMethod with string representations of these types. Share this post Link to post
Rollo62 536 Posted October 30, 2019 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. 1 Share this post Link to post
Mike Torrettinni 198 Posted October 30, 2019 Thanks for suggestions, will see what I can come up with. Share this post Link to post