diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 3ba7b2b..784c81c 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -53,7 +53,7 @@ module Knot::Protocol::Type end #class Knot::KnotC -# attr_accessor :debug, :binary +# attr_accessor :binary # # def initialize path = nil, binary: nil # @path = path @@ -141,10 +141,9 @@ module Knot::Protocol::Idx end class Knot::Protocol - attr_reader :sock, :conf, :zones - attr_accessor :debug + attr_reader :sock, :conf, :zones, :logger - def initialize path_or_sock = nil + def initialize path_or_sock = nil, logger: nil case path_or_sock when String, Pathname @sock = UNIXSocket.new path_or_sock.to_s @@ -153,7 +152,7 @@ class Knot::Protocol when nil @sock = UNIXSocket.new '/run/knot/knot.sock' end - @debug = false + @logger = logger || Logger.new(STDERR) @conf = Knot::Conf.new self @zones = Hash.new {|h, zone| h[zone] = Knot::Zone.new zone, self } end @@ -170,7 +169,10 @@ class Knot::Protocol end sock.write [3].pack( 'c') sock.flush - STDERR.puts( {send: data, _: s}.inspect) if @debug + if 0 >= @logger.sev_threshold + @logger.debug "send data #{data.inspect}" + @logger.debug "send raw #{data.inspect}" + end rsock.write s rsock.flush end @@ -200,7 +202,7 @@ class Knot::Protocol def rcv sock: nil ret, r = [], nil sock = sock || @sock - sock = RecordIO.new sock if @debug + sock = RecordIO.new sock if 0 >= @logger.sev_threshold loop do t = sock.unpack1 'c' case t @@ -217,7 +219,10 @@ class Knot::Protocol end end ensure - STDERR.puts( {rcvd: ret, _: sock.str}.inspect) if @debug + if RecordIO === sock + @logger.debug "rcvd raw #{sock.str.inspect}" + @logger.debug "rcvd data #{ret.inspect}" + end ret end