/usr/share/gocode/src/github.com/jeromer/syslogparser/rfc3164/rfc3164.go is in golang-github-jeromer-syslogparser-dev 0~20150717.ff71fe7a-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | package rfc3164
import (
"bytes"
"github.com/jeromer/syslogparser"
"time"
)
type Parser struct {
buff []byte
cursor int
l int
priority syslogparser.Priority
version int
header header
message rfc3164message
location *time.Location
}
type header struct {
timestamp time.Time
hostname string
}
type rfc3164message struct {
tag string
content string
}
func NewParser(buff []byte) *Parser {
return &Parser{
buff: buff,
cursor: 0,
l: len(buff),
location: time.UTC,
}
}
func (p *Parser) Location(location *time.Location) {
p.location = location
}
func (p *Parser) Parse() error {
pri, err := p.parsePriority()
if err != nil {
return err
}
hdr, err := p.parseHeader()
if err != nil {
return err
}
p.cursor++
msg, err := p.parsemessage()
if err != syslogparser.ErrEOL {
return err
}
p.priority = pri
p.version = syslogparser.NO_VERSION
p.header = hdr
p.message = msg
return nil
}
func (p *Parser) Dump() syslogparser.LogParts {
return syslogparser.LogParts{
"timestamp": p.header.timestamp,
"hostname": p.header.hostname,
"tag": p.message.tag,
"content": p.message.content,
"priority": p.priority.P,
"facility": p.priority.F.Value,
"severity": p.priority.S.Value,
}
}
func (p *Parser) parsePriority() (syslogparser.Priority, error) {
return syslogparser.ParsePriority(p.buff, &p.cursor, p.l)
}
func (p *Parser) parseHeader() (header, error) {
hdr := header{}
var err error
ts, err := p.parseTimestamp()
if err != nil {
return hdr, err
}
hostname, err := p.parseHostname()
if err != nil {
return hdr, err
}
hdr.timestamp = ts
hdr.hostname = hostname
return hdr, nil
}
func (p *Parser) parsemessage() (rfc3164message, error) {
msg := rfc3164message{}
var err error
tag, err := p.parseTag()
if err != nil {
return msg, err
}
content, err := p.parseContent()
if err != syslogparser.ErrEOL {
return msg, err
}
msg.tag = tag
msg.content = content
return msg, err
}
// https://tools.ietf.org/html/rfc3164#section-4.1.2
func (p *Parser) parseTimestamp() (time.Time, error) {
var ts time.Time
var err error
var tsFmtLen int
var sub []byte
tsFmts := []string{
"Jan 02 15:04:05",
"Jan 2 15:04:05",
}
found := false
for _, tsFmt := range tsFmts {
tsFmtLen = len(tsFmt)
if p.cursor+tsFmtLen > p.l {
continue
}
sub = p.buff[p.cursor : tsFmtLen+p.cursor]
ts, err = time.ParseInLocation(tsFmt, string(sub), p.location)
if err == nil {
found = true
break
}
}
if !found {
p.cursor = tsFmtLen
// XXX : If the timestamp is invalid we try to push the cursor one byte
// XXX : further, in case it is a space
if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
p.cursor++
}
return ts, syslogparser.ErrTimestampUnknownFormat
}
fixTimestampIfNeeded(&ts)
p.cursor += tsFmtLen
if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
p.cursor++
}
return ts, nil
}
func (p *Parser) parseHostname() (string, error) {
return syslogparser.ParseHostname(p.buff, &p.cursor, p.l)
}
// http://tools.ietf.org/html/rfc3164#section-4.1.3
func (p *Parser) parseTag() (string, error) {
var b byte
var endOfTag bool
var bracketOpen bool
var tag []byte
var err error
var found bool
from := p.cursor
for {
b = p.buff[p.cursor]
bracketOpen = (b == '[')
endOfTag = (b == ':' || b == ' ')
// XXX : parse PID ?
if bracketOpen {
tag = p.buff[from:p.cursor]
found = true
}
if endOfTag {
if !found {
tag = p.buff[from:p.cursor]
found = true
}
p.cursor++
break
}
p.cursor++
}
if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
p.cursor++
}
return string(tag), err
}
func (p *Parser) parseContent() (string, error) {
if p.cursor > p.l {
return "", syslogparser.ErrEOL
}
content := bytes.Trim(p.buff[p.cursor:p.l], " ")
p.cursor += len(content)
return string(content), syslogparser.ErrEOL
}
func fixTimestampIfNeeded(ts *time.Time) {
now := time.Now()
y := ts.Year()
if ts.Year() == 0 {
y = now.Year()
}
newTs := time.Date(y, ts.Month(), ts.Day(), ts.Hour(), ts.Minute(),
ts.Second(), ts.Nanosecond(), ts.Location())
*ts = newTs
}
|