Module: Narou::LoggerModule
- Included in:
- Logger, LoggerError, StreamingLogger
- Defined in:
- lib/logger.rb
Instance Method Summary collapse
-
#capture(ansicolor_strip = true, &block) ⇒ Object
標準出力($stdout)のバッファリング+取得.
- #initialize ⇒ Object
- #save(path) ⇒ Object
- #silence(&block) ⇒ Object
- #silent ⇒ Object
- #silent=(enable) ⇒ Object
- #strip_color(str) ⇒ Object
- #write_console(str, target) ⇒ Object
Instance Method Details
#capture(ansicolor_strip = true, &block) ⇒ Object
標準出力($stdout)のバッファリング+取得
キャプチャー用途なので標準エラーはキャプチャーしない
- ansicolor_strip
-
エスケープシーケンスを除去するか
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/logger.rb', line 60 def capture(ansicolor_strip = true, &block) raise "#capture block given" unless block temp_stream = $stdout $stdout = self.class.new $stdout.silence do block.call end buffer = $stdout.string $stdout = temp_stream ansicolor_strip ? strip_color(buffer) : buffer end |
#initialize ⇒ Object
21 22 23 24 |
# File 'lib/logger.rb', line 21 def initialize super @is_silent = false end |
#save(path) ⇒ Object
80 81 82 |
# File 'lib/logger.rb', line 80 def save(path) File.write(path, strip_color(string)) end |
#silence(&block) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/logger.rb', line 46 def silence(&block) raise "need a block" unless block tmp = self.silent self.silent = true block.call self.silent = tmp end |
#silent ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/logger.rb', line 30 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
26 27 28 |
# File 'lib/logger.rb', line 26 def silent=(enable) @is_silent = !!enable end |
#strip_color(str) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/logger.rb', line 72 def strip_color(str) if $disable_color str else str.gsub(/(?:\e\[\d*[a-zA-Z])+/, "") end end |
#write_console(str, target) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/logger.rb', line 84 def write_console(str, target) unless @is_silent if $disable_color target.write(str) else write_color(str, target) end end end |