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

master
Denis Knauf 2022-09-28 12:34:03 +02:00
parent 0282694a28
commit 18c0b6434a
1 changed files with 13 additions and 8 deletions

View File

@ -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