Thread overview
Analyze debug condition in template
Oct 26, 2021
novice3
Oct 26, 2021
novice3
Oct 26, 2021
Kagamin
Oct 26, 2021
novice2
Oct 26, 2021
novice2
Oct 27, 2021
novice2
Oct 27, 2021
Kagamin
Oct 27, 2021
novice2
October 26, 2021

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:

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

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.

October 26, 2021

i want to eliminate "debug(func1)" and "debug(func2)" from code:

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);
    ...
}
October 26, 2021

debug(func1)writefln(...)
But specify a global debug version for the compiler:
dmd -debug=func1 app.d

October 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 :(
October 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
October 26, 2021

On 10/26/21 11:39 AM, novice2 wrote:

>

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

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

October 27, 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).

October 27, 2021

You can do something like

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.

October 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!