digitalmars.D.learn - Logging Function Parameters
- dom (11/11) Mar 17 2018 Hi,
- aliak (36/48) Mar 18 2018 You may be able to do something with a mixin. I tried this but I
- Dennis (7/11) Mar 18 2018 You can't mix part of a function call in: "Mixed in text must
- Aliak (2/13) Mar 19 2018 Ah! Thanks for the clarification.
- Seb (4/16) Mar 18 2018 You can't get the parameters names at the moment, but there's a
Hi, I am looking for a method to log the current function name + parameters. Getting the name of the current function is simply possible with __PRETTY_FUNCTION__ Is there some possibility to generically access the parameters of a function such that they can be iterated and printed out? currently i have something like this: log.info(__PRETTY_FUNCTION__, " ", parameter1, " ", parameter2); i would like to get to something like that: log.info(__PRETTY_FUNCTION__, " ", parameters.join(", "));
Mar 17 2018
On Saturday, 17 March 2018 at 10:34:41 UTC, dom wrote:Hi, I am looking for a method to log the current function name + parameters. Getting the name of the current function is simply possible with __PRETTY_FUNCTION__ Is there some possibility to generically access the parameters of a function such that they can be iterated and printed out? currently i have something like this: log.info(__PRETTY_FUNCTION__, " ", parameter1, " ", parameter2); i would like to get to something like that: log.info(__PRETTY_FUNCTION__, " ", parameters.join(", "));You may be able to do something with a mixin. I tried this but I think I'm hitting a compiler bug, or I'm just using mixins wrong. import std.stdio; string arguments(alias f)() { import std.meta: AliasSeq; import std.traits: ParameterIdentifierTuple; import std.array: join; string[] args; foreach (a; [AliasSeq!(ParameterIdentifierTuple!f)]) { args ~= `"` ~ a ~ `: ", ` ~ a; } return args.join(`, ", ", `); } // calling writeln(mixin(arguments!f)) should do what you want. void f(int a, int b, int c) { // But you get a: // Error: Using the result of a comma expression is not allowed // writeln(mixin(arguments!f)); auto value = arguments!f; // "a: ", a, ", ", "b: ", b, ", ", "c: ", c writeln(value); // This is ok: writeln("a: ", a, ", ", "b: ", b, ", ", "c: ", c); } void main() { f(1, 2, 3); } You might be able to start with that and get something that works. Or maybe someone knows how to make the mixin part above compile. I have a feeling you may need to make the arguments() function return a string("p0: ", arg0, ", p1: ", arg1" ...) instead so that it's a full string instead of a comma separated expression maybe. Cheers
Mar 18 2018
On Sunday, 18 March 2018 at 22:57:15 UTC, aliak wrote:// But you get a: // Error: Using the result of a comma expression is not allowed // writeln(mixin(arguments!f));You can't mix part of a function call in: "Mixed in text must form complete declarations, statements, or expressions." (https://dlang.org/articles/mixin.html) In this case, it evaluates to a comma expression, which is deprecated. If you put the "writeln();" inside the mixin string it should form a complete statement and compile.
Mar 18 2018
On Sunday, 18 March 2018 at 23:17:58 UTC, Dennis wrote:On Sunday, 18 March 2018 at 22:57:15 UTC, aliak wrote:Ah! Thanks for the clarification.// But you get a: // Error: Using the result of a comma expression is not allowed // writeln(mixin(arguments!f));You can't mix part of a function call in: "Mixed in text must form complete declarations, statements, or expressions." (https://dlang.org/articles/mixin.html) In this case, it evaluates to a comma expression, which is deprecated. If you put the "writeln();" inside the mixin string it should form a complete statement and compile.
Mar 19 2018
On Saturday, 17 March 2018 at 10:34:41 UTC, dom wrote:Hi, I am looking for a method to log the current function name + parameters. Getting the name of the current function is simply possible with __PRETTY_FUNCTION__ Is there some possibility to generically access the parameters of a function such that they can be iterated and printed out? currently i have something like this: log.info(__PRETTY_FUNCTION__, " ", parameter1, " ", parameter2); i would like to get to something like that: log.info(__PRETTY_FUNCTION__, " ", parameters.join(", "));You can't get the parameters names at the moment, but there's a PR for it: https://github.com/dlang/dmd/pull/7821
Mar 18 2018