Knot::Protocol#debug replaced by #logger, which could be provided by #new(logger:), default Logger.new(STDERR)

This commit is contained in:
Denis Knauf 2022-09-28 12:34:03 +02:00
parent 0282694a28
commit 18c0b6434a

View file

@ -53,7 +53,7 @@ module Knot::Protocol::Type
end end
#class Knot::KnotC #class Knot::KnotC
# attr_accessor :debug, :binary # attr_accessor :binary
# #
# def initialize path = nil, binary: nil # def initialize path = nil, binary: nil
# @path = path # @path = path
@ -141,10 +141,9 @@ module Knot::Protocol::Idx
end end
class Knot::Protocol class Knot::Protocol
attr_reader :sock, :conf, :zones attr_reader :sock, :conf, :zones, :logger
attr_accessor :debug
def initialize path_or_sock = nil def initialize path_or_sock = nil, logger: nil
case path_or_sock case path_or_sock
when String, Pathname when String, Pathname
@sock = UNIXSocket.new path_or_sock.to_s @sock = UNIXSocket.new path_or_sock.to_s
@ -153,7 +152,7 @@ class Knot::Protocol
when nil when nil
@sock = UNIXSocket.new '/run/knot/knot.sock' @sock = UNIXSocket.new '/run/knot/knot.sock'
end end
@debug = false @logger = logger || Logger.new(STDERR)
@conf = Knot::Conf.new self @conf = Knot::Conf.new self
@zones = Hash.new {|h, zone| h[zone] = Knot::Zone.new zone, self } @zones = Hash.new {|h, zone| h[zone] = Knot::Zone.new zone, self }
end end
@ -170,7 +169,10 @@ class Knot::Protocol
end end
sock.write [3].pack( 'c') sock.write [3].pack( 'c')
sock.flush 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.write s
rsock.flush rsock.flush
end end
@ -200,7 +202,7 @@ class Knot::Protocol
def rcv sock: nil def rcv sock: nil
ret, r = [], nil ret, r = [], nil
sock = sock || @sock sock = sock || @sock
sock = RecordIO.new sock if @debug sock = RecordIO.new sock if 0 >= @logger.sev_threshold
loop do loop do
t = sock.unpack1 'c' t = sock.unpack1 'c'
case t case t
@ -217,7 +219,10 @@ class Knot::Protocol
end end
end end
ensure 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 ret
end end