unroller 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/unroller.rb +155 -101
  2. metadata +1 -1
data/lib/unroller.rb CHANGED
@@ -49,7 +49,15 @@ class Unroller
49
49
  @@instance = nil
50
50
 
51
51
  def self.trace(options = {}, &block)
52
- @@instance = Unroller.new(options)
52
+ #puts 'called self.trace'
53
+ if @@instance and @@instance.tracing
54
+ # In case they try to turn tracing on when it's already on... Assume it was an accident and don't do anything.
55
+ #puts "@@instance.tracing=#{@@instance.tracing}"
56
+ #return if @@instance and @@instance.tracing
57
+ #yield if block_given?
58
+ else
59
+ @@instance = Unroller.new(options)
60
+ end
53
61
  @@instance.trace &block
54
62
  end
55
63
 
@@ -135,7 +143,10 @@ class Unroller
135
143
  end
136
144
 
137
145
  def self.exclude(*args, &block)
138
- @@instance.exclude(*args, &block)
146
+ @@instance.exclude(*args, &block) unless @@instance.nil?
147
+ end
148
+ def self.suspend(*args, &block)
149
+ @@instance.exclude(*args, &block) unless @@instance.nil?
139
150
  end
140
151
  def exclude(&block)
141
152
  old_tracing = @tracing
@@ -145,14 +156,18 @@ class Unroller
145
156
  end
146
157
 
147
158
  def trace(&block)
148
- (puts 'Already tracing!'; return) if @tracing # This does not work. :fixme:
149
- @tracing = true
159
+ if @tracing
160
+ yield if block_given?
161
+ return
162
+ end
150
163
 
164
+ begin
165
+ @tracing = true
151
166
 
152
- if @condition.call
153
167
 
154
- trap_chain("INT") { set_trace_func(nil) }
168
+ if @condition.call
155
169
 
170
+ trap_chain("INT") { set_trace_func(nil) }
156
171
 
157
172
 
158
173
 
@@ -161,124 +176,124 @@ class Unroller
161
176
 
162
177
 
163
178
 
