Class: SyslogProtocol::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/syslog_protocol/packet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/syslog_protocol/packet.rb', line 4

def content
  @content
end

#facilityObject

Returns the value of attribute facility.



3
4
5
# File 'lib/syslog_protocol/packet.rb', line 3

def facility
  @facility
end

#hostnameObject

Returns the value of attribute hostname.



3
4
5
# File 'lib/syslog_protocol/packet.rb', line 3

def hostname
  @hostname
end

#severityObject

Returns the value of attribute severity.



3
4
5
# File 'lib/syslog_protocol/packet.rb', line 3

def severity
  @severity
end

#tagObject

Returns the value of attribute tag.



3
4
5
# File 'lib/syslog_protocol/packet.rb', line 3

def tag
  @tag
end

#timeObject

Returns the value of attribute time.



4
5
6
# File 'lib/syslog_protocol/packet.rb', line 4

def time
  @time
end

Instance Method Details

#assemble(max_size = 1024) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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_nameObject



92
93
94
# File 'lib/syslog_protocol/packet.rb', line 92

def facility_name
  FACILITY_INDEX[@facility]
end

#generate_timestampObject



112
113
114
115
116
117
118
119
# 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

#priObject



100
101
102
# File 'lib/syslog_protocol/packet.rb', line 100

def pri
  (@facility * 8) + @severity
end

#pri=(p) ⇒ Object



104
105
106
107
108
109
110
# 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_nameObject



96
97
98
# File 'lib/syslog_protocol/packet.rb', line 96

def severity_name
  SEVERITY_INDEX[@severity]
end

#string_bytesize(string) ⇒ Object



122
123
124
# File 'lib/syslog_protocol/packet.rb', line 122

def string_bytesize(string)
  string.bytesize
end

#to_sObject



6
7
8
# File 'lib/syslog_protocol/packet.rb', line 6

def to_s
  assemble
end