在调试shell脚本时,我发现使用xtrace
on运行有帮助:
-x xtrace Print commands and parameter
assignments when they are exe-
cuted, preceded by the value
of PS4.
例如:
$ set -x
$ s='Say again?'
+ s='Say again?'
# Other commands that might mess with the value of $s
$ echo $s
+ echo Say 'again?'
Say again?
我知道ruby有一些交互式的调试器,比如pry和byebug,但是我正在寻找一些易于打开的工具来记录自动化脚本。
我确实找到了xtrace gem,但它与php格式有关。
我还看到有一个Tracer class和一个TracePoint class似乎确实提供了一种在执行语句时打印语句的方法。但我还没有找到任何方法来打印变量的值(而不仅仅是变量名):
$ ruby -r tracer trace.rb
#0:/usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:Kernel:<: return gem_original_require(path)
#0:trace.rb:1::-: s='Say again?'
#0:trace.rb:2::-: puts s
Say again?
我想倒数第二行是:
#0:trace.rb:2::-: puts 'Say again?'
这可能吗?或者有更好的方法来使用ruby吗?
最佳答案
我能够构建一个模块,或多或少地实现了我想要的功能:
require 'pry'
=begin
We are always one line behind because the value of assignment comes
_after_ the trace is called. Without this, every assignment would look
like:
x = 1 #=> {:x=>nil}
It would be nice if the trace happened after assignment, but what can
you do?
=end
module Xtrace
# Only run the trace on the main file, not on require'd files.
# Possible room for expansion later.
@files = {$0 => Pry::Code.from_file($0)}
def Xtrace.print_trace
if @prev_line then
if @files[@path] then
line = @files[@path].around(@prev_line, 0).chomp
# When calling a method, don't make it look like it's being defined.
line.gsub!(/^\s*def\s*\b/, '') if @event == :call
values = []
@bind.local_variables.each do |v|
values << {v => @bind.local_variable_get(v)} if line =~ /\b#{v}\b/
end
STDERR.printf "%5s: %s", @prev_line, line
STDERR.printf " #=> %s", values.join(', ') unless values.empty?
STDERR.printf "\n"
end
end
end
@xtrace = TracePoint.trace(:line, :call) do |tp|
tp.disable
@bind=tp.binding
Xtrace.print_trace
# Other than the binding, everything we need to print comes from the
# previous trace call.
@prev_line = tp.lineno
@event=tp.event
@path=tp.path
tp.enable
end
# Need to print the trace one last time after the last line of code.
at_exit do
# It doesn't matter what we do in this last line. Any statement works.
# Also, it's a bit inconvenient that the disable command is itself traced.
@xtrace.disable
end
end
如果将其放在一个名为xtrace.rb的文件中并放在库加载路径中,则可以通过添加
require 'xtrace'
开始跟踪。它打印执行的每一行和方法调用的行号、实际代码和行中任何局部变量的值。对于简单的阶乘函数,输出可能如下: 3: def factorial(n)
8: puts factorial(3)
3: factorial(n) #=> {:n=>3}
4: return 1 if n <= 1 #=> {:n=>3}
5: return n*factorial(n-1) #=> {:n=>2}
3: factorial(n) #=> {:n=>2}
4: return 1 if n <= 1 #=> {:n=>2}
5: return n*factorial(n-1) #=> {:n=>1}
3: factorial(n) #=> {:n=>1}
4: return 1 if n <= 1
6
目前,它只关注局部变量。它还只跟踪已执行的文件,而不跟踪任何已加载的文件。目前还没有办法启用或禁用跟踪。跟踪在需要模块时开始,在执行时结束。跟踪输出转到stderr,格式是硬编码的。
如果使用此模块,请注意不要泄漏密码、api密钥或pii等敏感信息。