Class: XMPPLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/xmpp4log.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login, pass, users) ⇒ XMPPLogger

Returns a new instance of XMPPLogger.



33
34
35
36
37
38
39
40
41
42
# File 'lib/xmpp4log.rb', line 33

def initialize(, pass, users)
  @jabber = Jabber::Simple.new(, pass)
  @users = users
  @mute  = false
  @rd, @wr = IO.pipe
  super(@wr)
  @thread = Thread.new do
    run
  end
end

Instance Attribute Details

#jabberObject

Returns the value of attribute jabber.



31
32
33
# File 'lib/xmpp4log.rb', line 31

def jabber
  @jabber
end

#muteObject

Returns the value of attribute mute.



31
32
33
# File 'lib/xmpp4log.rb', line 31

def mute
  @mute
end

#rdObject

Returns the value of attribute rd.



31
32
33
# File 'lib/xmpp4log.rb', line 31

def rd
  @rd
end

#threadObject

Returns the value of attribute thread.



31
32
33
# File 'lib/xmpp4log.rb', line 31

def thread
  @thread
end

#usersObject

Returns the value of attribute users.



31
32
33
# File 'lib/xmpp4log.rb', line 31

def users
  @users
end

#wrObject

Returns the value of attribute wr.



31
32
33
# File 'lib/xmpp4log.rb', line 31

def wr
  @wr
end

Instance Method Details

#can_speak?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/xmpp4log.rb', line 60

def can_speak?
  not @mute
end

#deliver(msg) ⇒ Object



44
45
46
# File 'lib/xmpp4log.rb', line 44

def deliver(msg)
  @users.each {|user| @jabber.deliver(user, msg)} if can_speak?
end

#handle_commandsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/xmpp4log.rb', line 64

def handle_commands
    @jabber.received_messages do |msg|
	case msg.body.downcase
	when 'mute'
 self.mute = true
 self << "Logger set muted by #{msg.from}"
	when 'demute'
 self.mute = false
 self << "Logger set unmuted by #{msg.from}"
	when 'debug'
 self.level = Logger::DEBUG
 self << "Logger set to DEBUG level by #{msg.from}"
	when 'info'
 self.level = Logger::INFO
 self << "Logger set to INFO level by #{msg.from}"
	when 'warn'
 self.level = Logger::WARN
 self << "Logger set to WARN level by #{msg.from}"
	when 'error'
 self.level = Logger::ERROR
 self << "Logger set to ERROR level by #{msg.from}"
	when 'fatal'
 self.level = Logger::FATAL
 self << "Logger set to FATAL level by #{msg.from}"
	end
    end
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/xmpp4log.rb', line 48

def run
  loop do
    r,w,e = select([@rd], nil, nil, 1)
    if r != nil
	msg = r[0].read_nonblock(1024) 
	break if msg.empty? #XXX nothing to read?
	deliver(msg) if msg and not msg.empty?
    end
    handle_commands
  end
end