Class: Google::Logging::SourceLocation
- Inherits:
-
Object
- Object
- Google::Logging::SourceLocation
- Defined in:
- lib/google/logging/source_location.rb
Overview
An object representing a source location as used by Google Logging.
Instance Attribute Summary collapse
-
#file ⇒ String
readonly
Path to the source file.
-
#function ⇒ String
readonly
Name of the calling function.
-
#line ⇒ String
readonly
Line number as a string.
Class Method Summary collapse
-
.for_caller(locations: nil, extra_depth: 0, omit_files: nil) ⇒ SourceLocation?
Returns a SourceLocation corresponding to the caller.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(file:, line:, function:) ⇒ SourceLocation
constructor
Low-level constructor.
- #to_h ⇒ Object
Constructor Details
#initialize(file:, line:, function:) ⇒ SourceLocation
Low-level constructor.
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
#file ⇒ String (readonly)
Returns Path to the source file.
66 67 68 |
# File 'lib/google/logging/source_location.rb', line 66 def file @file end |
#function ⇒ String (readonly)
Returns Name of the calling function.
72 73 74 |
# File 'lib/google/logging/source_location.rb', line 72 def function @function end |
#line ⇒ String (readonly)
Returns 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.
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 |
#hash ⇒ Object
91 92 93 |
# File 'lib/google/logging/source_location.rb', line 91 def hash [file, line, function].hash end |
#to_h ⇒ Object
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 |