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
88
89
90
91
92
# 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
  result = options[:ansicolor_strip] ? strip_color(buffer) : buffer
  if $stdout.capturing && !options[:quiet]
    # 多段キャプチャ中かつ quite: false の場合は外側にも伝播する
    $stdout.string << result
  end
  result
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



128
129
130
# File 'lib/logger.rb', line 128

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



102
103
104
# File 'lib/logger.rb', line 102

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



94
95
96
97
98
99
100
# File 'lib/logger.rb', line 94

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

#warn(str) ⇒ Object



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

def warn(str)
  self.puts str
end

#write_base(str, stream) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/logger.rb', line 116

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



106
107
108
109
110
111
112
113
114
# File 'lib/logger.rb', line 106

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