Module: Log

Defined in:
lib/rbbt/util/log.rb

Constant Summary collapse

DEBUG =
0
LOW =
1
MEDIUM =
2
HIGH =
3
INFO =
4
WARN =
5
ERROR =
6
SEVERITY_COLOR =
[WHITE, GREEN, YELLOW, RED, GREEN, YELLOW, ERROR].collect{|e| "\033[#{e}"}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.logfileObject

Returns the value of attribute logfile.



12
13
14
# File 'lib/rbbt/util/log.rb', line 12

def logfile
  @logfile
end

.severityObject

Returns the value of attribute severity.



12
13
14
# File 'lib/rbbt/util/log.rb', line 12

def severity
  @severity
end

Class Method Details

.debug(message = nil, &block) ⇒ Object



39
40
41
# File 'lib/rbbt/util/log.rb', line 39

def self.debug(message = nil, &block)
  log(message, DEBUG, &block)
end

.error(message = nil, &block) ⇒ Object



63
64
65
# File 'lib/rbbt/util/log.rb', line 63

def self.error(message = nil, &block)
  log(message, ERROR, &block)
end

.high(message = nil, &block) ⇒ Object



51
52
53
# File 'lib/rbbt/util/log.rb', line 51

def self.high(message = nil, &block)
  log(message, HIGH, &block)
end

.info(message = nil, &block) ⇒ Object



55
56
57
# File 'lib/rbbt/util/log.rb', line 55

def self.info(message = nil, &block)
  log(message, INFO, &block)
end

.log(message = nil, severity = MEDIUM, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rbbt/util/log.rb', line 24

def self.log(message = nil, severity = MEDIUM, &block)
  message ||= block
  return if message.nil?
  severity_color = SEVERITY_COLOR[severity]
  font_color = {true => WHITE, false => DARK}[severity >= INFO]

  return if severity < self.severity
  message = message.call if Proc === message
  return if message.nil? or message.empty?

  str = "\033[0;37m#{Time.now.strftime("[%m/%d/%y-%H:%M:%S]")}#{severity_color}[#{severity.to_s}]\033[0m:#{font_color} " <<  message.strip  << "\033[0m"
  STDERR.puts str
  logfile.puts str unless logfile.nil?
end

.low(message = nil, &block) ⇒ Object



43
44
45
# File 'lib/rbbt/util/log.rb', line 43

def self.low(message = nil, &block)
  log(message, LOW, &block)
end

.medium(message = nil, &block) ⇒ Object



47
48
49
# File 'lib/rbbt/util/log.rb', line 47

def self.medium(message = nil, &block)
  log(message, MEDIUM, &block)
end

.warn(message = nil, &block) ⇒ Object



59
60
61
# File 'lib/rbbt/util/log.rb', line 59

def self.warn(message = nil, &block)
  log(message, WARN, &block)
end