164
- # (This is the meat of the library right here, so let's set it off with at least 5 blank lines.)
165
- set_trace_func( proc do |event, file, line, id, binding, klass|
166
- begin # begin/rescue block
167
- @event, @file, @line, @id, @binding, @klass =
168
- event, file, line, id, binding, klass
169
179
 
170
- # Sometimes klass is false and id is nil. Not sure why, but that's the way it is.
171
- #printf "- (event=%8s) (klass=%10s) (id=%10s) (%s:%-2d)\n", event, klass, id, file, line #if klass.to_s == 'false'
172
- #puts 'false!!!!!!!'+klass.inspect if klass.to_s == 'false'
180
+ # (This is the meat of the library right here, so let's set it off with at least 5 blank lines.)
181
+ set_trace_func( proc do |event, file, line, id, binding, klass|
182
+ begin # begin/rescue block
183
+ @event, @file, @line, @id, @binding, @klass =
184
+ event, file, line, id, binding, klass
173
185
 
174
- return if ['c-call', 'c-return'].include? event unless include_c_calls
175
- #(puts 'exclude') if @excluding_calls_made_within_unintersting_call unless event == 'return' # Until we hit a return and can break out of this uninteresting call, we don't want to do *anything*.
176
- #return if uninteresting_class?(klass.to_s) unless (klass == false)
186
+ # Sometimes klass is false and id is nil. Not sure why, but that's the way it is.
187
+ #printf "- (event=%8s) (klass=%10s) (id=%10s) (%s:%-2d)\n", event, klass, id, file, line #if klass.to_s == 'false'
188
+ #puts 'false!!!!!!!'+klass.inspect if klass.to_s == 'false'
177
189
 
178
- if too_far?
179
- puts "We've read #{@max_lines} (@max_lines) lines now. Turning off tracing..."
180
- trace_off
181
- return
182
- end
190
+ return if ['c-call', 'c-return'].include? event unless include_c_calls
191
+ #(puts 'exclude') if @excluding_calls_made_within_unintersting_call unless event == 'return' # Until we hit a return and can break out of this uninteresting call, we don't want to do *anything*.
192
+ #return if uninteresting_class?(klass.to_s) unless (klass == false)
183
193
 
184
- case event
194
+ if too_far?
195
+ puts "We've read #{@max_lines} (@max_lines) lines now. Turning off tracing..."
196
+ trace_off
197
+ return
198
+ end
185
199
 
200
+ case event
186
201
 
187
202
 
188
- when 'call'
189
- unless skip_line?
190
- # :todo: use # instead of :: if klass.constantize.instance_methods.include?(id)
191
- column sprintf(' ' + '+'.cyan + ' calling'.cyan + ' ' + '%s'.underline.cyan, fully_qualified_method), @column_widths[0]
192
- newline
193
203
 
194
- column code_for(file, line, '/'.magenta, :green), @column_widths[0]
195
- file_column file, line
196
- newline
204
+ when 'call'
205
+ unless skip_line?
206
+ # :todo: use # instead of :: if klass.constantize.instance_methods.include?(id)
207
+ column sprintf(' ' + '+'.cyan + ' calling'.cyan + ' ' + '%s'.underline.cyan, fully_qualified_method), @column_widths[0]
208
+ newline
197
209
 
198
- @lines_output += 1
210
+ column code_for(file, line, '/'.magenta, :green), @column_widths[0]
211
+ file_column file, line
212
+ newline
199
213
 
200
- @call_stack.push fully_qualified_method
214
+ @lines_output += 1
201
215
 
202
- @depth += 1
203
- #puts "++ Increased depth to #{depth}"
216
+ @call_stack.push fully_qualified_method
204
217
 
205
- # The locals at this point will be simply be the arguments that were passed in to this method.
206
- do_show_locals if show_args
207
- end
208
- @internal_depth += 1
218
+ @depth += 1
219
+ #puts "++ Increased depth to #{depth}"
209
220
 
221
+ # The locals at this point will be simply be the arguments that were passed in to this method.
222
+ do_show_locals if show_args
223
+ end
224
+ @internal_depth += 1
210
225
 
211
- when 'class'
212
- when 'end'
213
- when 'line'
214
- unless skip_line?
215
- # Show the state of the locals *before* executing the current line. (I might add the option to show it after instead/as well, but I don't think that would be easy...)
216
- do_show_locals if show_locals
217
226
 
218
- column code_for(file, line, ' ', :bold), @column_widths[0]
219
- file_column file, line
220
- newline
227
+ when 'class'
228
+ when 'end'
229
+ when 'line'
230
+ unless skip_line?
231
+ # Show the state of the locals *before* executing the current line. (I might add the option to show it after instead/as well, but I don't think that would be easy...)
232
+ do_show_locals if show_locals
221
233
 
222
- @lines_output += 1
223
- end
234
+ column code_for(file, line, ' ', :bold), @column_widths[0]
235
+ file_column file, line
236
+ newline
224
237
 
238
+ @lines_output += 1
239
+ end
225
240
 
226
241
 
227
- when 'return'
228
242
 
229
- @internal_depth -= 1
230
- unless skip_line?
231
- puts "Warning: @depth < 0. You may wish to call trace with a :depth => depth value greater than #{@initial_depth}" if @depth-1 < 0
232
- @depth -= 1 unless @depth == 0
233
- #puts "-- Decreased depth to #{depth}"
234
- returning_from = @call_stack.pop
243
+ when 'return'
235
244
 
236
- code = code_for(file, line, '\\'.magenta, :green, suffix = " (returning from #{returning_from})".green)
237
- code = code_for(file, line, '\\'.magenta + " (returning from #{returning_from})".green, :green) unless code =~ /return|end/
238
- # I've seen some really weird statements listed as "return" statements.
239
- # I'm not really sure *why* it thinks these are returns, but let's at least identify those lines for the user. Examples:
240
- # * must_be_open!
241
- # * @db = db
242
- # * stmt = @statement_factory.new( self, sql )
243
- # I think some of the time this happens it might be because people pass the wrong line number to eval (__LINE__ instead of __LINE__ + 1, for example), so the line number is just not accurate.
244
- # But I don't know if that explains all such cases or not...
245
- column code, @column_widths[0]
246
- file_column file, line
247
- newline
245
+ @internal_depth -= 1
246
+ unless skip_line?
247
+ puts "Warning: @depth < 0. You may wish to call trace with a :depth => depth value greater than #{@initial_depth}" if @depth-1 < 0
248
+ @depth -= 1 unless @depth == 0
249
+ #puts "-- Decreased depth to #{depth}"
250
+ returning_from = @call_stack.pop
248
251
 
249
- @lines_output += 1
250
- end
252
+ code = code_for(file, line, '\\'.magenta, :green, suffix = " (returning from #{returning_from})".green)
253
+ code = code_for(file, line, '\\'.magenta + " (returning from #{returning_from})".green, :green) unless code =~ /return|end/
254
+ # I've seen some really weird statements listed as "return" statements.
255
+ # I'm not really sure *why* it thinks these are returns, but let's at least identify those lines for the user. Examples:
256
+ # * must_be_open!
257
+ # * @db = db
258
+ # * stmt = @statement_factory.new( self, sql )
259
+ # I think some of the time this happens it might be because people pass the wrong line number to eval (__LINE__ instead of __LINE__ + 1, for example), so the line number is just not accurate.
260
+ # But I don't know if that explains all such cases or not...
261
+ column code, @column_widths[0]
262
+ file_column file, line
263
+ newline
251
264
 
252
- # Did we just get out of an uninteresting call?? Are we back in interesting land again??
253
- if @excluding_calls_made_within_unintersting_call and
254
- @excluding_calls_made_within_unintersting_call == @internal_depth
255
- puts "Yay, we're back in interesting land!"
256
- @excluding_calls_made_within_unintersting_call = nil
257
- end
265
+ @lines_output += 1
266
+ end
258
267
 
268
+ # Did we just get out of an uninteresting call?? Are we back in interesting land again??
269
+ if @excluding_calls_made_within_unintersting_call and
270
+ @excluding_calls_made_within_unintersting_call == @internal_depth
271
+ puts "Yay, we're back in interesting land!"
272
+ @excluding_calls_made_within_unintersting_call = nil
273
+ end
259
274
 
260
275
 
261
- when 'raise'
262
- # We probably always want to see these (?)... Never skip displaying them, even if we are "too deep".
263
- column "Raising an error (#{$!}) from #{klass}".red.bold, @column_widths[0]
264
- newline
265
276
 
266
- column code_for(file, line, ' ').red, @column_widths[0]
267
- file_column file, line
268
- newline
277
+ when 'raise'
278
+ # We probably always want to see these (?)... Never skip displaying them, even if we are "too deep".
279
+ column "Raising an error (#{$!}) from #{klass}".red.bold, @column_widths[0]
280
+ newline
269
281
 
270
- else
271
- column sprintf("- (%8s) %10s %10s (%s:%-2d)", event, klass, id, file, line)
272
- newline
273
- end
282
+ column code_for(file, line, ' ').red, @column_widths[0]
283
+ file_column file, line
284
+ newline
274
285
 
286
+ else
287
+ column sprintf("- (%8s) %10s %10s (%s:%-2d)", event, klass, id, file, line)
288
+ newline
289
+ end
275
290
 
276
- rescue Exception => exception
277
- puts exception.inspect
278
- raise
279
- end # begin/rescue block
280
- end) # set_trace_func
281
291
 
292
+ rescue Exception => exception
293
+ puts exception.inspect
294
+ raise
295
+ end # begin/rescue block
296
+ end) # set_trace_func
282
297
 
