class Airbrake::CodeHunk
@api private
around it
Represents a small hunk of code consisting of a base line and a couple lines
def get(file, line)
-
(Hash{Integer=>String}, nil)
- lines of code around the base line
Parameters:
-
line
(Integer
) -- The base line in the file -
file
(String
) -- The file to read
def get(file, line) return unless File.exist?(file) return unless line lines = get_lines(file, [line - NLINES, 1].max, line + NLINES) || {} return { 1 => '' } if lines.empty? lines end
def get_from_cache(file)
def get_from_cache(file) Airbrake::FileCache[file] ||= File.foreach(file) rescue StandardError => ex logger.error( "#{self.class.name}: can't read code hunk for #{file}: #{ex}", ) nil end
def get_lines(file, start_line, end_line)
def get_lines(file, start_line, end_line) return unless (cached_file = get_from_cache(file)) lines = {} cached_file.with_index(1) do |l, i| next if i < start_line break if i > end_line lines[i] = l[0...MAX_LINE_LEN].rstrip end lines end