class SyslogProtocol::Packet

Attributes

content[RW]
facility[R]
hostname[R]
severity[R]
tag[R]
time[RW]

Public Instance Methods

assemble(max_size = 1024) click to toggle source
# File lib/syslog_protocol/packet.rb, line 10
def assemble(max_size = 1024)
  unless @hostname and @facility and @severity and @tag
    raise "Could not assemble packet without hostname, tag, facility, and severity"
  end
  data = "<#{pri}>#{generate_timestamp} #{@hostname} #{@tag}: #{@content}"

  if string_bytesize(data) > max_size
    data = data.slice(0, max_size)
    while string_bytesize(data) > max_size
      data = data.slice(0, data.length - 1)
    end
  end

  data
end
facility=(f) click to toggle source
# File lib/syslog_protocol/packet.rb, line 26
def facility=(f)
  if f.is_a? Integer
    if (0..23).include?(f)
      @facility = f
    else
      raise ArgumentError.new "Facility must be within 0-23"
    end
  elsif f.is_a? String
    if facility = FACILITIES[f]
      @facility = facility
    else
      raise ArgumentError.new "'#{f}' is not a designated facility"
    end
  else
    raise ArgumentError.new "Facility must be a designated number or string"
  end
end
facility_name() click to toggle source
# File lib/syslog_protocol/packet.rb, line 92
def facility_name
  FACILITY_INDEX[@facility]
end
generate_timestamp() click to toggle source
# File lib/syslog_protocol/packet.rb, line 112
def generate_timestamp
  time = @time || Time.now
  # The timestamp format requires that a day with fewer than 2 digits have
  # what would normally be a preceding zero, be instead an extra space.
  day = time.strftime("%d")
  day = day.sub(/^0/, ' ') if day =~ /^0\d/
  time.strftime("%b #{day} %H:%M:%S")
end
hostname=(h) click to toggle source
# File lib/syslog_protocol/packet.rb, line 79
def hostname=(h)
  unless h and h.is_a? String and h.length > 0
    raise ArgumentError.new("Hostname may not be omitted")
  end
  if h =~ /\s/
    raise ArgumentError.new("Hostname may not contain spaces")
  end
  if h =~ /[^\x21-\x7E]/
    raise ArgumentError.new("Hostname may only contain ASCII characters 33-126")
  end
  @hostname = h
end
pri() click to toggle source
# File lib/syslog_protocol/packet.rb, line 100
def pri
  (@facility * 8) + @severity
end
pri=(p) click to toggle source
# File lib/syslog_protocol/packet.rb, line 104
def pri=(p)
  unless p.is_a? Integer and (0..191).include?(p)
    raise ArgumentError.new "PRI must be a number between 0 and 191"
  end
  @facility = p / 8
  @severity = p - (@facility * 8)
end
severity=(s) click to toggle source
# File lib/syslog_protocol/packet.rb, line 61
def severity=(s)
  if s.is_a? Integer
    if (0..7).include?(s)
      @severity = s
    else
      raise ArgumentError.new "Severity must be within 0-7"
    end
  elsif s.is_a? String
    if severity = SEVERITIES[s]
      @severity = severity
    else
      raise ArgumentError.new "'#{s}' is not a designated severity"
    end
  else
    raise ArgumentError.new "Severity must be a designated number or string"
  end
end
severity_name() click to toggle source
# File lib/syslog_protocol/packet.rb, line 96
def severity_name
  SEVERITY_INDEX[@severity]
end
string_bytesize(string) click to toggle source
# File lib/syslog_protocol/packet.rb, line 122
def string_bytesize(string)
  string.bytesize
end
tag=(t) click to toggle source
# File lib/syslog_protocol/packet.rb, line 44
def tag=(t)
  unless t && t.is_a?(String) && t.length > 0
    raise ArgumentError, "Tag must not be omitted"
  end
  if t.length > 1024
    raise ArgumentError, "Tag must not be longer than 1024 characters"
  end
  if t =~ /\s/
    raise ArgumentError, "Tag may not contain spaces"
  end
  if t =~ /[^\x21-\x7E]/
    raise ArgumentError, "Tag may only contain ASCII characters 33-126"
  end

  @tag = t
end
to_s() click to toggle source
# File lib/syslog_protocol/packet.rb, line 6
def to_s
  assemble
end