Class: IRC::Client::Modules::Logger

Inherits:
IRC::Client::Module show all
Defined in:
lib/failirc/client/modules/Logger.rb

Constant Summary collapse

@@version =
'0.0.1'

Instance Attribute Summary

Attributes inherited from IRC::Client::Module

#client

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Logger

Returns a new instance of Logger.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/failirc/client/modules/Logger.rb', line 39

def initialize (server)
    @events = {
        :pre  => Event::Callback.new(self.method(:dispatch), -9001),
        :post => Event::Callback.new(self.method(:dispatch), -9001),

        :custom => {
            :log  => self.method(:log),
        },
    }

    super(server)
end

Class Method Details

.versionObject



31
32
33
# File 'lib/failirc/client/modules/Logger.rb', line 31

def self.version
    return @@version
end

Instance Method Details

#descriptionObject



35
36
37
# File 'lib/failirc/client/modules/Logger.rb', line 35

def description
    "Logger-#{Logger.version}"
end

#dispatch(event, thing, string) ⇒ Object



72
73
74
75
76
77
# File 'lib/failirc/client/modules/Logger.rb', line 72

def dispatch (event, thing, string)
    if (event.chain == :input && event.special == :pre) || (event.chain == :output && event.special == :post)
        @log.puts "[#{Time.now}] #{(event.chain == :input) ? '<' : '>'} #{string.inspect}"
        @log.flush
    end
end

#finalizeObject



66
67
68
69
70
# File 'lib/failirc/client/modules/Logger.rb', line 66

def finalize
    if @log != $stdout
        @log.close
    end
end

#log(string) ⇒ Object



79
80
81
82
# File 'lib/failirc/client/modules/Logger.rb', line 79

def log (string)
    @log.puts "[#{Time.now}] #{string}"
    @log.flush
end

#rehashObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/failirc/client/modules/Logger.rb', line 52

def rehash
    if @log && @log != $stdout
        @log.close
    end

    file = client.config.elements['config/modules/module[@name="Logger"]/file']

    if file
        @log = File.open(file.text)
    else
        @log = $stdout
    end
end