Class: DohLogger::SocketAcceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/doh/logger/socket_acceptor.rb

Overview

this class accepts log events, and posts them to a socket this is typically used in conjunction with socket_viewer.rb to do remote debugging

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = '127.0.0.1', port = SocketAcceptor::default_port) ⇒ SocketAcceptor

Returns a new instance of SocketAcceptor.



12
13
14
15
16
# File 'lib/doh/logger/socket_acceptor.rb', line 12

def initialize(host = '127.0.0.1', port = SocketAcceptor::default_port)
  @host = host
  @port = port
  @last_connect_attempt = nil
end

Class Method Details

.default_portObject



18
19
20
# File 'lib/doh/logger/socket_acceptor.rb', line 18

def self.default_port
  2377
end

Instance Method Details

#connect_socket(how_often = five_seconds) ⇒ Object

don’t attempt to connect more than once every 5 seconds



27
28
29
30
31
32
33
# File 'lib/doh/logger/socket_acceptor.rb', line 27

def connect_socket(how_often = five_seconds)
  if @sock || @last_connect_attempt && @last_connect_attempt >= (DateTime.now - how_often)
    return
  end
  @last_connect_attempt = DateTime.now
  @sock ||= TCPSocket.new(@host, @port)
end

#five_secondsObject



22
23
24
# File 'lib/doh/logger/socket_acceptor.rb', line 22

def five_seconds
  5.0/86440
end

#log(event) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/doh/logger/socket_acceptor.rb', line 35

def log(event)
  begin
    connect_socket
    return if !@sock
    event.pid = Process.pid
    if event.exception
      newevent = event.dup
      newevent.exception = Exception.new("Exception(#{event.exception.class}): " + event.exception.to_s)
      newevent.exception.set_backtrace(event.exception.backtrace)
      event = newevent
    end
    @sock.write_object(Marshal::dump(event))
  rescue Errno::ECONNREFUSED => e
    #ignore silently?
  end
end