Class: MogilefsdLogTailer::TailHandler

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/mogilefsd_log_tailer/tail_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(hostname, filename) ⇒ TailHandler

Returns a new instance of TailHandler.



5
6
7
8
9
10
11
12
13
14
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 5

def initialize(hostname, filename)
  @hostname = hostname
  @received_data = ''

  if filename.nil? || filename.empty?
    @file = $stdout
  else
    @file = File.open(filename, 'wb')
  end
end

Instance Method Details

#post_initObject



16
17
18
19
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 16

def post_init
  @received_data = ''
  send_data("!watch\r\n")
end

#print_log_entry(line) ⇒ Object



40
41
42
43
44
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 40

def print_log_entry(line)
  msg = "#{Time.now.to_s}: #{@hostname}: #{line}"
  @file.puts(msg)
  @file.flush
end

#receive_data(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 21

def receive_data(data)
  # puts(" - Received #{data.inspect} from #{@hostname}")
  if data =~ /\r\n/
    lines = data.split("\r\n", -1)
    lines.each_with_index do |line, i|
      if i == 0
        print_log_entry(@received_data + line)
        @received_data = ''
      elsif i == lines.size - 1
        @received_data << line
      else
        print_log_entry(line)
      end
    end
  else
    @received_data << data
  end
end