Often times in C programs (and using GCC) I will create a debug print macro that includes the name of the current function. That is, something like:
#define DPRINTF(fmt, ...) printf("[%s] " fmt, __FUNCTION__, ##__VA_ARGS__)
When used, the current function will be prepended to each print, providing more useful debug information at runtime. For example, the following code
#include <stdio.h>
#define DPRINT(fmt, ...) printf("[%s] " fmt, __FUNCTION__, ##__VA_ARGS__)
void testfunction1(){
DPRINT("Running %d\n",1);
}
void testfunction2(){
DPRINT("Running %d\n",2);
}
int main(void) {
DPRINT("Running: %d\n",0);
testfunction1();
testfunction2();
return 0;
}
Would output:
[main] Running: 0
[testfunction1] Running 1
[testfunction2] Running 2
Can something like this be done in Python?
I searched around a bit and found this StackOverflow question explaining how inspect
can be used to read names from the stack. However, from what I've found Python does not support macros, so the same form as my C program cannot be used.
Is there some mechanism by which this type of debug printing can be supported? I tried lambdas, but the "function name" in this case prints as "< lambda >".