Module: ImLost
- Defined in:
- lib/im-lost.rb,
lib/im-lost/version.rb
Overview
If you have overlooked something again and don’t really understand what your code is doing. If you have to maintain this application but can’t really find your way around and certainly can’t track down that stupid error. If you feel lost in all that code, here’s the gem to help you out!
ImLost helps you by analyzing function calls of objects, informing you about exceptions and logging your way through your code. In short, ImLost is your debugging helper!
Defined Under Namespace
Classes: TimerStore
Constant Summary collapse
- VERSION =
The version number of the gem.
'1.2.4'
Class Attribute Summary collapse
-
.caller_locations ⇒ Boolean
Enables/disables to include code location into traced call information.
-
.output ⇒ #<<
The output device used to write information.
-
.timer ⇒ TimerStore
readonly
The timer store used to estimate the runtime of your code.
-
.trace_calls ⇒ Boolean
Enables/disables tracing of method calls.
-
.trace_results ⇒ Boolean
Enables/disables tracing of returned values of method calls.
Class Method Summary collapse
-
.here(test = true) ⇒ Object
Print the call location conditionally.
-
.time(title = nil, &block) ⇒ Object
Measure runtime of given block.
-
.trace(*args, &block) ⇒ Object
Trace objects.
-
.trace_exceptions(with_locations: true) ⇒ Object
Traces exceptions raised within a given block.
-
.traced?(obj) ⇒ Boolean
Test if a given object is currently traced.
-
.untrace(*args) ⇒ Array<Object>?
Stop tracing objects.
-
.untrace_all! ⇒ self
Stop tracing any object.
-
.vars(object) ⇒ Object
Inspect internal variables of a given object.
Class Attribute Details
.caller_locations ⇒ Boolean
Enables/disables to include code location into traced call information. This is enabled by default.
21 22 23 |
# File 'lib/im-lost.rb', line 21 def caller_locations @caller_locations end |
.output ⇒ #<<
The output device used to write information. This should be an ‘IO` device or any other object responding to `#<<` like a Logger.
‘STDERR` is configured by default.
51 52 53 |
# File 'lib/im-lost.rb', line 51 def output @output end |
.timer ⇒ TimerStore (readonly)
67 68 69 |
# File 'lib/im-lost.rb', line 67 def timer @timer end |
.trace_calls ⇒ Boolean
Enables/disables tracing of method calls. This is enabled by default.
76 |
# File 'lib/im-lost.rb', line 76 def trace_calls = @trace_calls.enabled? |
.trace_results ⇒ Boolean
Enables/disables tracing of returned values of method calls. This is enabled by default.
130 |
# File 'lib/im-lost.rb', line 130 def trace_results = @trace_results.enabled? |
Class Method Details
.here ⇒ true .here(test) ⇒ Object .here { ... } ⇒ Object
Print the call location conditionally.
166 167 168 169 170 171 |
# File 'lib/im-lost.rb', line 166 def here(test = true) return test if !test || (block_given? && !(test = yield)) loc = Kernel.caller_locations(1, 1)[0] @output << "* #{loc.path}:#{loc.lineno}\n" test end |
.time(title = nil, &block) ⇒ Object
Measure runtime of given block.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/im-lost.rb', line 329 def time(title = nil, &block) title &&= "#{title} " if @caller_locations loc = Kernel.caller_locations(1, 1)[0] suffix = " #{loc.path}:#{loc.lineno}\n" end unless block_given? @output << "t #{title}#{TimerStore.now}\n#{suffix}" return end t = TimerStore.now begin yield ensure t = (TimerStore.now - t).round(4) @output << "R #{title}#{t} sec.\n#{suffix} #{block.inspect}\n" end end |
.trace(*args) ⇒ Array<Object> .trace(*args) {|args| ... } ⇒ Object
Trace objects.
The given arguments can be any object instance or module or class.
215 216 217 218 219 |
# File 'lib/im-lost.rb', line 215 def trace(*args, &block) return block&.call if args.empty? return args.size == 1 ? _trace(args[0]) : _trace_all(args) unless block args.size == 1 ? _trace_b(args[0], &block) : _trace_all_b(args, &block) end |
.trace_exceptions(with_locations: true) ⇒ Object
Traces exceptions raised within a given block.
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/im-lost.rb', line 109 def trace_exceptions(with_locations: true) return unless block_given? begin we = @trace_exceptions.enabled? el = @exception_locations @exception_locations = with_locations @trace_exceptions.enable unless we yield ensure @trace_exceptions.disable unless we @exception_locations = el end end |
.traced?(obj) ⇒ Boolean
Test if a given object is currently traced.
227 |
# File 'lib/im-lost.rb', line 227 def traced?(obj) = @trace.key?(obj) |
.untrace(*args) ⇒ Array<Object>?
Stop tracing objects.
244 245 246 247 |
# File 'lib/im-lost.rb', line 244 def untrace(*args) args = args.filter_map { @trace.delete(_1) } args.size < 2 ? args[0] : args end |
.untrace_all! ⇒ self
Stop tracing any object. When you are really lost and just like to stop tracing of all your objects.
257 258 259 260 |
# File 'lib/im-lost.rb', line 257 def untrace_all! @trace = {}.compare_by_identity self end |
.vars(object) ⇒ Object
The dedicated handling of ‘Fiber` is platform dependent!
Inspect internal variables of a given object.
When the given object is
-
a ‘Binding` it prints the local variables of the binding
-
a ‘Thread` it prints the fiber-local and thread variables
-
the current ‘Fiber` it prints the fibers’ storage
Be aware that only the current fiber can be inspected.
When the given object can not be inspected it prints an error message.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/im-lost.rb', line 308 def vars(object) out = LineStore.new traced = @trace.delete(object) return _local_vars(out, object) if Binding === object out.location(Kernel.caller_locations(1, 1)[0]) return _thread_vars(out, object) if Thread === object return _fiber_vars(out, object) if @fiber_support && Fiber === object return _instance_vars(out, object) if defined?(object.instance_variables) out << ' !!! unable to retrieve vars' object ensure @trace[traced] = traced if traced @output << out end |