Module: NLHue::Log

Included in:
NLHue, NLHue, Bridge, Bridge, Disco, Disco, RequestQueue, RequestQueue
Defined in:
lib/nlhue.rb

Constant Summary collapse

@@log_block =
nil
@@log_e_block =
nil
@@bench_block =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.on_bench(&block) ⇒ Object

Pass a block to be called



23
24
25
# File 'lib/nlhue.rb', line 23

def self.on_bench(&block)
  @@bench_block = block
end

.on_log(&block) ⇒ Object

Pass a block that accepts a message to log, or pass nil to restore default logging.



12
13
14
# File 'lib/nlhue.rb', line 12

def self.on_log(&block)
  @@log_block = block
end

.on_log_e(&block) ⇒ Object

Pass a block that accepts an exception and an optional message to log, or pass nil to restore default exception logging.



18
19
20
# File 'lib/nlhue.rb', line 18

def self.on_log_e(&block)
  @@log_e_block = block
end

Instance Method Details

#bench(label, *args, &block) ⇒ Object

Dummy benchmarking method that may be overridden by library users.



28
29
30
31
32
33
34
# File 'lib/nlhue.rb', line 28

def bench(label, *args, &block)
  if @@bench_block
    @@bench_block.call(label, *args, &block)
  else
    yield
  end
end

#log(msg) ⇒ Object

Logging method that may be overridden by library users.



37
38
39
40
41
42
43
# File 'lib/nlhue.rb', line 37

def log(msg)
  if @@log_block
    @@log_block.call(msg)
  else
    puts msg
  end
end

#log_e(e, msg = nil) ⇒ Object

Exception logging method that may be overridden by library users using the on_log_e method.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nlhue.rb', line 47

def log_e(e, msg=nil)
  if @@log_e_block
    @@log_e_block.call(e, msg)
  else
    e ||= StandardError.new('No exception given to log')
    if msg
      log "#{msg}: #{e}", e.backtrace
    else
      log e, e.backtrace
    end
  end
end