Skip to content

Commit c8e12dc

Browse files
committed
🔍 Improve debug connection trace output
This uses String#dump to print out control characters legibly, whic makes the CRLF line endings explicit and enables reliable copy/pasting of logged data for tests or debugging. This also improves how sent/rcvd data is interleaved (in my opinion). Note that, because client-sent data is generally in the main/application thread(s) (inside `#synchronize`), and server responses are received in the server thread (_not_ synchronized), this did have a threading race condition. So this updated version uses `synchronize` to log the server responses.
1 parent 36ecc26 commit c8e12dc

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

lib/net/imap.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ def initialize(host, port: nil, ssl: nil, response_handlers: nil,
11021102

11031103
# Basic Client State
11041104
@utf8_strings = false
1105-
@debug_output_bol = true
1105+
@debug_output_line = nil
11061106
@exception = nil
11071107
@greeting = nil
11081108
@capabilities = nil
@@ -3556,7 +3556,7 @@ def get_tagged_response(tag, cmd, timeout = nil)
35563556
def get_response
35573557
buff = @reader.read_response_buffer
35583558
return nil if buff.length == 0
3559-
$stderr.print(buff.gsub(/^/n, "S: ")) if config.debug?
3559+
synchronize do print_debug_trace("S: ", buff) end if config.debug?
35603560
@parser.parse(buff)
35613561
end
35623562

@@ -3633,15 +3633,20 @@ def generate_tag
36333633

36343634
def put_string(str)
36353635
@sock.print(str)
3636-
if config.debug?
3637-
if @debug_output_bol
3638-
$stderr.print("C: ")
3636+
print_debug_trace("C: ", str) if config.debug?
3637+
end
3638+
3639+
def print_debug_trace(prefix, str)
3640+
str.each_line do |line|
3641+
if @debug_output_line.nil? then $stderr.print prefix
3642+
elsif @debug_output_line != prefix then $stderr.print "\n", prefix
36393643
end
3640-
$stderr.print(str.gsub(/\n/n) { $'.empty? ? $& : "\nC: " })
3641-
if /\n\z/n.match(str)
3642-
@debug_output_bol = true
3644+
if /\n\z/n.match?(line)
3645+
$stderr.puts line.dump.delete_prefix('"').delete_suffix('"')
3646+
@debug_output_line = nil
36433647
else
3644-
@debug_output_bol = false
3648+
$stderr.print line.dump.delete_prefix('"').delete_suffix('"')
3649+
@debug_output_line = prefix
36453650
end
36463651
end
36473652
end

0 commit comments

Comments
 (0)