Class: Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/riel/timer.rb

Instance Method Summary collapse

Constructor Details

#initialize(what, args = Hash.new) ⇒ Timer

Returns a new instance of Timer.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/riel/timer.rb', line 7

def initialize what, args = Hash.new
  if args.kind_of? Fixnum
    args = { :level => args }
  end

  @io    = args[:io]    || $stdout
  @level = args[:level] || Logue::Log::DEBUG
  
  stmsg  = args.include?(:startmsg) ? args[:startmsg] : "#{what} start time"
  endmsg = args.include?(:endmsg)   ? args[:endmsg]   : "#{what} end   time"
  elmsg  = args.include?(:elmsg)    ? args[:elmsg]    : "#{what} elapsed   "
  
  sttime = Time.new
  logmsg stmsg,  sttime
  
  yield
  
  endtime = Time.new
  
  logmsg stmsg,  sttime
  logmsg endmsg, endtime
  logmsg elmsg,  endtime - sttime
end

Instance Method Details

#logmsg(msg, value) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/riel/timer.rb', line 31

def logmsg msg, value
  if msg
    if @io
      @io.puts "#{msg}: #{value}"
    else
      Logue::Logue::Log.log "#{msg}: #{value}", @level
    end
  end
end