0

I have for debugging purposes created following workflow.

I defined a global macro

#define PRINT_CALL(x) std::cout << "(CALL) " << x << std::endl

to notify whenever something is called. Trivially it is used like so:

void foo() {
    PRINT_CALL("foo()");
    // do stuff
}

This makes debugging much easier in my use case. Is there a way to automate this? Here is what I had in mind in totally illegal C++ syntax:

#define_decorator call_me(x) std::cout << "(CALL) " << x << std::endl

@call_me
void foo() {
    // do stuff
}
// ==> resulting in the exact same outcome as above.

I hope you get the idea. As I said, syntax is obviously trash; since I don't know of such a functionality and didn't find any useful information online, maybe someone knows a hack. Appreciate all answers!

Gamoron
  • 29
  • 3
  • With [`std::source_location`](https://en.cppreference.com/w/cpp/utility/source_location), your MACRO can be simplified, and so, your decorator is simply `PRINT_CALL();` as first instruction. – Jarod42 Feb 26 '23 at 13:45

1 Answers1

0

Assuming you really want to litter your code with debug annotations like this, you could use any of __FUNCTION__, __func__, __PRETTY_FUNCTION__ etc., depending on your compiler and the language standard you are using. I.e. define the macro like this

#define PRINT_CALL() std::cout << "(CALL) " << __func__ << std::endl

and use it like so

void foo() {
    PRINT_CALL(); // No need to rewrite the name 'foo'
    // do stuff
}

See here for more: What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__?

However I would strongly advise against this and consider using a debugger and breakpoints.

chrysante
  • 2,328
  • 4
  • 24