network_logger_io 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/network_logger_io.rb +54 -0
  2. metadata +45 -0
@@ -0,0 +1,54 @@
1
+ require 'socket'
2
+
3
+ class NetworkLoggerIO < IO
4
+ attr_reader :options
5
+
6
+ def initialize(options)
7
+ @options = options
8
+
9
+ raise(ArgumentError, ':host is required') unless @options[:host]
10
+ raise(ArgumentError, ':api_key is required') unless @options[:api_key]
11
+
12
+ @options[:port] ||= 4455
13
+ @options[:type] ||= 'default'
14
+ @options[:buffer_size] ||= 512
15
+
16
+ @socket = UDPSocket.new
17
+ empty_buffer!
18
+ end
19
+
20
+ def empty_buffer!
21
+ @buffer = fresh_buffer
22
+ end
23
+
24
+ def fresh_buffer
25
+ [@options[:api_key].bytesize, @options[:api_key], @options[:type].bytesize, @options[:type]].pack('nA*nA*')
26
+ end
27
+
28
+ def write(data)
29
+ data.force_encoding('BINARY')
30
+ data = data[0,@options[:buffer_size] - fresh_buffer.bytesize - 2]
31
+
32
+ ## Pre-flush if the existing buffer plus the new data plus the terminator would be more than the buffer size
33
+ if @buffer.bytesize + 2 + data.bytesize + 2 > @options[:buffer_size]
34
+ self.flush
35
+ end
36
+
37
+ @buffer << [data.bytesize].pack('n')
38
+ @buffer << data
39
+
40
+ ## Flush is autoflush is set or the buffer is full
41
+ self.flush if @options[:auto_flush] or @buffer.bytesize + 2 >= @options[:buffer_size]
42
+
43
+ return data.bytesize
44
+ end
45
+
46
+ def flush
47
+ return self if @buffer == @api_key
48
+ @buffer << "\0\0"
49
+ @socket.send(@buffer, 0, @options[:host], @options[:port])
50
+ empty_buffer!
51
+ return self
52
+ end
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: network_logger_io
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charlie Smurthwaite
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-17 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: An IO stream to log to a remote server via UDP
15
+ email: charlie@atechmedia.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/network_logger_io.rb
21
+ homepage: http://atechmedia.com
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.18
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Network Logger IO
45
+ test_files: []