Class: Zeusd::Log::Tailer

Inherits:
Object
  • Object
show all
Defined in:
lib/zeusd/log/tailer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Tailer

Returns a new instance of Tailer.



6
7
8
9
# File 'lib/zeusd/log/tailer.rb', line 6

def initialize(file)
  @file  = file
  @lines = []
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/zeusd/log/tailer.rb', line 4

def file
  @file
end

#linesObject (readonly)

Returns the value of attribute lines.



4
5
6
# File 'lib/zeusd/log/tailer.rb', line 4

def lines
  @lines
end

Instance Method Details

#following?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/zeusd/log/tailer.rb', line 16

def following?
  !!@thread
end

#on_update(&block) ⇒ Object



11
12
13
14
# File 'lib/zeusd/log/tailer.rb', line 11

def on_update(&block)
  @on_update = block if block_given?
  @on_update
end

#restart!Object



20
21
22
# File 'lib/zeusd/log/tailer.rb', line 20

def restart!
  stop!.start!
end

#start!Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zeusd/log/tailer.rb', line 24

def start!
  @thread = Thread.new do
    File.open(file) do |log|
      log.extend(File::Tail)
      log.interval = 0.1
      log.backward(0)
      log.tail do |line|
        @lines << line
        on_update.call(line) if on_update
      end
    end
  end
  self
end

#stop!Object



39
40
41
42
43
44
45
# File 'lib/zeusd/log/tailer.rb', line 39

def stop!
  if @thread
    @thread.terminate
    @thread = nil
  end
  self
end