Class: Streamforce::Extension::Logging

Inherits:
Object
  • Object
show all
Defined in:
lib/streamforce/extension/logging.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Logging

Returns a new instance of Logging.



2
3
4
# File 'lib/streamforce/extension/logging.rb', line 2

def initialize(logger)
  @logger = logger
end

Instance Method Details

#debug(message, text) ⇒ Object



78
79
80
# File 'lib/streamforce/extension/logging.rb', line 78

def debug(message, text)
  @logger.debug "[#{message.client_id}##{message.id}] #{text}"
end

#error(message, text) ⇒ Object



90
91
92
# File 'lib/streamforce/extension/logging.rb', line 90

def error(message, text)
  @logger.error "[#{message.client_id}##{message.id}] #{text}"
end

#incoming(payload, callback) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/streamforce/extension/logging.rb', line 6

def incoming(payload, callback)
  message = Streamforce::Message.new(payload)

  case message.channel
  when "/meta/handshake"
    log_incoming_handshake(message)
  when "/meta/connect"
    log_incoming_connect(message)
  when "/meta/subscribe"
    log_incoming_subscribe(message)
  else
    @logger.debug "[#{message.channel}] #{message.data}"
  end

  callback.call(payload)
end

#info(message, text) ⇒ Object



82
83
84
# File 'lib/streamforce/extension/logging.rb', line 82

def info(message, text)
  @logger.info "[#{message.client_id}##{message.id}] #{text}"
end

#log_incoming_connect(message) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/streamforce/extension/logging.rb', line 48

def log_incoming_connect(message)
  if message.success?
    debug message, "Connection successful!"
  else
    error message, "Connection failed: #{message.error_message}"
  end
end

#log_incoming_handshake(message) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/streamforce/extension/logging.rb', line 40

def log_incoming_handshake(message)
  if message.success?
    @logger.debug "[Server] Handshake accepted, assigning client_id=#{message.client_id}"
  else
    @logger.error "[Server] Connection was refused: #{message.error_message}"
  end
end

#log_incoming_subscribe(message) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/streamforce/extension/logging.rb', line 70

def log_incoming_subscribe(message)
  if message.success?
    info message, "Successfully subscribed to #{message.subscription}"
  else
    error message, "Subscription for #{message.subscription} failed: #{message.error_message}"
  end
end

#log_outgoing_subscribe(message) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/streamforce/extension/logging.rb', line 56

def log_outgoing_subscribe(message)
  replay_id = message.replay_id

  replay_info = if message.no_replay?
    "for all new messages"
  elsif message.replay_available_messages?
    "and requesting all stored messages"
  else
    "and requesting all messages newer than ##{replay_id}"
  end

  info message, "Subscribing to #{message.subscription} #{replay_info}"
end

#outgoing(payload, callback) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/streamforce/extension/logging.rb', line 23

def outgoing(payload, callback)
  message = Streamforce::Message.new(payload)

  case message.channel
  when "/meta/handshake"
    @logger.debug "[Client] Handshake requested..."
  when "/meta/connect"
    debug message, "Sending connection request"
  when "/meta/subscribe"
    log_outgoing_subscribe(message)
  else
    @logger.debug "[#{message.channel}] #{message.data}"
  end

  callback.call(payload)
end

#warn(message, text) ⇒ Object



86
87
88
# File 'lib/streamforce/extension/logging.rb', line 86

def warn(message, text)
  @logger.warn "[#{message.client_id}##{message.id}] #{text}"
end