今天在重构应用时,需要将一些通用调试信息集中在中间函数中打印。
这就要求在中间函数中执行打印调试信息时输出上下文的信息,如调用的函数名之类。
baidu到python的inspect.stack()模块可以实现,但是打印在控制台上的命令太乱,找了半天格式化工具也没找到,只好手动稍微格式化一下。
[
(<frame object at 0x7f3f005e9880>,'/home/lion/workspace/SmartOpenstack/SmartOpenstack/openstackapi.py', 16, 'restclient', ['logging.debug("Restclient:%s" % (inspect.stack()))\n'], 0),
(<frame object at 0x7f3f005e93e0>, '/home/lion/workspace/SmartOpenstack/SmartOpenstack/openstackapi.py', 30, 'getToken', ['r=restclient(host,port,url,method,body,headers)\n'], 0),
(<frame object at 0x7f3f005e9200>, '/home/lion/workspace/SmartOpenstack/SmartOpenstack/forms.py', 22, 'validate', ["return getToken(cd['username'],cd['password'],cd['openstack_url'])\n"], 0),
(<frame object at 0x7f3f005e1140>, '/home/lion/workspace/SmartOpenstack/SmartOpenstack/views.py', 46, 'logon', ['service = form.validate()\n'], 0),
(<frame object at 0x7f3f00012bd0>, '/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py', 111, 'get_response', ['response = callback(request, *callback_args, **callback_kwargs)\n'], 0),
(<frame object at 0x7f3f00009060>, '/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py', 241, '__call__', ['response = self.get_response(request)\n'], 0),
(<frame object at 0x7f3f00008cb0>, '/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py', 67, '__call__', ['return self.application(environ, start_response)\n'], 0), (<frame object at 0x7f3f00007300>, '/usr/lib/python2.7/wsgiref/handlers.py', 85, 'run', ['self.result = application(self.environ, self.start_response)\n'], 0),
(<frame object at 0x7f3f00003bc0>, '/usr/lib/python2.7/wsgiref/simple_server.py', 124, 'handle', ['handler.run(self.server.get_app())\n'], 0),
(<frame object at 0x7f3f00003010>, '/usr/lib/python2.7/SocketServer.py', 638, '__init__', ['self.handle()\n'], 0),
(<frame object at 0x7f3f000012f0>, '/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py', 139, '__init__', [' super(WSGIRequestHandler, self).__init__(*args, **kwargs)\n'], 0), (<frame object at 0x7f3f00001110>, '/usr/lib/python2.7/SocketServer.py', 323, 'finish_request', ['self.RequestHandlerClass(request, client_address, self)\n'], 0),
(<frame object at 0x7f3f00000f30>, '/usr/lib/python2.7/SocketServer.py', 582, 'process_request_thread', ['self.finish_request(request, client_address)\n'], 0),
(<frame object at 0x7f3f00000d60>, '/usr/lib/python2.7/threading.py', 504, 'run', ['self.__target(*self.__args, **self.__kwargs)\n'], 0),
(<frame object at 0x7f3f00000b20>, '/usr/lib/python2.7/threading.py', 551, '__bootstrap_inner', ['self.run()\n'], 0),
(<frame object at 0x7f3f00000950>, '/usr/lib/python2.7/threading.py', 524, '__bootstrap', ['self.__bootstrap_inner()\n'], 0)
]
这下就清晰了,可以看出
第一列是对象名,第二列是当前脚本文件名,第三列是行数,第四列是函数名,第五列是具体执行的程序。
第一行是当前函数,第二行是父级函数,。。以此往上钻取,基本上只有前两三行有用。
看来有空开发一个列表格式化工具,还是蛮有用的。
=-=-=-=-=
Powered by Blogilo