Class: Proxi::RequestResponseLogging

Inherits:
Object
  • Object
show all
Defined in:
lib/proxi/listeners.rb

Overview

Log all incoming and outgoing traffic to files under `/tmp`

For example:

Proxi.tcp_server(...).subscribe(RequestResponseLogging.new).call

The in and outgoing traffic will be captured per connection in `/tmp/proxi.1.in` and `/tmp/proxi.1.out`; `/tmp/proxi.2.in`, etc.

Instance Method Summary collapse

Constructor Details

#initialize(dir: Dir.tmpdir, name: "proxi") ⇒ RequestResponseLogging

Returns a new instance of RequestResponseLogging.


16
17
18
19
20
21
# File 'lib/proxi/listeners.rb', line 16

def initialize(dir: Dir.tmpdir, name: "proxi")
  @dir = dir
  @name = name
  @count = 0
  @mutex = Mutex.new
end

Instance Method Details

#log_name(num, suffix) ⇒ Object


34
35
36
# File 'lib/proxi/listeners.rb', line 34

def log_name(num, suffix)
  '%s/%s.%d.%s' % [@dir, @name, num, suffix]
end

#new_connection(connection) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'lib/proxi/listeners.rb', line 23

def new_connection(connection)
  count = @mutex.synchronize { @count += 1 }
  in_fd = File.open(log_name(count, "in"), 'w')
  out_fd = File.open(log_name(count, "out"), 'w')

  connection
    .on(:data_in) { |_, data| in_fd.write(data) ; in_fd.flush }
    .on(:data_out) { |_, data| out_fd.write(data) ; out_fd.flush }
    .on(:end_connection) { in_fd.close ; out_fd.close }
end