Class: HotPixiv::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/hotpixiv/utils.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, debug_on) ⇒ Log

Returns a new instance of Log.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hotpixiv/utils.rb', line 48

def initialize(path, debug_on)
  @debug = {}
  @debug_on = debug_on
  if Utils.directory? path
    level = [0, 1, 2, 3, 4].index(LOG_LEVEL) || Logger::DEBUG
    @log = Logger.new(logfile(path))
    @log.level = level
  else
    unless path.nil?
      @debug["error"] = []
      @debug["error"] << "Invalid log directory path - #{path}"
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



92
93
94
95
96
# File 'lib/hotpixiv/utils.rb', line 92

def method_missing(name, *args)
  attr = name.to_s
  write(attr, args)
  debug(attr)
end

Instance Method Details

#debug(name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hotpixiv/utils.rb', line 79

def debug(name)
  # デバッグメッセージを標準出力
  if name == 'print'
    @debug[@name].each do |msg|
      puts msg
    end if @debug[@name].instance_of?(Array) && @debug_on
  else
     # ログメソッドを保存
    @name = name
    self
  end
end

#logfile(dir) ⇒ Object



63
64
65
# File 'lib/hotpixiv/utils.rb', line 63

def logfile(dir)
  Pathname.new(dir + "/" + "#{Time.now.strftime("%Y%m%d")}.log").cleanpath
end

#write(name, *args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hotpixiv/utils.rb', line 67

def write(name, *args)
  # ログ出力処理、デバッグスタックに保存
  if name =~ /=$/
    name.chop!
    @debug[name] = [] unless @debug[name].instance_of?(Array)
    args[0].each do |msg|
      @log.send(name, msg.encode(Utils.os_encoding)) unless @log.nil?
      @debug[name] << msg
    end
  end
end