Is there a way to do the same thing in entry and return of a bunch of functions?

Stefanos Baziotis sdi1600105 at di.uoa.gr
Tue Sep 17 17:11:09 UTC 2019


I think it's better to give a concrete example rather than 
explaining this vaguely.

- For those who are familiar with LDC internals:
I want to create something like LOG_SCOPE. You can skip the 
explanation.

- For those who are not:
Imagine that you want to track down how deep in the call stack 
you are,
so that you can print nice log messages.

That is, if you have:

func3() {
   debug_log("message in func3");
}

func2() {
   debug_log("message in func2");
   func3();
   debug_log("message in func2");
}

func1() {
   func2();
}

main() {
   func1();
}

In this case, I'd like to have something like this:
* * message in func 2
* * * message in func3
* * message in func2

So, we could create a global variable CALL_DEPTH or smth, and in 
every
function right at the beginning, do: CALL_DEPTH += 1;
and in the end: CALL_DEPTH -= 1;

And then implement debug_log as (skipping the printf-like things 
etc.):
   for (int i = 0; i != CALL_DEPTH; ++i)
     printf("* ");

But the thing is, now we have to put the += and -= in every 
function,
when it is really common in all of them and there's no reason to 
be visible
anyway.

LDC does something that IMO is ingenious. It's something like 
(it's C++):
#define LOG_SCOPE LoggerObj _logscope;

But LoggerObj has a constructor that does the += and the 
destructor that does
-=. So, you can put just one line of:
LOG_SCOPE;

at any point inside a function and the desired thing is done 
almost invisibly.


-- The question --
Can we do better ? For one, I believe that because D does not 
have a preprocessor,
we have to do an actual declaration which would be somewhat more 
verbose.
Or do a mixin that does it. mixin can help as it can be more 
complicated
and also we can access local scope (although I don't think this 
is a good idea).

But in both cases, they're not totally invisible.

Can we do something like: func1, func2 and func3, when they enter 
do the X
and when they return, they do the Y.

Thanks,
Stefanos


More information about the Digitalmars-d-learn mailing list