Skip to content

Instantly share code, notes, and snippets.

@wndxlori
Created August 7, 2019 15:12
Show Gist options
  • Save wndxlori/49c83ea3c922914c1c96aa4d0ff06f1c to your computer and use it in GitHub Desktop.
Save wndxlori/49c83ea3c922914c1c96aa4d0ff06f1c to your computer and use it in GitHub Desktop.
Stack tracing for DragonRuby
module Spy
def self.on! instance
all_methods = instance.class.instance_methods false
instance.class.class_eval do
attr_accessor :__spy_call_depth__ unless instance.class.instance_methods.include?(:__spy_call_depth__)
all_methods.reject { |m| m.start_with? "__spy_" }.each do |m|
original_method_name = m
new_method_name = "__spy_original_#{m}__".to_sym
alias_method new_method_name, m
puts "Spying on! #{m}"
define_method(m) do |*args|
instance.__spy_call_depth__ ||= 0
tab_width = " " * (instance.__spy_call_depth__ * 8)
parameters = "#{args}"[1..-2]
instance.__spy_call_depth__ += 1
print "\n #{tab_width}#{m}(#{parameters})"
execution_time = Time.new.to_i
result = send(new_method_name, *args)
completion_time = Time.new.to_i
instance.__spy_call_depth__ -= 1
instance.__spy_call_depth__ = instance.__spy_call_depth__.greater 0
delta = (completion_time - execution_time)
if delta > 0
print "\n#{delta}#{tab_width} success: #{m}"
else
print "\n0#{tab_width} success: #{m}"
end
puts "" if instance.__spy_call_depth__ == 0
result
rescue Exception => e
instance.__spy_call_depth__ -= 1
instance.__spy_call_depth__ = instance.__spy_call_depth__.greater 0
print "\n #{tab_width} failed: #{m}"
puts "" if instance.__spy_call_depth__ == 0
raise e
end
end
end
end
end
def test_game args, assert
game = Game.new
Spy.on!(game)
game._defaults args
game._init args
game._update args
end
$gtk.suppress_framerate_warning = true
$gtk.tests.start
@wndxlori
Copy link
Author

wndxlori commented Aug 7, 2019

This was posted in LOWREZ DragonRiders Discord on Aug 1, 2019 by @amirrajan

@amirrajan
Copy link

This is now part of DragonRuby GTK proper. Source code is here: https://github.com/DragonRuby/Game-Toolkit-Symbol-Lookup/blob/master/trace.rb

@wndxlori
Copy link
Author

wndxlori commented Aug 8, 2019

Cool. I was just making note for the newsletter, as you can't link to posts in Discord (D'Oh!)

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