对于Ruby,是否有相当于shell脚本的xtrace选项?

时间:2017-05-09 18:17:41

标签: ruby

调试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有交互式调试器,例如prybyebug,但我正在寻找一些易于记录自动化脚本的东西。

我找到了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有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

我能够构建一个或多或少能够满足我所需要的模块:

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