digitalmars.D.learn - Analyze debug condition in template
- novice3 (30/30) Oct 25 2021 Hello.
- novice3 (17/17) Oct 25 2021 i want to eliminate "debug(func1)" and "debug(func2)" from code:
- Kagamin (3/3) Oct 26 2021 `debug(func1)writefln(...)`
- novice2 (8/8) Oct 26 2021 Thanks Kagamin!
- novice2 (4/7) Oct 26 2021 i want to eliminate "debug(func1)"
- Steven Schveighoffer (9/17) Oct 26 2021 ```d
Hello. Need advice: Is it possible analyze "debug" condition in template, obtained from instantiation place? Like we using __LINE__ ? For example, i have template for logging: ```d void logf(string func = __FUNCTION__, int line = __LINE__, A...)(string fmt, A args) { // here i want analyze, if debug == func // and writef conditionally writefln("%s:%d " ~ fmt, func, line, args); } ``` And i want switch on logging for one function module1.func(), then i declare in module1 ```d debug = func1; // enable logging for func1 void func1() { ... logf("var1=%d", var1); ... } ``` And i want to check in logf()() template: is debug condition equal function name, which instantiate logf() template. Just advise me direction or proper keyword ... Thanks.
Oct 25 2021
i want to eliminate "debug(func1)" and "debug(func2)" from code: ```d debug = func1; // enable logging for func1 //debug = func2; // disable logging for func2 void func1() { ... debug(func1) logf("var1=%d", var1); ... } void func2() { ... debug(func2) logf("var1=%d", var1); ... } ```
Oct 25 2021
`debug(func1)writefln(...)` But specify a global debug version for the compiler: `dmd -debug=func1 app.d`
Oct 26 2021
Thanks Kagamin! One more way, i think, mark function with UDA, and then analize UDA in template. But i have problem to implement this: i have function name __FUNCTION__ in template as sting, but __traits(getAttributes, __FUNCTION__) want symbol, not string as second parameter :(
Oct 26 2021
On Tuesday, 26 October 2021 at 09:44:42 UTC, Kagamin wrote:`debug(func1)writefln(...)` But specify a global debug version for the compiler: `dmd -debug=func1 app.d`i want to eliminate "debug(func1)" i want to be able on/off debugging for one function or another, and logf() template should "understand", those on/off for caller
Oct 26 2021
On 10/26/21 11:39 AM, novice2 wrote:On Tuesday, 26 October 2021 at 09:44:42 UTC, Kagamin wrote:```d mixin("debug(" ~ func ~ ") doRealThing();"); ``` Now, you just have to actually log in `doRealThing` (a local function) or wrap however you want. Note that setting debug versions doesn't get seen in imported modules, only ones specified on the command line will be seen inside your logger. -Steve`debug(func1)writefln(...)` But specify a global debug version for the compiler: `dmd -debug=func1 app.d`i want to eliminate "debug(func1)" i want to be able on/off debugging for one function or another, and logf() template should "understand", those on/off for caller
Oct 26 2021
On Tuesday, 26 October 2021 at 15:53:54 UTC, Steven Schveighoffer wrote:mixin("debug(" ~ func ~ ") doRealThing();");Thank you, Steven. Unfortunately, all variants with global "-debug" in command line is unhandy. It leads to ugly, very big command line :(Note that setting debug versions doesn't get seen in imported modules, only ones specified on the command line will be seen inside your logger.This is the problem. At the moment i failed to get "debug" condition from one module (caller) in other module (logger). Variant with UDA failed too - i can't get UDA from one module (caller) in other module (logger).
Oct 26 2021
You can do something like ```d enum LogSettings { func1,func2,func3 } alias logger!LogSettings logf; void func1() { logf(...); } ``` Then the logger can inspect symbols in the template argument and compare their names to the function name.
Oct 27 2021
On Wednesday, 27 October 2021 at 08:14:29 UTC, Kagamin wrote: ...Then the logger can inspect symbols in the template argument and compare their names to the function name.Aha, thank you, i will try!
Oct 27 2021