Class: RemoteSyslogSender::Sender
- Inherits:
-
Object
- Object
- RemoteSyslogSender::Sender
- Defined in:
- lib/remote_syslog_sender/sender.rb
Defined Under Namespace
Classes: Packet
Instance Attribute Summary collapse
-
#packet ⇒ Object
Returns the value of attribute packet.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(remote_hostname, remote_port, options = {}) ⇒ Sender
constructor
A new instance of Sender.
- #transmit(message, packet_options = nil) ⇒ Object (also: #write)
Constructor Details
#initialize(remote_hostname, remote_port, options = {}) ⇒ Sender
Returns a new instance of Sender.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/remote_syslog_sender/sender.rb', line 17 def initialize(remote_hostname, remote_port, = {}) @remote_hostname = remote_hostname @remote_port = remote_port @whinyerrors = [:whinyerrors] @packet_size = [:packet_size] || 1024 @packet = Packet.new local_hostname = [:hostname] || [:local_hostname] || (Socket.gethostname rescue `hostname`.chomp) local_hostname = 'localhost' if local_hostname.nil? || local_hostname.empty? @packet.hostname = local_hostname @packet.facility = [:facility] || 'user' @packet.severity = [:severity] || 'notice' @packet.tag = [:tag] || [:program] || "#{File.basename($0)}[#{$$}]" @socket = nil end |
Instance Attribute Details
#packet ⇒ Object
Returns the value of attribute packet.
15 16 17 |
# File 'lib/remote_syslog_sender/sender.rb', line 15 def packet @packet end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
14 15 16 |
# File 'lib/remote_syslog_sender/sender.rb', line 14 def socket @socket end |
Instance Method Details
#close ⇒ Object
63 64 65 |
# File 'lib/remote_syslog_sender/sender.rb', line 63 def close @socket.close end |
#transmit(message, packet_options = nil) ⇒ Object Also known as: write
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/remote_syslog_sender/sender.rb', line 36 def transmit(, = nil) .split(/\r?\n/).each do |line| begin next if line =~ /^\s*$/ packet = @packet.dup if packet.tag = [:program] if [:program] packet.hostname = [:local_hostname] if [:local_hostname] %i(hostname facility severity tag).each do |key| packet.send("#{key}=", [key]) if [key] end end packet.content = line send_msg(packet.assemble(@packet_size)) rescue if @whinyerrors raise else $stderr.puts "#{self.class} error: #{$!.class}: #{$!}\nOriginal message: #{line}" end end end end |