Class: CrashLog::Backtrace::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/crash_log/backtrace/line.rb

Constant Summary collapse

INPUT_FORMAT =

Backtrace line parsing regexp (optionnally allowing leading X: for windows support)

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, number, method) ⇒ Line

Returns a new instance of Line.



27
28
29
30
31
32
# File 'lib/crash_log/backtrace/line.rb', line 27

def initialize(file, number, method)
  self.file           = file
  self.original_file  = file
  self.number         = number.to_i
  self.method         = method
end

Instance Attribute Details

#fileObject

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



10
11
12
# File 'lib/crash_log/backtrace/line.rb', line 10

def file
  @file
end

#methodObject

The method of the line (such as index)



16
17
18
# File 'lib/crash_log/backtrace/line.rb', line 16

def method
  @method
end

#numberObject

The line number portion of the line



13
14
15
# File 'lib/crash_log/backtrace/line.rb', line 13

def number
  @number
end

#original_fileObject

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



10
11
12
# File 'lib/crash_log/backtrace/line.rb', line 10

def original_file
  @original_file
end

Class Method Details

.parse(unparsed_line) ⇒ Line

Parses a single line of a given backtrace backtrace.

Parameters:

  • unparsed_line (String)

    The raw line from caller or some

Returns:

  • (Line)

    The parsed backtrace line



22
23
24
25
# File 'lib/crash_log/backtrace/line.rb', line 22

def self.parse(unparsed_line)
  _, file, number, method = unparsed_line.match(INPUT_FORMAT).to_a
  new(file, number, method)
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
# File 'lib/crash_log/backtrace/line.rb', line 39

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

#apply_filter(filter) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/crash_log/backtrace/line.rb', line 43

def apply_filter(filter)
  result = filter.call(file)
  if result.nil?
    # Filter returned nil, discard this line
    @_mark_for_deletion = true
  else
    # Filter manipulated parsed file name only
    self.file = result
  end
end

#as_jsonObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/crash_log/backtrace/line.rb', line 84

def as_json
  {}.tap do |hash|
    hash[:number] = number
    hash[:method] = method
    hash[:file] = file

    if context_lines
      hash[:context_line] = context_line
      hash[:pre_context] = pre_context
      hash[:post_context] = post_context
    end
  end
end

#context_lineObject Also known as: line_context



54
55
56
# File 'lib/crash_log/backtrace/line.rb', line 54

def context_line
  Backtrace::LineCache::getline(original_file, number)
end

#context_linesObject



72
73
74
# File 'lib/crash_log/backtrace/line.rb', line 72

def context_lines
  CrashLog.configuration.context_lines
end

#inspectObject



76
77
78
# File 'lib/crash_log/backtrace/line.rb', line 76

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

#marked_for_deletion?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/crash_log/backtrace/line.rb', line 98

def marked_for_deletion?
  @_mark_for_deletion == true
end

#post_contextObject



66
67
68
69
70
# File 'lib/crash_log/backtrace/line.rb', line 66

def post_context
  (number+1..number+context_lines).map {|i|
    Backtrace::LineCache.getline(original_file, i)
  }.select { |line| line }
end

#pre_contextObject



60
61
62
63
64
# File 'lib/crash_log/backtrace/line.rb', line 60

def pre_context
  (number-context_lines..number-1).map {|i|
    Backtrace::LineCache.getline(original_file, i)
  }.select { |line| line }
end

#to_hashObject



80
81
82
# File 'lib/crash_log/backtrace/line.rb', line 80

def to_hash
  as_json
end

#to_sObject

Reconstructs the line in a readable fashion



35
36
37
# File 'lib/crash_log/backtrace/line.rb', line 35

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