Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Udp #58

Closed
wants to merge 2 commits into from
Closed

Udp #58

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/scrolls/log.rb
@@ -1,6 +1,7 @@
require "scrolls/parser"
require "scrolls/utils"
require "scrolls/syslog"
require "scrolls/udp"

module Scrolls

Expand Down Expand Up @@ -60,6 +61,9 @@ def stream=(out=nil)
@defined = out.nil? ? false : true
if out == 'syslog'
@stream = Scrolls::SyslogLogger.new(progname, facility)
elsif out =~ /^udp/
parse_url(out)
@stream = Scrolls::UDPLogger.new(udp_host, udp_port)
else
@stream = sync_stream(out)
end
Expand Down Expand Up @@ -192,6 +196,14 @@ def with_context(prefix)
res
end

def udp_host
@udp_host ||= "127.0.0.1"
end

def udp_port
@udp_port ||= 514
end

private

def get_global_context
Expand Down Expand Up @@ -270,5 +282,18 @@ def progname
def default_log_facility
LOG_FACILITY
end

def parse_url(out)
# FIXME
if out =~ /^udp:\/\/([^ ]):([^ ])/
# We've been given something like: udp://host:port
@udp_host = $1
@udp_port = $2.to_i
elsif out =~ /^udp:([^ ]):([^ ])/
# We've been given something like: udp:host:port
@udp_host = $1
@udp_port = $2.to_i
end
end
end
end
26 changes: 26 additions & 0 deletions lib/scrolls/udp.rb
@@ -0,0 +1,26 @@
require 'socket'

module Scrolls
class UDPLogger

def initialize(host, port, options={})
@host = host || "127.0.0.1"
@port = port || "514"

initialize_socket
end

def initialize_socket
@socket = UDPSocket.new
@socket.connect(@host, @port)
end

def socket
@socket ||= initialize_socket
end

def puts(data)
socket.send(data, 0)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Format message in syslog format.

end
end
end
13 changes: 13 additions & 0 deletions test/test_scrolls.rb
Expand Up @@ -165,6 +165,19 @@ def test_setting_syslog_facility_after_instantiation
assert_match /facility=184/, Scrolls.stream.inspect
end

def test_udp_integration
Scrolls.stream = 'udp'
assert_equal Scrolls::UDPLogger, Scrolls.stream.class
end

def test_udp_host
assert_equal '127.0.0.1', Scrolls::Log.udp_host
end

def test_udp_port
assert_equal 514, Scrolls::Log.udp_port
end

def test_add_timestamp
Scrolls.add_timestamp = true
Scrolls.log(:test => "foo")
Expand Down