lib/kamerling/logging.rb in kamerling-0.0.2 vs lib/kamerling/logging.rb in kamerling-0.0.3

- old
+ new

@@ -1,32 +1,52 @@ require 'after_do' require 'logger' +require_relative 'net_dispatcher' +require_relative 'server/sock' -module Kamerling module Logging - module_function +module Kamerling + class Logging + def self.log_to(logger = Logger.new($stdout)) + new logger + end - def log_to logger = Logger.new($stdout) - log_dispatcher_to logger - log_server_to logger - end + def initialize(logger) + @logger = logger + log_dispatcher + log_server + end - class << self + attr_reader :logger + private :logger + private - def log_dispatcher_to logger + def log_dispatcher NetDispatcher.singleton_class.extend AfterDo - NetDispatcher.singleton_class.before :dispatch do |addr, bytes| - logger.debug "sent #{addr} #{bytes}" + NetDispatcher.singleton_class.before(:dispatch) do |addr, message| + logger.debug "sent #{addr} #{message.to_hex}" end end - def log_server_to logger + 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}" } - Server::Sock.before :handle do |input, client_addr| - logger.info "connect #{client_addr}" - logger.debug "received #{client_addr} #{input}" - end end end -end end +end