Sha256: dae659ece9678a544d520fb8fae737f8dcf23966f48cd43629a66444a7d8f999

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

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

require "singleton"
require "stringio"
require_relative "color"

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)
    str.gsub(/(?:\e\[\d*[a-zA-Z])+/, "")
  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)
    end
  end
end

class Logger < StringIO
  include Singleton
  include LoggerModule
  
  def self.get
    instance
  end

  def write(str)
    str = str.to_s
    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
    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

2 entries across 2 versions & 1 rubygems

Version Path
narou-1.1.2.1 lib/logger.rb
narou-1.1.2 lib/logger.rb