Class: LogBench::Log::File

Inherits:
Object
  • Object
show all
Defined in:
lib/log_bench/log/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



8
9
10
11
12
# File 'lib/log_bench/log/file.rb', line 8

def initialize(path)
  self.path = find_log_file(path)
  self.last_position = 0
  validate!
end

Instance Attribute Details

#last_positionObject

Returns the value of attribute last_position.



6
7
8
# File 'lib/log_bench/log/file.rb', line 6

def last_position
  @last_position
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/log_bench/log/file.rb', line 6

def path
  @path
end

Instance Method Details

#collectionObject



22
23
24
# File 'lib/log_bench/log/file.rb', line 22

def collection
  @collection ||= Collection.new(lines)
end

#entriesObject



18
19
20
# File 'lib/log_bench/log/file.rb', line 18

def entries
  collection.entries
end

#exist?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/log_bench/log/file.rb', line 60

def exist?
  ::File.exist?(path)
end

#linesObject



26
27
28
# File 'lib/log_bench/log/file.rb', line 26

def lines
  @lines ||= read_lines
end

#mtimeObject



64
65
66
# File 'lib/log_bench/log/file.rb', line 64

def mtime
  ::File.mtime(path)
end

#reload!Object



30
31
32
33
34
# File 'lib/log_bench/log/file.rb', line 30

def reload!
  self.lines = nil
  self.collection = nil
  self.last_position = 0
end

#requestsObject



14
15
16
# File 'lib/log_bench/log/file.rb', line 14

def requests
  collection.requests
end

#sizeObject



56
57
58
# File 'lib/log_bench/log/file.rb', line 56

def size
  ::File.size(path)
end

#tail(max_lines = 1000) ⇒ Object



36
37
38
39
40
# File 'lib/log_bench/log/file.rb', line 36

def tail(max_lines = 1000)
  all_lines = read_lines
  recent_lines = all_lines.last(max_lines)
  Collection.new(recent_lines)
end

#watch(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/log_bench/log/file.rb', line 42

def watch(&block)
  return enum_for(:watch) unless block_given?

  loop do
    new_lines = read_new_lines
    next if new_lines.empty?

    new_collection = Collection.new(new_lines)
    yield new_collection unless new_collection.empty?

    sleep 0.5
  end
end