Module: LoggerModule

Included in:
Logger, LoggerError
Defined in:
lib/logger.rb

Instance Method Summary collapse

Instance Method Details

#initializeObject



31
32
33
34
# File 'lib/logger.rb', line 31

def initialize
  super
  @is_silent = false
end

#save(path) ⇒ Object



72
73
74
# File 'lib/logger.rb', line 72

def save(path)
  File.write(path, strip_color(string))
end

#silence(&block) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/logger.rb', line 56

def silence(&block)
  raise "need a block" unless block
  tmp = self.silent
  self.silent = true
  block.call
  self.silent = tmp
end

#silentObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/logger.rb', line 40

def silent
  if block_given?
    if /^(.+?):(\d+)/ =~ caller.first
      file = $1
      line = $2.to_i
      error_msg = "Did you mean: silence\n"
      str = File.read(file).split("\n")[line-1]
      error_msg += "in #{file}:#{line}\n"
      error_msg += str + "\n"
      error_msg +=  " " * str.index("silent") + "~~~~~~"
      raise error_msg
    end
  end
  @is_silent
end

#silent=(enable) ⇒ Object



36
37
38
# File 'lib/logger.rb', line 36

def silent=(enable)
  @is_silent = !!enable
end

#strip_color(str) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/logger.rb', line 64

def strip_color(str)
  if $disable_color
    str
  else
    str.gsub(/(?:\e\[\d*[a-zA-Z])+/, "")
  end
end

#write_console(str, target) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/logger.rb', line 76

def write_console(str, target)
  unless @is_silent
    if $disable_color
      target.write(str)
    else
      write_color(str, target)
    end
  end
end