class Google::Logging::SourceLocation

An object representing a source location as used by Google Logging.

Attributes

file[R]

@return [String] Path to the source file.

function[R]

@return [String] Name of the calling function.

line[R]

@return [String] Line number as a string.

Public Class Methods

for_caller(locations: nil, extra_depth: 0, omit_files: nil) click to toggle source

Returns a SourceLocation corresponding to the caller. This basically walks the stack trace backwards until it finds the first entry not in the ‘google/logging/message.rb` source file or in any of the other files optionally listed.

@param locations [Array<Thread::Backtrace::Location>] The caller stack

to search. Optional; defaults to the current stack.

@param extra_depth [Integer] Optional extra steps backwards to walk.

Defaults to 0.

@param omit_files [Array<String,Regexp>] File paths to omit. @return [SourceLocation,nil] The SourceLocation, or nil if none found.

# File lib/google/logging/source_location.rb, line 37
def self.for_caller locations: nil, extra_depth: 0, omit_files: nil
  in_file = true
  omit_files = [__FILE__] + Array(omit_files)
  (locations || self.caller_locations).each do |loc|
    if in_file
      next if omit_files.any? { |pat| pat === loc.absolute_path }
      in_file = false
    end
    extra_depth -= 1
    next unless extra_depth.negative?
    return new file: loc.path, line: loc.lineno.to_s, function: loc.base_label
  end
  nil
end
new(file:, line:, function: @file = file.to_s) click to toggle source

Low-level constructor.

@param file [String] Path to the source file. @param line [String] Line number as a string. @param function [String] Name of the calling function.

# File lib/google/logging/source_location.rb, line 59
def initialize file:, line:, function:
  @file = file.to_s
  @line = line.to_s
  @function = function.to_s
end

Public Instance Methods

==(other) click to toggle source

@private

# File lib/google/logging/source_location.rb, line 84
def == other
  return false unless other.is_a? SourceLocation
  file == other.file && line == other.line && function == other.function
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source

@private

# File lib/google/logging/source_location.rb, line 91
def hash
  [file, line, function].hash
end
to_h() click to toggle source

@private

# File lib/google/logging/source_location.rb, line 75
def to_h
  {
    file: @file,
    line: @line,
    function: @function
  }
end