Module: Fargo::CLI::Logging

Included in:
Console
Defined in:
lib/fargo/cli/logging.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject



27
28
29
# File 'lib/fargo/cli/logging.rb', line 27

def client
  @client ||= DRbObject.new_with_uri 'druby://127.0.0.1:8082'
end

Instance Method Details

#add_logger(type, &block) ⇒ Object



23
24
25
# File 'lib/fargo/cli/logging.rb', line 23

def add_logger type, &block
  @logging[type.to_s] << block
end

#log_published_messagesObject



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
59
60
61
62
63
64
# File 'lib/fargo/cli/logging.rb', line 31

def log_published_messages
  @logging = Hash.new{ |h, k| h[k] = [] }

  streamer = proc {
    host = "ws://#{client.config.websocket_host}" +
              ":#{client.config.websocket_port}/"

    ws = EventMachine::HttpRequest.new(host).get(:timeout => 0)

    ws.disconnect {
      Readline.above_prompt{ puts "Stopping logging stream." }
    }

    ws.callback {
      Readline.above_prompt{ puts "Streaming logging messages." }
    }

    ws.stream { |msg|
      to_log = nil
      type, message = Marshal.load(Base64.decode64(msg))

      @logging[type.to_s].each{ |l|
        to_log = l.call message
        Readline.above_prompt{ puts to_log } unless to_log.nil?
      }
    }
  }

  if EventMachine.reactor_running?
    EventMachine.schedule streamer
  else
    Thread.start{ EventMachine.run streamer }
  end
end

#setup_consoleObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fargo/cli/logging.rb', line 10

def setup_console
  super

  add_logger(:chat) do |message|
    "<#{message[:from]}>: #{message[:text]}"
  end

  add_logger(:hub_disconnected) do |_|
    puts "Hub disconnected, exiting..."
    exit
  end
end