Module: Tag
- Defined in:
- lib/dbg_tags.rb
Overview
Defined Under Namespace
Classes: GlobalState
Constant Summary collapse
- TAG_FEATURE_ALL =
:all overrides the minimum level on ALL tags in the system
:all- TAG_FEATURE_GENERIC =
:generic is the default feature, if left unspecified
:generic- NONE =
lowest level. No output
0- ERR =
level 1. Only error situation should use this
1- LOG =
level 2. Only very important information
2- TRC =
level 3. Mostly used for method entries
3- VAL =
level 4. Mostly used dumping of attribute values
4- DTL =
level 5. Mostly used for exhaustive dumping of attribute values and minor details
5- TAG_DEFAULT_LEVEL =
the default is :trc
TRC- TAG_MAPPING =
nil and :nil will both map to NONE
{ none: NONE, err: ERR, log: LOG, trc: TRC, val: VAL, dtl: DTL, nil: NONE, nil => NONE }
Class Attribute Summary collapse
-
.no_thread_local_state ⇒ Bool?
(also: no_thread_local_state?)
readonly
True if we should store global state inside the Tag class itself.
Class Method Summary collapse
-
.disable_thread_local_state ⇒ Object
(also: disable_fiber_local_state)
shortcut for no_thread_local_state = true By default thread local state is enabled and this means that ‘enabling’ of tags in some thread does effect in others.
- .dtl(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
- .enable ⇒ Object
-
.enable_thread_local_state ⇒ Object
(also: enable_fiber_local_state)
shortcut for no_thread_local_state := false For rspec use mostly.
-
.enabled ⇒ {Symbol=>0..5}
(also: state)
Keys are the features.
-
.enabled?(feature) ⇒ bool
Reflects explicit enable calls only.
- .err(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
-
.global_state ⇒ GlobalState
Either thread local data (default) or truly global.
-
.inside? ⇒ Boolean
inside? -> true if we are currently executing a block in err/log/trc/val or dtl.
-
.level(feature) ⇒ 0..5
2023-08-14 no longer returns nil.
- .log(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
- .restore_state ⇒ Object
-
.stream ⇒ IO
By default this is STDERR.
-
.stream=(val) ⇒ Object
Override the output stream.
-
.trc(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
The call is silently ignored if called from inside another Tag block (likely a sign of a stack overflow in process).
-
.use_thread_local_state? ⇒ Bool
(also: use_fiber_local_state?)
in each thread of the application.
- .val(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
Class Attribute Details
.no_thread_local_state ⇒ Bool? Also known as: no_thread_local_state?
Returns True if we should store global state inside the Tag class itself.
201 202 203 |
# File 'lib/dbg_tags.rb', line 201 def no_thread_local_state @no_thread_local_state end |
Class Method Details
.disable_thread_local_state ⇒ Object Also known as: disable_fiber_local_state
shortcut for no_thread_local_state = true By default thread local state is enabled and this means that ‘enabling’ of tags in some thread does effect in others. So: when using threads leave this enabled, as it will cause race conditions otherwise. When not using threads, it is more efficient to disable it.
314 |
# File 'lib/dbg_tags.rb', line 314 def disable_thread_local_state; self.no_thread_local_state = true end |
.dtl(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/dbg_tags.rb', line 250 TAG_MAPPING.each do |meth, lev| next if lev == NONE define_method meth do |feature = TAG_FEATURE_GENERIC, msg = '', &msg_block| state = global_state msg, feature = feature, TAG_FEATURE_GENERIC if String === feature #STDERR.puts "DEBUGTAG #{meth}. feature=#{feature}, level=#{Tag.level(feature)}, lev=#{lev}" return if state.inside? || (state.level(feature) || Tag::NONE) < lev # either msg OR msg_block must be set if msg_block prev_inside = state.inside? begin state.inside = true msg = msg_block.call ensure state.inside = prev_inside end end if msg c = caller[0] #STDERR.puts "DEBUGTAG c[#{idx}] = #{c}, caller[1]=#{caller[1]}, caller[2]=#{caller[2]}" label = c[/\w+\.rb:\d+:in `[^']+'/]&.sub(/in `([^']+)'/, '\1:') || c[/\w+\.rb:\d+:/] || c[/[^:\/]+:\d+:/] || c state.stream.print "#{label} #{msg}\n" end end # Tag.err, Tag.log, Tag.trc, Tag.val, Tag.dtl end |
.enable ⇒ Object
283 |
# File 'lib/dbg_tags.rb', line 283 def enable(...); global_state.enable(...); end |
.enable_thread_local_state ⇒ Object Also known as: enable_fiber_local_state
shortcut for no_thread_local_state := false For rspec use mostly.
321 |
# File 'lib/dbg_tags.rb', line 321 def enable_thread_local_state; self.no_thread_local_state = false end |
.enabled ⇒ {Symbol=>0..5} Also known as: state
Returns Keys are the features.
289 |
# File 'lib/dbg_tags.rb', line 289 def enabled; global_state.enabled; end |
.enabled?(feature) ⇒ bool
Returns Reflects explicit enable calls only. The :all feature is IGNORED.
306 |
# File 'lib/dbg_tags.rb', line 306 def enabled? feature; (global_state.enabled[feature] || NONE) > NONE; end |
.err(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
|
|
# File 'lib/dbg_tags.rb', line 238
|
.global_state ⇒ GlobalState
Returns Either thread local data (default) or truly global.
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/dbg_tags.rb', line 215 def global_state if @no_thread_local_state @global_state ||= GlobalState.new else ct = Thread.current unless state = ct.thread_variable_get(:dbg_tags_global_state) state = GlobalState.new ct.thread_variable_set :dbg_tags_global_state, state end state end end |
.inside? ⇒ Boolean
inside? -> true if we are currently executing a block in err/log/trc/val or dtl.
280 |
# File 'lib/dbg_tags.rb', line 280 def inside?; global_state.inside?; end |
.level(feature) ⇒ 0..5
2023-08-14 no longer returns nil
302 |
# File 'lib/dbg_tags.rb', line 302 def level feature; global_state.level feature; end |
.log(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
|
|
# File 'lib/dbg_tags.rb', line 241
|
.restore_state ⇒ Object
286 |
# File 'lib/dbg_tags.rb', line 286 def restore_state(...); global_state.restore_state(...); end |
.stream ⇒ IO
Returns By default this is STDERR.
293 |
# File 'lib/dbg_tags.rb', line 293 def stream; global_state.stream; end |
.stream=(val) ⇒ Object
Override the output stream.
297 |
# File 'lib/dbg_tags.rb', line 297 def stream= val; global_state.stream = val; end |
.trc(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
feature and msg can be switched
The call is silently ignored if called from inside another Tag block (likely a sign of a stack overflow in process). The call is silently ignored if the current tag level is too low. For trc it should be trc or val or dtl to actually print something.
|
|
# File 'lib/dbg_tags.rb', line 228
|
.use_thread_local_state? ⇒ Bool Also known as: use_fiber_local_state?
in each thread of the application.
209 |
# File 'lib/dbg_tags.rb', line 209 def use_thread_local_state?; !no_thread_local_state? end |
.val(feature = TAG_FEATURE_GENERIC, msg = '', &block) ⇒ Object
|
|
# File 'lib/dbg_tags.rb', line 244
|