diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-20 03:30:09 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-20 03:30:09 -0700 |
commit | 7e7aa3278a9e478b02eb152575fb00ee0802ac81 (patch) | |
tree | 4f7a6c1fafeae67500d3bfba5321ea5e2e571f18 | |
parent | ed09327eac899b9d5a1f782616747ca13c4806b1 (diff) | |
download | dinobot-7e7aa3278a9e478b02eb152575fb00ee0802ac81.tar.gz dinobot-7e7aa3278a9e478b02eb152575fb00ee0802ac81.tar.xz |
Add singleton logger class.
-rw-r--r-- | dinobot.rb | 44 | ||||
-rw-r--r-- | irc.rb | 40 | ||||
-rw-r--r-- | logger.rb | 48 |
3 files changed, 71 insertions, 61 deletions
@@ -1,6 +1,7 @@ require 'timeout' require_relative 'irc' +require_relative 'logger' module Dinobot class Bot @@ -16,6 +17,8 @@ module Dinobot @trigger = '!' @irc = Dinobot::IRC.new(@server, @port, @nick, @pass) + @logger = Dinobot::Logger.instance + @modules = Hash.new @channels = Array.new @@ -34,7 +37,7 @@ module Dinobot end @irc.disconnect - log :info, 'Disconnected.' + @logger.info 'Disconnected.' end def say(channel, message) @@ -55,7 +58,7 @@ module Dinobot def load_module(mod) mod = mod.downcase.intern - log :info, "Loading module: #{mod}" + @logger.info "Loading module: #{mod}" begin load "#{mod}.rb" @@ -63,15 +66,15 @@ module Dinobot m = Dinobot.const_get(Dinobot.constants.find { |x| x.downcase == mod }) @modules[mod] = m.new(self) - log :info, "Loaded module: #{mod} (#{m})" + @logger.info "Loaded module: #{mod} (#{m})" rescue LoadError, StandardError => e - log :error, "Failed to load module: #{mod} (#{e})" + @logger.error "Failed to load module: #{mod} (#{e})" end end def unload_module(mod) mod = mod.downcase.intern - log :info, "Unloading module: #{mod}" + @logger.info "Unloading module: #{mod}" begin raise 'module not loaded' unless @modules.has_key?(mod) @@ -80,31 +83,10 @@ module Dinobot m = Dinobot.send(:remove_const, Dinobot.constants.find { |x| x.downcase == mod }) - log :info, "Unloaded module: #{mod} (#{m})" + @logger.info "Unloaded module: #{mod} (#{m})" rescue => e - log :error, "Failed to unload module: #{mod} (#{e})" - end - end - - def log(type, *lines) - str = lines.join("\n") - - case type - when :in - prefix = "\e[32m<<\e[0m " - when :out - prefix = "\e[36m>>\e[0m " - when :error - prefix = "\e[31m!!\e[0m " - when :info - prefix = "\e[33m==\e[0m " - when :indent - prefix = ' ' - else - raise "unknown type specified -- #{type}" + @logger.error "Failed to unload module: #{mod} (#{e})" end - - puts str.gsub(/^/, prefix) end private @@ -116,8 +98,8 @@ module Dinobot parse_line(str.chomp) end rescue => e - log :error, "Error parsing line. (#{e})" - log :indent, *e.backtrace + @logger.error "Error parsing line. (#{e})" + @logger.indent *e.backtrace end end end @@ -166,7 +148,7 @@ module Dinobot def run_methods(methods) methods.each do |m| - log :info, "Executing method: #{m.inspect}" + @logger.info "Executing method: #{m.inspect}" send(*m) end end @@ -1,5 +1,7 @@ require 'socket' +require_relative 'logger' + module Dinobot class IRC def initialize(server, port, nick, pass=nil) @@ -9,10 +11,11 @@ module Dinobot @pass = pass @socket = nil + @logger = Dinobot::Logger.instance end def connect - log :info, "Connecting to #{@server}:#{@port}." + @logger.info "Connecting to #{@server}:#{@port}." @socket = TCPSocket.new(@server, @port) @@ -32,17 +35,21 @@ module Dinobot def gets str = @socket.gets - log :in, str.inspect + @logger.in str.inspect str end def puts(str) - log :out, str.inspect + @logger.out str.inspect @socket.puts str end + def pong(message) + puts "PONG #{message}" + end + def join(channel) puts "JOIN #{channel}" end @@ -54,32 +61,5 @@ module Dinobot def privmsg(channel, message) puts "PRIVMSG #{channel} :#{message}" end - - def pong(message) - puts "PONG #{message}" - end - - private - - def log(type, *lines) - str = lines.join("\n") - - case type - when :in - prefix = "\e[32m<<\e[0m " - when :out - prefix = "\e[36m>>\e[0m " - when :error - prefix = "\e[31m!!\e[0m " - when :info - prefix = "\e[33m==\e[0m " - when :indent - prefix = ' ' - else - raise "unknown type specified -- #{type}" - end - - Kernel.puts str.gsub(/^/, prefix) - end end end diff --git a/logger.rb b/logger.rb new file mode 100644 index 0000000..f84c157 --- /dev/null +++ b/logger.rb @@ -0,0 +1,48 @@ +module Dinobot + class Logger + @@instance = nil + @@mutex = Mutex.new + + def in(*lines) + str = lines.join("\n") + + puts str.gsub(/^/, "\e[32m<<\e[0m ") + end + + def out(*lines) + str = lines.join("\n") + + puts str.gsub(/^/, "\e[36m>>\e[0m ") + end + + def error(*lines) + str = lines.join("\n") + + puts str.gsub(/^/, "\e[31m!!\e[0m ") + end + + def info(*lines) + str = lines.join("\n") + + puts str.gsub(/^/, "\e[33m==\e[0m ") + end + + def indent(*lines) + str = lines.join("\n") + + puts str.gsub(/^/, ' ') + end + + class << self + def instance + return @@instance if @@instance + + @@mutex.synchronize do + @@instance ||= new + end + end + end + + private_class_method :allocate, :new + end +end |