Class: Binnacle::Trap::Backtrace::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/binnacle/trap/backtrace.rb

Overview

Handles backtrace parsing line by line

Constant Summary collapse

GEM_PATHS =
(Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) }
GEMS_REGEXP =
%r{(#{GEM_PATHS.join('|')})/gems/([^/]+)-([\w.]+)/(.*)}
GEMS_RESULT =
'\2 (\3) \4'.freeze
INPUT_FORMAT =

regexp (optionnally allowing leading X: for windows support)

%r{^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in `([^']+)')?$}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, number, method_name) ⇒ Line

Returns a new instance of Line.



50
51
52
53
54
# File 'lib/binnacle/trap/backtrace.rb', line 50

def initialize(file, number, method_name)
  @file   = file
  @number = number
  @method_name = method_name
end

Instance Attribute Details

#fileObject (readonly)

The file portion of the line (such as app/models/user.rb)



21
22
23
# File 'lib/binnacle/trap/backtrace.rb', line 21

def file
  @file
end

#method_nameObject (readonly)

The method_name of the line (such as index)



27
28
29
# File 'lib/binnacle/trap/backtrace.rb', line 27

def method_name
  @method_name
end

#numberObject (readonly)

The line number portion of the line



24
25
26
# File 'lib/binnacle/trap/backtrace.rb', line 24

def number
  @number
end

Class Method Details

.parse(unparsed_line) ⇒ Line

Parses a single line of a given backtrace

Parameters:

  • unparsed_line (String)

    The raw line from caller or some backtrace

Returns:

  • (Line)

    The parsed backtrace line



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/binnacle/trap/backtrace.rb', line 32

def self.parse(unparsed_line)
  _, file, number, method_name = unparsed_line.match(INPUT_FORMAT).to_a

  # Remove Rails root from log lines
  file = defined?(Rails) ? file.gsub(Rails.root.to_s, '') : file

  if file
    # Clean those ERB lines, we don't need the internal autogenerated
    # ERB method, what we do need (line number in ERB file) is already there
    file = file.sub /(\.erb:\d+)\:in `__.*$/, "\\1"

    # Remove RubyGems root directories
    file = file.sub(GEMS_REGEXP, GEMS_RESULT)
  end

  new(file, number, method_name)
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
# File 'lib/binnacle/trap/backtrace.rb', line 61

def ==(other)
  to_s == other.to_s
end

#empty?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/binnacle/trap/backtrace.rb', line 69

def empty?
  file.nil? && number.nil? && method_name.nil?
end

#inspectObject



65
66
67
# File 'lib/binnacle/trap/backtrace.rb', line 65

def inspect
  "<Line:#{to_s}>"
end

#to_sObject

Reconstructs the line in a readable fashion



57
58
59
# File 'lib/binnacle/trap/backtrace.rb', line 57

def to_s
  "#{file}:#{number}:in `#{method_name}'"
end