Class: Balotelli::Core::IrcLogger

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

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

Returns a new instance of IrcLogger.



13
14
15
16
17
18
19
20
21
# File 'lib/balotelli/core/irc_logger.rb', line 13

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.



11
12
13
# File 'lib/balotelli/core/irc_logger.rb', line 11

def channels
  @channels
end

#channels_logsObject (readonly)

Returns the value of attribute channels_logs.



11
12
13
# File 'lib/balotelli/core/irc_logger.rb', line 11

def channels_logs
  @channels_logs
end

#dirObject (readonly)

Returns the value of attribute dir.



11
12
13
# File 'lib/balotelli/core/irc_logger.rb', line 11

def dir
  @dir
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



11
12
13
# File 'lib/balotelli/core/irc_logger.rb', line 11

def log_file
  @log_file
end

Instance Method Details

#channel_log(channel, to_log) ⇒ Object



60
61
62
63
64
# File 'lib/balotelli/core/irc_logger.rb', line 60

def channel_log(channel, to_log)
  if (logger = @channels_logs[channel])
    logger.info to_log
  end
end

#included(des) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/balotelli/core/irc_logger.rb', line 23

def included(des)
  super

  define_log_method

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

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

    def sputs(str)
      orig_sputs(str)
      to_log = ">>#{str.inspect}\n"
      @log_file.info(to_log)
      if str =~ /\A:?\S+ (#\S+) .*/
        @logger.channel_log($1.downcase, to_log)
      end
      str
    end

    alias_method :orig_sgets, :sgets
    private :orig_sgets

    def sgets
      str = orig_sgets
      to_log = "<<#{str.inspect}\n"
      @log_file.info(to_log)
      if str =~ /\A:?\S+ \S+ (#\S+) .*/
        @logger.channel_log($1.downcase, to_log)
      end
      str
    end
  end
end