本文簡要介紹ruby語言中 TracePoint類 的用法。
Document-class: TracePoint
在麵向對象的 API 中提供 Kernel#set_trace_func 函數的類。
示例
我們可以使用 TracePoint 專門針對異常收集信息:
trace = TracePoint.new(:raise) do |tp|
p [tp.lineno, tp.event, tp.raised_exception]
end
#=> #<TracePoint:disabled>
trace.enable
#=> false
0 / 0
#=> [5, :raise, #<ZeroDivisionError: divided by 0>]
事件
如果您未指定要偵聽的事件類型, TracePoint 將包括所有可用事件。
注意不要依賴於當前的事件集,因為此列表可能會發生變化。相反,建議您指定要使用的事件類型。
要過濾跟蹤的內容,您可以將以下任何內容作為 events 傳遞:
:line-
在新行上執行表達式或語句
:class-
開始一個類或模塊定義
:end-
完成一個類或模塊定義
:call-
調用 Ruby 方法
:return-
從 Ruby 方法返回
:c_call-
調用 C-language 例程
:c_return-
從 C-language 例程返回
:raise-
引發異常
:b_call-
塊入口處的事件鉤子
:b_return-
塊結束時的事件鉤子
:a_call-
所有調用的事件鉤子(
call,b_call和c_call) :a_return-
所有返回的事件鉤子(
return、b_return和c_return) :thread_begin-
線程開始處的事件鉤子
:thread_end-
線程結束時的事件鉤子
:fiber_switch-
光纖交換機上的事件鉤子
:script_compiled-
編譯新的 Ruby 代碼(使用
eval、load或require)
相關用法
- Ruby TracePoint.defined_class用法及代碼示例
- Ruby TracePoint.self用法及代碼示例
- Ruby TracePoint.new用法及代碼示例
- Ruby TracePoint.disable用法及代碼示例
- Ruby TracePoint.enable用法及代碼示例
- Ruby TracePoint.trace用法及代碼示例
- Ruby TrueClass.true | obj用法及代碼示例
- Ruby TreeBuilder類用法及代碼示例
- Ruby Time tv_sec用法及代碼示例
- Ruby Time usec用法及代碼示例
- Ruby TCPServer.accept用法及代碼示例
- Ruby Time yday()用法及代碼示例
- Ruby Time succ()用法及代碼示例
- Ruby Time mon()用法及代碼示例
- Ruby Time.gmtime用法及代碼示例
- Ruby Time iso8601用法及代碼示例
- Ruby Time.at用法及代碼示例
- Ruby Thread.kill用法及代碼示例
- Ruby Time.utc_offset用法及代碼示例
- Ruby Time.isdst用法及代碼示例
- Ruby Time.time + numeric用法及代碼示例
- Ruby Thread.pending_interrupt?用法及代碼示例
- Ruby Time wednesday?用法及代碼示例
- Ruby Time.wednesday?用法及代碼示例
- Ruby Thread kill()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 TracePoint類。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
