Class: SysLogger::IO

Inherits:
Object
  • Object
show all
Defined in:
lib/syslogger/io.rb

Instance Method Summary collapse

Constructor Details

#initialize(&file_creator) ⇒ IO

Returns a new instance of IO.



3
4
5
6
7
# File 'lib/syslogger/io.rb', line 3

def initialize(&file_creator)
  @file_creator = file_creator
  @file = nil
  @connect_pid = Process.pid
end

Instance Method Details

#closeObject



49
50
51
52
53
54
# File 'lib/syslogger/io.rb', line 49

def close
  if !@file.nil?
    @file.close
    @file = nil
  end
end

#fileObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/syslogger/io.rb', line 9

def file
  # re-connect on fork
  if Process.pid != @connect_pid
    @file.close
  end
  if @file.nil? || @file.closed?
    @file = @file_creator.call
  end
  @file
end

#flushObject



43
44
45
46
47
# File 'lib/syslogger/io.rb', line 43

def flush
  if !@file.nil?
    @file.flush
  end
end

#transmit(message) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/syslogger/io.rb', line 32

def transmit(message)
  # UNIXSocket overwrites Object#send, so using file.respond_to?(:send)
  # does not work. If send and __send__ have different signigutres call
  # send. Otherwise, call write.
  unless file.method(:send).hash == file.method(:__send__).hash
    file.send(message, 0)
  else
    file.write(message)
  end
end

#write(message) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/syslogger/io.rb', line 20

def write(message)
  tries = 2
  Array(message).each do |msg|
    begin
      transmit(msg)
    rescue
      file.close
      retry unless (tries -= 1).zero?
    end
  end
end