View stack trace using [Python] inspect

I created a function to display a stack trace using inspect.

hello_world.py


# -*- coding: utf-8 -*-

import inspect

def printmessage(message):
    record = inspect.stack()[1]
    frame = record[0]
    info = inspect.getframeinfo(frame)
    fn = str(info.filename).split('/')[-1]
    print '[TAG]' + fn + '#' + info.function + '(L:'+str(info.lineno) + ') : ' + message

def printstack():
    for num in range(len(inspect.stack())):
        if num == 0:
            continue
        record = inspect.stack()[num]
        frame = record[0]
        info = inspect.getframeinfo(frame)
        fn = str(info.filename).split('/')[-1]
        print '[TAG][' + str(num) + ']' + fn + '#' + info.function + '(L:'+str(info.lineno) + ')'

if __name__ == '__main__':
    printmessage('hogehoge')
    print ('-----------')
    printstack()

result


[TAG]hello_world.py#<module>(L:23) : hogehoge
-----------
[TAG][1]hello_world.py#<module>(L:25)
[TAG][2]visualstudio_py_util.py#exec_code(L:95)
[TAG][3]visualstudio_py_util.py#exec_file(L:119)
[TAG][4]visualstudio_py_debugger.py#debug(L:2624)
[TAG][5]visualstudio_py_launcher.py#<module>(L:91)

[reference] https://docs.python.jp/3/library/inspect.html

Recommended Posts

View stack trace using [Python] inspect
Show stack trace with python exception
Depth-first search using stack in Python
Python #stack queue
Start using Python
Scraping using Python
View drug reviews using a list in Python
Operate Redmine using Python Redmine
Fibonacci sequence using Python
Data analysis using Python 0
Data cleaning using Python
Using Python #external packages
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
Try using Tweepy [Python2.7]
[Blender Python] Display images on 3D View using OpenGL (bgl)
View using the python module of Nifty Cloud mobile backend
Python notes using perl-ternary operator
Flatten using Python yield from
Save images using python3 requests
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Using Quaternion with Python ~ numpy-quaternion ~
Try using Kubernetes Client -Python-
Python notes using perl-special variables
Scraping using Python 3.5 Async syntax
Website change monitoring using python
Post to Twitter using Python
Search algorithm using word2vec [python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Create JIRA tickets using Python
Instrument control using Python [pyvisa]
Manipulate spreadsheets locally using Python
Python memo using perl --join
View Python communication with Fiddler
Web scraping using Selenium (Python)
Stack and Queue in Python
[Python] I tried using OpenPose
[Python] JSON validation using Voluptuous
Broadcast on LINE using python
Data analysis using python pandas
Translate using googletrans in Python
Using Python mode in Processing
Using OpenCV with Python @Mac
[Python] Shooting game using pyxel
Send using Python with Gmail