class SyslogGenerator::Logger

Implementation of a log formatter/sender

Attributes

options[RW]

Public Class Methods

new(options) click to toggle source
# File lib/syslog_generator.rb, line 44
def initialize(options)
  self.options = options
  # TODO: Won't work on systems that don't have `hostname`?
  @formatter = SyslogProtocol::Logger.new(
    `hostname`.strip,
    options[:name],
    options[:facility]
  )
  @socket = initialize_socket(options)
end

Public Instance Methods

send(text) click to toggle source
# File lib/syslog_generator.rb, line 55
def send(text)
  if options[:test]
    puts(gen_payload(text))
  else
    @socket.send(gen_payload(text), 0, options[:server], options[:port])
  end
end
start() click to toggle source
# File lib/syslog_generator.rb, line 63
def start
  if options[:count] != -1
    options[:count].times { send(gen_words) }
  else
    loop { send(gen_words) }
  end
end

Private Instance Methods

gen_payload(text) click to toggle source
# File lib/syslog_generator.rb, line 24
def gen_payload(text)
  # Syslog_protocol uses different methods for each priority
  # (@formatter.debug, @formatter.emerg, etc) so we use .method to save the
  # call with the appropriate name, and .call to invoke it with the right
  # string. This won't be likely to break since we check for valid names
  # back at options parsing.
  meta_formatter = @formatter.method(options[:priority])
  if text.is_a?(Array)
    @payload = meta_formatter.call(text.join(' '))
  else
    @payload = meta_formatter.call(info(text))
  end
end
gen_words() click to toggle source
# File lib/syslog_generator.rb, line 38
def gen_words
  RandomWord.nouns.take(options[:words])
end
initialize_socket(options) click to toggle source
# File lib/syslog_generator.rb, line 14
def initialize_socket(options)
  case options[:protocol].downcase
  when 'udp'
    UDPSocket.new
  when 'tcp'
    TCPSocket.new
  else fail 'Invalid protocol specified.'
  end
end