283
298
 
284
299
 
@@ -286,15 +301,17 @@ class Unroller
286
301
 
287
302
 
288
303
 
289
- end # if @condition.call
290
304
 
291
- if block_given?
292
- yield
293
- end
305
+ end # if @condition.call
294
306
 
295
- ensure
296
- trace_off if block_given?
297
- end
307
+ if block_given?
308
+ yield
309
+ end
310
+
311
+ ensure
312
+ trace_off if block_given?
313
+ end # rescue/ensure block
314
+ end # def trace(&block)
298
315
 
299
316
  def self.trace_off
300
317
  if @@instance and @@instance.tracing
@@ -317,7 +334,7 @@ protected
317
334
  variables = Variables.new(:local, @binding)
318
335
  # puts "In do_show_locals at depth #{depth}; event = #{@event}"
319
336
  if variables.any?
320
- column variables.to_s
337
+ column variables.to_s, nil, :allow, :yellow
321
338
  newline
322
339
  end
323
340
  end
@@ -464,11 +481,33 @@ end
464
481
 
465
482
 
466
483
 
484
+ # _____ _
485
+ # |_ _|__ ___| |_
486
+ # | |/ _ \/ __| __|
487
+ # | | __/\__ \ |_
488
+ # |_|\___||___/\__|
489
+ #
490
+ # :todo: These tests seriously need to be converted into automated tests. But I was lazy/in a hurry when I wrote this...
491
+
467
492
  if $0 == __FILE__
493
+ require 'test/unit'
494
+
495
+ class TheTest < Test::Unit::TestCase
496
+ def test_1
497
+ end
498
+
499
+ end
500
+
501
+
502
+
468
503
  puts '-----------------------------------------------------------'
469
504
  puts 'Can call trace_off even if not tracing'
470
505
  Unroller::trace_off
471
506
 
507
+ puts '-----------------------------------------------------------'
508
+ puts 'Can call exclude even if not tracing'
509
+ Unroller::exclude
510
+
472
511
  puts '-----------------------------------------------------------'
473
512
  puts 'Testing return value (should print 3 in this case)'
474
513
  puts Unroller::trace { 1 + 2 }
@@ -506,6 +545,21 @@ if $0 == __FILE__
506
545
  method3
507
546
  end
508
547
 
548
+ puts '-----------------------------------------------------------'
549
+ puts "Testing that we can try to turn tracing on even if it's already on"
550
+ def method1
551
+ Unroller::trace
552
+ v = 'in method1'
553
+ end
554
+ Unroller::trace do
555
+ Unroller::trace do
556
+ v = 'about to call method1'
557
+ method1
558
+ end
559
+ v = "We've exited out of one trace block, but we're still within a trace block, so this line should be traced."
560
+ end
561
+
562
+
509
563
  puts '-----------------------------------------------------------'
510
564
  puts 'Test with block; very deep (test for over-wide columns)'
511
565
  ('a'..last='y').each do |method_name|
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: unroller
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.10
6
+ version: 0.0.11
7
7
  date: 2007-04-23 00:00:00 -07:00
8
8
  summary: A tool for generating human-readable "execution traces"
9
9
  require_paths: