2

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 >".

sherrellbc
  • 4,650
  • 9
  • 48
  • 77

3 Answers3

4

You can inspect the last frame to retrieve the caller name and the rest is simple formatting, something like

import sys

def dprint(fmt=None, *args, **kwargs):
    try:
        name = sys._getframe(1).f_code.co_name
    except (IndexError, TypeError, AttributeError):  # something went wrong
        name = "<unknown>"
    print("[{}] {}".format(name, (fmt or "{}").format(*args, **kwargs)))

def testfunction1():
    dprint("Running {}", 1)

def testfunction2():
    dprint("Running {}", 2)

def main():
    dprint("Running {}", 0)
    testfunction1()
    testfunction2()
    return 0

if __name__ == "__main__":
    main()

# prints:
# [main] Running 0
# [testfunction1] Running 1
# [testfunction2] Running 2
zwer
  • 24,943
  • 3
  • 48
  • 66
4

I would suggest not using print at all, and instead setup a logger. The logging module has a funcName attribute available for all LogRecords.

You'll then have (code taken from zwer's answer):

import logging

def create_logger(app_name=None):
    logger = logging.getLogger(app_name or __name__)
    logger.setLevel(logging.DEBUG)
    log_format = '[%(asctime)-15s] [%(levelname)08s] (%(funcName)s %(message)s'
    logging.basicConfig(format=log_format)
    return logger

LOGGER = create_logger()

def testfunction1():
    LOGGER.info("Running %d", 1)

def testfunction2():
    LOGGER.error("Running %s", 2)

def main():
    LOGGER.debug("Running %03d", 0)
    testfunction1()
    testfunction2()

if __name__ == "__main__":
    main()

which will generate something similar to:

[2017-06-13 19:41:45,677] [   DEBUG] (main) Running 000
[2017-06-13 19:41:45,677] [    INFO] (testfunction1) Running 1
[2017-06-13 19:41:45,677] [   ERROR] (testfunction2) Running 2
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
1
import inspect
import sys
def DPRINT(fmt, *args):
    print "{} {} {}".format(sys._getframe(1).f_code.co_name, fmt, args)

    # below line is another way to get the name of a calling function
    # print "{} {} {}".format(inspect.stack()[1][3], fmt, args)

def testfunction1():
    DPRINT("Running: 1")

def testfunction2():
    DPRINT("Running: 2")

def main():
    DPRINT("Running: 0")
    testfunction1()
    testfunction2()

main()

I don't know if this could be helpful

Amey Kumar Samala
  • 904
  • 1
  • 7
  • 20