module PrettyBacktrace

Constants

CONFIG
EXCEPTION_MODIFIER_TRACE
VERSION

Public Class Methods

disable() { || ... } click to toggle source
# File lib/pretty_backtrace.rb, line 131
def self.disable
  if block_given?
    EXCEPTION_MODIFIER_TRACE.disable{
      yield
    }
  else
    EXCEPTION_MODIFIER_TRACE.disable
  end
end
effective_lines=(lines) click to toggle source
# File lib/pretty_backtrace.rb, line 149
def self.effective_lines=(lines)
  CONFIG[:effective_lines] = lines
end
enable() { || ... } click to toggle source

configuration

# File lib/pretty_backtrace.rb, line 121
def self.enable
  if block_given?
    EXCEPTION_MODIFIER_TRACE.enable{
      yield
    }
  else
    EXCEPTION_MODIFIER_TRACE.enable
  end
end
file_contents=(setting) click to toggle source
# File lib/pretty_backtrace.rb, line 145
def self.file_contents=(setting)
  CONFIG[:file_contents] = setting
end
iseq_local_variables(iseq) click to toggle source
# File lib/pretty_backtrace.rb, line 64
def self.iseq_local_variables iseq
  _,_,_,_,arg_info,name,path,a_path,_,type,lvs, * = iseq.to_a
  lvs.select{|lv| lv.is_a?(Symbol) || lv.is_a?(String) }
end
modify_trace_line(backtrace_location, absolute_path, lineno, local_variables_values) click to toggle source

local_variables_values is a Hash object containing pairs of a local variable name and value of local variable.

# File lib/pretty_backtrace.rb, line 73
def self.modify_trace_line backtrace_location, absolute_path, lineno, local_variables_values
  trace_line = backtrace_location.to_s

  if CONFIG[:multi_line]
    indent = ' ' * CONFIG[:multi_line_indent]
    additional = ''

    # file scope
    if CONFIG[:file_contents] && File.exists?(absolute_path)
      fclines = CONFIG[:file_contents_lines]
      start_line = lineno - 1 - 1 * fclines
      start_line = 0 if start_line < 0

      additional << indent + "[FILE]\n"
      additional << open(absolute_path){|f| f.readlines[start_line, 1 + 2 * fclines]}.map.with_index{|line, i|
        ln = start_line + i + 1
        line = line.chomp
        '%s%4d|%s' % [ln == lineno ? indent[0..-3] + "->" : indent, ln, line]
      }.join("\n")
      additional << "\n"
      additional << "\n"
    end

    # local variables
    unless local_variables_values.empty?
      additional << indent + "[LOCAL VARIABLES]\n"
      additional << local_variables_values.map{|lv, v|
        v = v[0..CONFIG[:multi_line_truncate_length]] + '...' if v.length > CONFIG[:multi_line_truncate_length]
        indent + "  #{lv} = #{v.to_s}"
      }.join("\n") + "\n"
    end

    trace_line = "#{trace_line}\n#{additional}" unless additional.empty?
  else
    unless local_variables_values.empty?
      additional = local_variables_values.map{|lv, v|
        v = v[0..CONFIG[:truncate_length]] + '...' if v.length > CONFIG[:truncate_length]
        "#{lv} = #{v.to_s}"
      }.join(", ")
      trace_line = "#{trace_line} (#{additional})"
    end
  end

  trace_line
end
multi_line=(setting) click to toggle source
# File lib/pretty_backtrace.rb, line 141
def self.multi_line=(setting)
  CONFIG[:multi_line] = setting
end