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.3'
Class Attribute Summary collapse
-
.caller_locations ⇒ Boolean
Enables/disables to include code location into traced call information.
-
.output ⇒ #puts
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.
-
.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 ⇒ #puts
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)
Returns the timer store used to estimate the runtime of your code.
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.
129 |
# File 'lib/im-lost.rb', line 129 def trace_results = @trace_results.enabled? |
Class Method Details
.here ⇒ true .here(test) ⇒ Object .here { ... } ⇒ Object
Print the call location conditionally.
165 166 167 168 169 170 |
# File 'lib/im-lost.rb', line 165 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 |
.trace(*args) ⇒ Array<Object> .trace(*args) {|args| ... } ⇒ Object
Trace objects.
The given arguments can be any object instance or module or class.
214 215 216 217 218 |
# File 'lib/im-lost.rb', line 214 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.
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/im-lost.rb', line 108 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.
226 |
# File 'lib/im-lost.rb', line 226 def traced?(obj) = @trace.key?(obj) |
.untrace(*args) ⇒ Array<Object>?
Stop tracing objects.
243 244 245 246 |
# File 'lib/im-lost.rb', line 243 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.
256 257 258 259 |
# File 'lib/im-lost.rb', line 256 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.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/im-lost.rb', line 307 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 |