lib/logger.rb in narou-1.1.2.1 vs lib/logger.rb in narou-1.2.0

- old
+ new

@@ -5,10 +5,18 @@ require "singleton" require "stringio" require_relative "color" +if $disable_color + class String + def termcolor + self.gsub(/<\/?.+?>/, "").gsub("&lt;", "<").gsub("&gt;", ">") + end + end +end + module LoggerModule def initialize super @is_silent = false end @@ -20,24 +28,28 @@ def silent @is_silent end def strip_color(str) - str.gsub(/(?:\e\[\d*[a-zA-Z])+/, "") + if $disable_color + str + else + str.gsub(/(?:\e\[\d*[a-zA-Z])+/, "") + end end def save(path) File.write(path, string) end def write_console(str, target) - if str.encoding == Encoding::ASCII_8BIT - str.force_encoding("utf-8") - end unless @is_silent - str = strip_color(str) if $disable_color - write_color(str, target) + if $disable_color + target.write(str) + else + write_color(str, target) + end end end end class Logger < StringIO @@ -48,10 +60,13 @@ instance end def write(str) str = str.to_s + if str.encoding == Encoding::ASCII_8BIT + str.force_encoding(Encoding::UTF_8) + end super(strip_color(str)) write_console(str, STDOUT) end end @@ -63,9 +78,12 @@ instance end def write(str) str = str.to_s + if str.encoding == Encoding::ASCII_8BIT + str.force_encoding(Encoding::UTF_8) + end super(strip_color(str)) write_console(str, STDERR) end end