How to determine file, function and line number?

前端 未结 7 1075
清歌不尽
清歌不尽 2020-11-28 06:14

In C++, I can print debug output like this:

printf(
   \"FILE: %s, FUNC: %s, LINE: %d, LOG: %s\\n\",
   __FILE__,
   __FUNCTION__,
   __LINE__,
   logmessage         


        
相关标签:
7条回答
  • 2020-11-28 07:12
    import inspect
        .
        .
        .
    def __LINE__():
        try:
            raise Exception
        except:
            return sys.exc_info()[2].tb_frame.f_back.f_lineno
    
    def __FILE__():
        return inspect.currentframe().f_code.co_filename
        .
        .
        .
    print "file: '%s', line: %d" % (__FILE__(), __LINE__())
    
    0 讨论(0)
提交回复
热议问题