Class: Balotelli::Core::IrcLogger

Inherits:
Module
  • Object
show all
Defined in:
lib/balotelli/core/irc_logger.rb

Defined Under Namespace

Modules: Methods

Constant Summary collapse

LOG_FORMAT =
proc do |_severity, datetime, _progname, msg|
  "#{datetime.utc}: #{msg}\n"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir = '', log_name = 'logs.log', channels = []) ⇒ IrcLogger



35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



33
34
35
# File 'lib/balotelli/core/irc_logger.rb', line 33

def channels
  @channels
end

#channels_logsObject (readonly)

Returns the value of attribute channels_logs.



33
34
35
# File 'lib/balotelli/core/irc_logger.rb', line 33

def channels_logs
  @channels_logs
end

#dirObject (readonly)

Returns the value of attribute dir.



33
34
35
# File 'lib/balotelli/core/irc_logger.rb', line 33

def dir
  @dir
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



33
34
35
# File 'lib/balotelli/core/irc_logger.rb', line 33

def log_file
  @log_file
end

Instance Method Details

#channel_log(channel, to_log) ⇒ Object



63
64
65
66
67
# 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_methodObject



69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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