Class: BTC::Diagnostics

Inherits:
Object
  • Object
show all
Defined in:
lib/btcruby/diagnostics.rb

Defined Under Namespace

Classes: Item

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_infoObject

Returns the value of attribute last_info.



10
11
12
# File 'lib/btcruby/diagnostics.rb', line 10

def last_info
  @last_info
end

#last_itemObject

Returns the value of attribute last_item.



11
12
13
# File 'lib/btcruby/diagnostics.rb', line 11

def last_item
  @last_item
end

#last_messageObject

Returns the value of attribute last_message.



9
10
11
# File 'lib/btcruby/diagnostics.rb', line 9

def last_message
  @last_message
end

Class Method Details

.currentObject

Instance unique for this thread.



5
6
7
# File 'lib/btcruby/diagnostics.rb', line 5

def self.current
  Thread.current[:BTCRubyDiagnosticsCurrentInstance] ||= self.new
end

Instance Method Details

#add_message(message, info: nil) ⇒ Object

Adds a diagnostic message. Use it to record warnings and reasons for errors. Do not use when the input is nil - code that have produced that nil could have already recorded a specific message for that.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/btcruby/diagnostics.rb', line 49

def add_message(message, info: nil)
  self.last_message = message
  self.last_info = info
  self.last_item = Item.new(message, info)

  # add to each recording group
  recording_groups.each do |group|
    group << Item.new(message, info)
  end

  @uniq_trace_streams.each do |stream|
    stream.puts message
  end

  return self
end

#record(&block) ⇒ Object

Begins recording of series of messages and returns all recorded events. If there is no record block on any level, messages are not accumulated, but only last_message is updated. Returns a list of all recorded messages.



17
18
19
20
21
22
23
24
25
26
# File 'lib/btcruby/diagnostics.rb', line 17

def record(&block)
  recording_groups << Array.new
  last_group = nil
  begin
    yield
  ensure
    last_group = recording_groups.pop
  end
  last_group
end

#trace(stream = $stderr, &block) ⇒ Object

Prints out every message to a stream. Default stream is $stderr. You can nest these calls with different streams and each of them will receive logged messages.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/btcruby/diagnostics.rb', line 32

def trace(stream = $stderr, &block)
  @trace_streams << stream
  # Use uniq list internally so when nested we don't write the same thing twice.
  @uniq_trace_streams = @trace_streams.uniq
  begin
    yield
  ensure
    @trace_streams.pop
    @uniq_trace_streams = @trace_streams.uniq
  end
  self
end