Skip to content

Instantly share code, notes, and snippets.

@st0012
Last active July 7, 2022 22:46
Show Gist options
  • Save st0012/02368276bd35519a5a2acd619031a454 to your computer and use it in GitHub Desktop.
Save st0012/02368276bd35519a5a2acd619031a454 to your computer and use it in GitHub Desktop.
Monitor a queue's message inflow/outflow
require 'io/console/size'
# Usage: Replace a Queue.new with DebugQueue.new(Queue.new)
class DebugQueue < SimpleDelegator
def <<(*msg, trace: caller_locations(3, 3))
debug_print("<<", msg, trace)
super(*msg)
end
def pop(trace: caller_locations(3, 3))
super().tap do |msg|
debug_print(">>", msg, trace)
end
end
private
def debug_print(direction, msg, trace)
msg = msg.to_s
msg =
if msg.size > msg_width
msg[0..msg_width] + "..."
else
msg
end
$stdout.puts(
<<~MSG
#{direction} | #{msg}
trace: #{trace.join(",\n" + " " * 9)}
MSG
)
end
def msg_width
# prevent msg from overflow to another line
@msg_width ||= (IO.console_size[1] - 10)
end
end
@st0012
Copy link
Author

st0012 commented Jul 7, 2022

Some sample output I from ruby/debug's queues

<< | [true]
  trace: /Users/st0012/projects/debug/lib/debug/session.rb:210:in `block in activate'
>> | true
  trace: /Users/st0012/projects/debug/lib/debug/session.rb:214:in `activate',
         /Users/st0012/projects/debug/lib/debug/session.rb:2079:in `block in singleton class',
         /Users/st0012/projects/debug/lib/debug/session.rb:2012:in `start'
<< | [[#<DBG:TC 1:waiting@/Users/st0012/projects/debug/lib/debug/session.rb:166:in `block in initialize'>, [], :load, nil, <RubyVM::InstructionSequence:<main>@target.rb:0>, nil]]
  trace: /Users/st0012/projects/debug/lib/debug/thread_client.rb:215:in `event!',
         /Users/st0012/projects/debug/lib/debug/thread_client.rb:226:in `wait_reply',
         /Users/st0012/projects/debug/lib/debug/thread_client.rb:231:in `on_load'
>> | [#<DBG:TC 1:waiting@/Users/st0012/projects/debug/lib/debug/session.rb:166:in `block in initialize'>, [], :load, nil, <RubyVM::InstructionSequence:<main>@target.rb:0>, nil]
  trace: /Users/st0012/projects/debug/lib/debug/session.rb:236:in `pop_event',
         /Users/st0012/projects/debug/lib/debug/session.rb:240:in `session_server_main',
         /Users/st0012/projects/debug/lib/debug/session.rb:211:in `block in activate'
<< | [[#<DBG:TC 1:waiting@target.rb:1:in `<main>'>, ["[1, 10] in target.rb\n", "=>   1| class Foo\n", "     2|   def first_call\n", "     3|     second_call(20)\n", "     4|   end\n", "     5| \n", "     6|   def second_call(num)\n", "     7|     third_call_with_block do |ten|\n", "     8|       forth_call(num, ten)\n", "     9|     en...
  trace: /Users/st0012/projects/debug/lib/debug/thread_client.rb:215:in `event!',
         /Users/st0012/projects/debug/lib/debug/thread_client.rb:300:in `suspend',
         /Users/st0012/projects/debug/lib/debug/thread_client.rb:243:in `on_breakpoint'
>> | [#<DBG:TC 1:waiting@target.rb:1:in `<main>'>, ["[1, 10] in target.rb\n", "=>   1| class Foo\n", "     2|   def first_call\n", "     3|     second_call(20)\n", "     4|   end\n", "     5| \n", "     6|   def second_call(num)\n", "     7|     third_call_with_block do |ten|\n", "     8|       forth_call(num, ten)\n", "     9|     end...
  trace: /Users/st0012/projects/debug/lib/debug/session.rb:236:in `pop_event',
         /Users/st0012/projects/debug/lib/debug/session.rb:240:in `session_server_main',
         /Users/st0012/projects/debug/lib/debug/session.rb:211:in `block in activate'

@st0012
Copy link
Author

st0012 commented Jul 7, 2022

Perhaps it could also use a name attribute when there're multiple queues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment