Class: Pazuzu::Utility::OutputTailer

Inherits:
Object
  • Object
show all
Defined in:
lib/pazuzu/utility/output_tailer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ OutputTailer

Returns a new instance of OutputTailer.



6
7
8
9
10
11
12
# File 'lib/pazuzu/utility/output_tailer.rb', line 6

def initialize(options = {})
  @limit = options[:limit] || 1000
  @callback = options[:on_line] || proc { }
  @mutex = Mutex.new
  @buffer = ''
  @entries = []
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



45
46
47
# File 'lib/pazuzu/utility/output_tailer.rb', line 45

def entries
  @entries
end

#limitObject (readonly)

Returns the value of attribute limit.



46
47
48
# File 'lib/pazuzu/utility/output_tailer.rb', line 46

def limit
  @limit
end

Instance Method Details

#closeObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/pazuzu/utility/output_tailer.rb', line 21

def close
  if @stream
    @stream.close rescue nil
    @stream = nil
  end
  if @thread
    @thread.terminate
    @thread = nil
  end
end

#openObject



14
15
16
17
18
19
# File 'lib/pazuzu/utility/output_tailer.rb', line 14

def open
  close
  @stream, writeable_stream = IO.pipe
  @thread = Thread.start { tail! }
  return writeable_stream
end

#tail!Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pazuzu/utility/output_tailer.rb', line 32

def tail!
  while @stream
    data = @stream.readpartial(4096)
    @mutex.synchronize do
      @buffer << data
      while @buffer =~ /\A([^\r\n]*)\r?\n(.*)/
        @buffer = $2
        add_line!($1)
      end
    end
  end
end