Class: Google::Logging::SourceLocation

Inherits:
Object
  • Object
show all
Defined in:
lib/google/logging/source_location.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:, line:, function:) ⇒ SourceLocation

Low-level constructor.

Parameters:

  • file (String)

    Path to the source file.

  • line (String)

    Line number as a string.

  • function (String)

    Name of the calling function.



59
60
61
62
63
# 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

Instance Attribute Details

#fileString (readonly)

Returns Path to the source file.

Returns:

  • (String)

    Path to the source file.



66
67
68
# File 'lib/google/logging/source_location.rb', line 66

def file
  @file
end

#functionString (readonly)

Returns Name of the calling function.

Returns:

  • (String)

    Name of the calling function.



72
73
74
# File 'lib/google/logging/source_location.rb', line 72

def function
  @function
end

#lineString (readonly)

Returns Line number as a string.

Returns:

  • (String)

    Line number as a string.



69
70
71
# File 'lib/google/logging/source_location.rb', line 69

def line
  @line
end

Class Method Details

.for_caller(locations: nil, extra_depth: 0, omit_files: nil) ⇒ SourceLocation?

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.

Parameters:

  • locations (Array<Thread::Backtrace::Location>) (defaults to: nil)

    The caller stack to search. Optional; defaults to the current stack.

  • extra_depth (Integer) (defaults to: 0)

    Optional extra steps backwards to walk. Defaults to 0.

  • omit_files (Array<String,Regexp>) (defaults to: nil)

    File paths to omit.

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



84
85
86
87
# 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

#hashObject



91
92
93
# File 'lib/google/logging/source_location.rb', line 91

def hash
  [file, line, function].hash
end

#to_hObject



75
76
77
78
79
80
81
# File 'lib/google/logging/source_location.rb', line 75

def to_h
  {
    file: @file,
    line: @line,
    function: @function
  }
end