Class: Pwrake::Logger

Inherits:
Object
  • Object
show all
Includes:
Severity
Defined in:
lib/pwrake/logger.rb

Defined Under Namespace

Modules: Severity

Constant Summary

Constants included from Severity

Severity::DEBUG, Severity::ERROR, Severity::FATAL, Severity::INFO, Severity::UNKNOWN, Severity::WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



20
21
22
23
24
25
# File 'lib/pwrake/logger.rb', line 20

def initialize
  @level = WARN
  @out = nil
  @filename = nil
  @lock = Mutex.new
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



7
8
9
# File 'lib/pwrake/logger.rb', line 7

def level
  @level
end

Instance Method Details

#add(severity, message) ⇒ Object Also known as: log



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pwrake/logger.rb', line 54

def add(severity, message)
  if !severity || severity >= @level
    if @out
      @lock.synchronize do
        @out.write(message+"\n")
      end
    else
      LOCK.synchronize do
        $stderr.write(message+"\n")
      end
    end
  end
  true
end

#closeObject



90
91
92
93
94
95
96
97
# File 'lib/pwrake/logger.rb', line 90

def close
  finish "LogEnd", @start_time
  @lock.synchronize do
    @out.close if @filename
    @out=nil
  end
  @filename=nil
end

#debug(msg) ⇒ Object



74
75
76
# File 'lib/pwrake/logger.rb', line 74

def debug(msg)
  add(DEBUG, msg)
end

#finish(str, start_time) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/pwrake/logger.rb', line 44

def finish(str, start_time)
  if @out
    finish_time = Time.now
    t1 = Log.fmt_time(start_time)
    t2 = Log.fmt_time(finish_time)
    elap = finish_time - start_time
    info "#{str} : start=#{t1} end=#{t2} elap=#{elap}"
  end
end

#fmt_time(t) ⇒ Object



82
83
84
# File 'lib/pwrake/logger.rb', line 82

def fmt_time(t)
  t.strftime("%Y-%m-%dT%H:%M:%S.%%06d") % t.usec
end

#info(msg) ⇒ Object



70
71
72
# File 'lib/pwrake/logger.rb', line 70

def info(msg)
  add(INFO, msg)
end

#open(file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pwrake/logger.rb', line 27

def open(file)
  close if @out
  case file
  when IO
    @out = file
    @filename = nil
  when String
    @out = File.open(file,"w")
    @filename = file
  else
    raise "file arg must be IO or String"
  end
  @start_time = Time.now
  info "LogStart=" + fmt_time(@start_time)
  info "logfile=#{@filename}" if @filename
end

#timer(prefix, *args) ⇒ Object



86
87
88
# File 'lib/pwrake/logger.rb', line 86

def timer(prefix,*args)
  Timer.new(prefix,*args)
end

#warn(msg) ⇒ Object



78
79
80
# File 'lib/pwrake/logger.rb', line 78

def warn(msg)
  add(WARN, msg)
end