堆栈跟踪作为字符串 [英] Stack trace as a string

查看:80
本文介绍了堆栈跟踪作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以将堆栈跟踪作为字符串获取?

Is there any method to get a stack trace as a string?

查看调试包( https://golang.org/pkg/runtime/debug/)只能打印到标准输出.

Looking at the debug package (https://golang.org/pkg/runtime/debug/) it can only be printed to standard output.

推荐答案

runtime.Stack()将格式化的堆栈跟踪放入提供的[]byte中.然后,您可以将其转换为字符串.

runtime.Stack() puts a formatted stack trace into a supplied []byte. You can then convert that to a string.

您还可以使用debug.Stack(),它会分配足够大的缓冲区来容纳整个堆栈跟踪,并使用runtime.Stack将跟踪放入其中,然后返回缓冲区([]byte).

You can also use debug.Stack(), which allocates a large enough buffer to hold the entire stack trace, puts the trace in it using runtime.Stack, and returns the buffer ([]byte).

这篇关于堆栈跟踪作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