gpt4 book ai didi

ruby - 是否有等效于 Ruby 的 shell 脚本的 xtrace 选项?

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:09 24 4
gpt4 key购买 nike

调试 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 有更好的方法吗?

最佳答案

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

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 key 或 PII

关于ruby - 是否有等效于 Ruby 的 shell 脚本的 xtrace 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43877146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com