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

时间:2010-09-30 09:00:32

标签: ruby stack stack-trace

我需要在Ruby中获取堆栈跟踪对象;不要打印它,只是为了让它做一些录音和倾倒以供以后分析。那可能吗?怎么样?

6 个答案:

答案 0 :(得分:79)

您可以使用Kernel.caller。为异常生成堆栈跟踪时使用相同的方法。

来自文档:

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"]

答案 1 :(得分:30)

尝试

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

答案 2 :(得分:12)

尝试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

产生

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

答案 3 :(得分:7)

对于Ruby 2.0+,您可以使用Kernel#caller_locations。它与Kernel#caller(在Sven Koschnicke's answer中涵盖)基本相同,除了返回一个Thread::Backtrace::Location个对象的数组而不是返回一个字符串数组。 Thread::Backtrace::Location提供pathlinenobase_label等方法,当您需要访问有关堆栈跟踪的特定详细信息而不仅仅是原始字符串时,这些方法可能很有用

来自the docs

  

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

     

caller_locations(range)→array或nil

     

返回当前执行堆栈 - 包含backtrace的数组   位置对象。

     

有关详细信息,请参阅Thread::Backtrace::Location

     

可选的start参数确定初始堆栈的数量   要从堆栈顶部省略的条目。

     

第二个可选length参数可用于限制数量   条目从堆栈返回。

     

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

     

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

用法示例:

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

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

答案 4 :(得分:3)

Thread.current.backtrace

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

答案 5 :(得分:2)

如果您愿意,也可以创建自己的。正如Russ Olsen在 Eloquent 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)