Sha256: 568af1b4c94ab16652b10d00aff31c677b0aface4b22614a68503ab25eec08fe

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'after_do'
require 'logger'
require_relative 'net_dispatcher'
require_relative 'server/sock'

module Kamerling
  class Logging
    def self.log_to(logger = Logger.new($stdout))
      new logger
    end

    def initialize(logger)
      @logger = logger
      log_dispatcher
      log_server
    end

    attr_reader :logger
    private     :logger

    private

    def log_dispatcher
      NetDispatcher.singleton_class.extend AfterDo
      NetDispatcher.singleton_class.before(:dispatch) do |addr, message|
        logger.debug "sent #{addr} #{message.to_hex}"
      end
    end

    def log_server
      Server::Sock.extend AfterDo
      log_server_lifecycle
      log_server_communication
    end

    def log_server_communication
      Server::Sock.before(:handle) do |input, client_addr|
        begin
          logger.info "connect #{client_addr}"
          logger.debug "received #{client_addr} #{Message.parse(input).to_hex}"
        rescue Message::UnknownType
          logger.debug "received #{client_addr} unknown message type"
        end
      end
    end

    def log_server_lifecycle
      Server::Sock.before(:start) { |srv| logger.info "start #{srv.addr}" }
      Server::Sock.after(:stop)   { |srv| logger.info "stop #{srv.addr}"  }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kamerling-0.0.3 lib/kamerling/logging.rb