Sha256: 4180b3644daafb753e6842f4c575bf763dc9f5420efb24f8eb0546dd596cc305

Contents?: true

Size: 1.59 KB

Versions: 45

Compression:

Stored size: 1.59 KB

Contents

# -*- coding: utf-8 -*-
#
# Copyright 2013 whiteleaf. All rights reserved.
#

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

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

  def silent
    @is_silent
  end

  def strip_color(str)
    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)
    unless @is_silent
      if $disable_color
        target.write(str)
      else
        write_color(str, target)
      end
    end
  end
end

class Logger < StringIO
  include Singleton
  include LoggerModule
  
  def self.get
    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

class LoggerError < StringIO
  include Singleton
  include LoggerModule
  
  def self.get
    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

def error(str)
  warn "<red>[ERROR]</red> #{str}".termcolor
end

$stdout = Logger.get
$stderr = LoggerError.get

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
narou-1.5.7.1 lib/logger.rb
narou-1.5.7 lib/logger.rb
narou-1.5.6.1 lib/logger.rb
narou-1.5.6 lib/logger.rb
narou-1.5.5.1 lib/logger.rb
narou-1.5.5 lib/logger.rb
narou-1.5.4 lib/logger.rb
narou-1.5.3 lib/logger.rb
narou-1.5.2.1 lib/logger.rb
narou-1.5.2 lib/logger.rb
narou-1.5.1 lib/logger.rb
narou-1.5.0.2 lib/logger.rb
narou-1.5.0.1 lib/logger.rb
narou-1.5.0 lib/logger.rb
narou-1.4.6 lib/logger.rb
narou-1.4.5 lib/logger.rb
narou-1.4.4 lib/logger.rb
narou-1.4.3 lib/logger.rb
narou-1.4.2.rc2 lib/logger.rb
narou-1.4.2.rc1 lib/logger.rb