gtkdebug

Ruby code posted
created at 03 Aug 02:50

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
begin
  require 'gtksourceview'
rescue LoadError
else
  module Kernel
    alias __raise raise
    def raise(*args)
      __raise(*args)
    rescue Exception => exc
      exc.backtrace.shift
      exc.backtrace.each do |line|
        puts "\tfrom #{line}"
      end
      unless [ SystemExit, Interrupt ].include? exc.class
        Gtk::DebugDialog.new(exc)
      end
    end
  end
end

module Gtk
  class DebugDialog < MessageDialog
    def initialize(exc)
      super(nil, 0, ERROR, BUTTONS_CLOSE,
        "#{exc.class}: #{exc.message}")
      set_title(exc.class.to_s)
      set_resizable(true)
      set_secondary_text _("Congratulations, you found a bug in #{$APP}!")
      set_default_response(RESPONSE_CLOSE)
      signal_connect('response') { destroy }

      expander = Expander.new _("<b>Show _backtrace</b>"), true
      expander.use_markup = true
      expander.border_width = 6
      expander.spacing = 0
      vbox.pack_start(expander, true)
      vbox.spacing = 0

      sw = ScrolledWindow.new
      sw.set_size_request(500, 300)
      expander << sw

      source = SourceView.new
      source.editable = false
      source.cursor_visible = false
      sw << source

      @buffer = source.buffer
      @buffer.language = SourceLanguagesManager.new.get_language('application/x-ruby')
      @buffer.highlight = true
      @buffer.create_tag('bold',
        'weight' => Pango::FontDescription::WEIGHT_BOLD)
      @buffer.create_tag('monospace',
        'family' => 'monospace')
      @buffer.create_tag('highlight',
        'background' => '#eee')
      @buffer.create_tag('black',
        'foreground' => 'black')
      @buffer.create_tag('hidden',
        'foreground' => 'white')
      iter = @buffer.start_iter

      @buffer.insert(iter, "#{exc.class}: #{exc.message}\n", 'black', 'bold')

      exc.backtrace.each do |line|
        file, pos, message = line.split(':', 3)
        @buffer.insert(iter, 'from ', 'black')
        @buffer.insert(iter, file, 'black', 'bold')
        @buffer.insert(iter, ":\n  line ", 'black')
        @buffer.insert(iter, "#{pos}", 'black', 'bold')
        @buffer.insert(iter, " #{message}\n", 'black')

        if File.exists?(file)
          lines = File.readlines(file)
          pos = pos.to_i - 1
          next if pos.zero?
          [0, pos - 2].max.step(pos + 2) do |i|
            if pos == i
              @buffer.insert(iter, '%4d' % (i + 1),
                'monospace', 'bold', 'highlight')
              @buffer.insert(iter, "  #{lines[i]}",
                'monospace', 'highlight')
            else
              @buffer.insert(iter, '%4d' % (i + 1),
                'monospace', 'highlight')
              @buffer.insert(iter, "  #{lines[i]}",
                'monospace')
            end
          end
          @buffer.insert(iter, "\n")
        end
      end

      show_all
      run
    end
  end
end
2.9 KB in 13 ms with coderay