From 7e7aa3278a9e478b02eb152575fb00ee0802ac81 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sun, 20 Apr 2014 03:30:09 -0700 Subject: Add singleton logger class. --- irc.rb | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) (limited to 'irc.rb') diff --git a/irc.rb b/irc.rb index 835694e..bb7146d 100644 --- a/irc.rb +++ b/irc.rb @@ -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 -- cgit v1.2.3-70-g09d2