Module: Narou::LoggerModule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#capturingObject

Returns the value of attribute capturing.



21
22
23
# File 'lib/logger.rb', line 21

def capturing
  @capturing
end

Instance Method Details

#capture(options = {}, &block) ⇒ Object

標準出力($stdout)のバッファリング+取得

キャプチャー用途なので標準エラーはキャプチャーしない

quiet

標準出力に出力をしないかどうか

ansicolor_strip

エスケープシーケンスを除去するか



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/logger.rb', line 70

def capture(options = {}, &block)
  options = {
    quiet: true, ansicolor_strip: true
  }.merge(options)
  raise "#capture block given" unless block
  temp_stream = $stdout
  $stdout = (self == $stdout ? copy_instance : self)
  $stdout.capturing = true
  if options[:quiet]
    $stdout.silence { block.call }
  else
    block.call
  end
  $stdout.capturing = false
  buffer = $stdout.string
  $stdout = temp_stream
  options[:ansicolor_strip] ? strip_color(buffer) : buffer
end

#copy_instanceObject



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

def copy_instance
  self.class.new.tap { |obj|
    obj.silent = silent
  }
end

#error(str) ⇒ Object



123
124
125
# File 'lib/logger.rb', line 123

def error(str)
  self.puts "<bold><red>[ERROR]</red></bold> ".termcolor + str
end

#initializeObject



23
24
25
26
27
# File 'lib/logger.rb', line 23

def initialize
  super
  @is_silent = false
  @capturing = false
end

#save(path) ⇒ Object



97
98
99
# File 'lib/logger.rb', line 97

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

#silence(&block) ⇒ Object



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

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

#silentObject



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

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



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

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

#strip_color(str) ⇒ Object



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

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

#warn(str) ⇒ Object



119
120
121
# File 'lib/logger.rb', line 119

def warn(str)
  self.puts str
end

#write_base(str, stream) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/logger.rb', line 111

def write_base(str, stream)
  str = str.to_s
  if str.encoding == Encoding::ASCII_8BIT
    str.force_encoding(Encoding::UTF_8)
  end
  write_console(str, stream)
end

#write_console(str, target) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/logger.rb', line 101

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