class Balotelli::Core::IrcLogger

Constants

LOG_FORMAT

Attributes

channels[R]
channels_logs[R]
dir[R]
log_file[R]

Public Class Methods

new(dir = '', log_name = 'logs.log', channels = []) click to toggle source
# File lib/balotelli/core/irc_logger.rb, line 35
def initialize(dir = '', log_name = 'logs.log', channels = [])
  @dir = dir
  check_dir
  @log_file = new_logger(log_name)
  @channels = Set.new channels
  @channels_logs = @channels.each_with_object({}) do |c, o|
    o[c] = new_logger(channel_log_file(c))
  end
end

Public Instance Methods

channel_log(channel, to_log) click to toggle source
# File lib/balotelli/core/irc_logger.rb, line 63
def channel_log(channel, to_log)
  if (logger = @channels_logs[channel])
    logger.info to_log
  end
end
define_log_method() click to toggle source
# File lib/balotelli/core/irc_logger.rb, line 69
def define_log_method
  define_method :log_file do |str|
    class_variable_get(:@log_file)
  end

  define_method :log do |str|
    log_string = "LOG: #{str.inspect}\n"
    log_file.info log_string
    puts log_string
  end
end
included(des) click to toggle source
Calls superclass method
# File lib/balotelli/core/irc_logger.rb, line 45
def included(des)
  super

  des.instance_variable_set(:@log_file, log_file)
  des.instance_variable_set(:@logger, self)

  define_log_method

  class << des
    alias_method :orig_sputs, :sputs
    private :orig_sputs

    alias_method :orig_sgets, :sgets
    private :orig_sgets
  end
  des.extend(Methods)
end

Private Instance Methods

channel_log_file(channel) click to toggle source
# File lib/balotelli/core/irc_logger.rb, line 83
def channel_log_file(channel)
  "#{channel.tr('#', '')}_logs.log"
end
check_dir() click to toggle source
# File lib/balotelli/core/irc_logger.rb, line 93
def check_dir
  FileUtils.mkdir_p(dir) unless File.file?(dir)
end
new_logger(file_name) click to toggle source
# File lib/balotelli/core/irc_logger.rb, line 87
def new_logger(file_name)
  Logger.new(File.join(dir, file_name), 0, 1.0 / 0).tap do |l|
    l.formatter = LOG_FORMAT
  end
end