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

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