热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在Ruby中获取堆栈跟踪对象?-HowtogetastacktraceobjectinRuby?

IneedtogetastacktraceobjectinRuby;nottoprintit,justtogetittodosomerecordingand

I need to get a stack trace object in Ruby; not to print it, just to get it to do some recording and dumping for later analysis. Is that possible? How?

我需要在Ruby中获得一个堆栈跟踪对象;不是打印出来,只是为了让它做一些记录和转储以便以后分析。这有可能吗?如何?

6 个解决方案

#1


76  

You can use Kernel.caller for this. The same method is used when generating stack traces for exceptions.

您可以使用内核。调用者。在为异常生成堆栈跟踪时也使用相同的方法。

From the docs:

从文档:

def a(skip)
  caller(skip)
end
def b(skip)
  a(skip)
end
def c(skip)
  b(skip)
end
c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10"]
c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
c(2) #=> ["prog:8:in `c'", "prog:12"]
c(3) #=> ["prog:13"]

#2


24  

Try

试一试

Thread.current.backtrace.join("\n")

#3


12  

Try error.backtrace:

试试error.backtrace:

# Returns any backtrace associated with the exception.  
# The backtrace is an array of strings, each containing either ``filename:lineNo: in `method’’’ or ``filename:lineNo.’‘

def a
  raise "boom"
end

def b
  a()
end

begin
  b()
rescue => detail
  print detail.backtrace.join("\n")
end

produces:

生产:

prog.rb:2:in `a'
prog.rb:6:in `b'
prog.rb:10

#4


7  

For Ruby 2.0+, you can use Kernel#caller_locations. It is essentially the same as Kernel#caller (covered in Sven Koschnicke's answer), except that instead of returning an array of strings, it returns an array of Thread::Backtrace::Location objects. Thread::Backtrace::Location provides methods such as path, lineno, and base_label, which may be useful when you need access to specific details about the stack trace, and not just a raw string.

对于Ruby 2.0+,您可以使用内核#caller_locations。它本质上与内核#caller (Sven Koschnicke的答案中有介绍)相同,除了返回一个字符串数组之外,它返回一个Thread::Backtrace::Location对象数组。Location提供路径、lineno和base_label等方法,当您需要访问有关堆栈跟踪的特定细节而不仅仅是一个原始字符串时,这些方法可能很有用。

From the docs:

从文档:

caller_locations(start=1, length=nil) → array or nil

caller_locations(range) → array or nil

Returns the current execution stack—an array containing backtrace location objects.

返回当前执行堆栈——一个包含回溯位置对象的数组。

See Thread::Backtrace::Location for more information.

有关更多信息,请参见线程:::Backtrace::Location。

The optional start parameter determines the number of initial stack entries to omit from the top of the stack.

可选的启动参数决定从堆栈顶部删除初始堆栈条目的数量。

A second optional length parameter can be used to limit how many entries are returned from the stack.

第二个可选长度参数可用于限制从堆栈返回多少个条目。

Returns nil if start is greater than the size of current execution stack.

如果start大于当前执行堆栈的大小,则返回nil。

Optionally you can pass a range, which will return an array containing the entries within the specified range.

您可以随意地传递一个范围,该范围将返回一个包含指定范围内的条目的数组。

Usage example:

使用的例子:

def a
  caller_locations(0)
end
def b
  a
end
def c
  b
end

c.map(&:base_label)
#=> ["a", "b", "c", "
"]

#5


2  

You can create your own if you want as well. As demonstrated in Eloquent Ruby by Russ Olsen:

如果你愿意,你也可以自己创造。正如Russ Olsen在雄辩的Ruby中所展示的:

# define a proc to use that will handle your trace 
proc_object = proc do |event, file, line, id, binding, klass| 
  puts "#{event} in #{file}/#{line} #{id} #{klass}"
end 

# tell Ruby to use your proc on traceable events
set_trace_func(proc_object)

#6


2  

Thread.current.backtrace

This will give you an array which contains all the lines that you may get in any normal backtrace.

这将为您提供一个数组,该数组包含任何正常回溯中可能得到的所有行。


推荐阅读
author-avatar
王聪明2011
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有