Skip to content

Commit a7fb955

Browse files
author
Jérôme Renard
committed
Reorganized files
1 parent 055b0b0 commit a7fb955

File tree

12 files changed

+343
-301
lines changed

12 files changed

+343
-301
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
SUBPACKAGES=. rfc3164 rfc5424
12
help:
23
@echo "Available targets:"
34
@echo "- tests: run tests"
@@ -6,13 +7,13 @@ help:
67
@echo "- benchmarks: run benchmarks"
78

89
installdependencies:
9-
cat dependencies.txt | xargs go get
10+
@cat dependencies.txt | xargs go get
1011

1112
tests: installdependencies
12-
go test -i && go test
13+
@for pkg in $(SUBPACKAGES); do cd $$pkg && go test -i && go test ; cd -;done
1314

1415
clean:
1516
find . -type 'f' -name '*.test' -print | xargs rm -f
1617

1718
benchmarks:
18-
go test -gocheck.b
19+
@for pkg in $(SUBPACKAGES); do cd $$pkg && go test -gocheck.b ; cd -;done

errors.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

interface.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

rfc3164/example_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package rfc3164_test
2+
3+
import (
4+
"fmt"
5+
"github.com/jeromer/syslogparser/rfc3164"
6+
)
7+
8+
func ExampleNewParser() {
9+
b := "<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8"
10+
buff := []byte(b)
11+
12+
p := rfc3164.NewParser(buff)
13+
err := p.Parse()
14+
if err != nil {
15+
panic(err)
16+
}
17+
18+
fmt.Println(p.Dump())
19+
}

rfc3164.go renamed to rfc3164/rfc3164.go

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
1-
package syslogparser
1+
package rfc3164
22

33
import (
44
"bytes"
5+
"github.com/jeromer/syslogparser"
56
"time"
67
)
78

8-
func NewRfc3164Parser(buff []byte) *rfc3164Parser {
9-
return &rfc3164Parser{
9+
var (
10+
ErrTagTooLong = &syslogparser.ParserError{"Tag name too long"}
11+
)
12+
13+
type Parser struct {
14+
buff []byte
15+
cursor int
16+
l int
17+
priority syslogparser.Priority
18+
version int
19+
header header
20+
message rfc3164message
21+
}
22+
23+
type header struct {
24+
timestamp time.Time
25+
hostname string
26+
}
27+
28+
type rfc3164message struct {
29+
tag string
30+
content string
31+
}
32+
33+
func NewParser(buff []byte) *Parser {
34+
return &Parser{
1035
buff: buff,
1136
cursor: 0,
1237
l: len(buff),
1338
}
1439
}
1540

16-
func (p *rfc3164Parser) Parse() error {
41+
func (p *Parser) Parse() error {
1742
pri, err := p.parsePriority()
1843
if err != nil {
1944
return err
@@ -26,37 +51,37 @@ func (p *rfc3164Parser) Parse() error {
2651

2752
p.cursor++
2853

29-
msg, err := p.parseMessage()
30-
if err != ErrEOL {
54+
msg, err := p.parsemessage()
55+
if err != syslogparser.ErrEOL {
3156
return err
3257
}
3358

3459
p.priority = pri
35-
p.version = NO_VERSION
60+
p.version = syslogparser.NO_VERSION
3661
p.header = hdr
3762
p.message = msg
3863

3964
return nil
4065
}
4166

42-
func (p *rfc3164Parser) Dump() LogParts {
43-
return LogParts{
67+
func (p *Parser) Dump() syslogparser.LogParts {
68+
return syslogparser.LogParts{
4469
"timestamp": p.header.timestamp,
4570
"hostname": p.header.hostname,
4671
"tag": p.message.tag,
4772
"content": p.message.content,
48-
"priority": p.priority.p,
49-
"facility": p.priority.f.value,
50-
"severity": p.priority.s.value,
73+
"priority": p.priority.P,
74+
"facility": p.priority.F.Value,
75+
"severity": p.priority.S.Value,
5176
}
5277
}
5378

54-
func (p *rfc3164Parser) parsePriority() (priority, error) {
55-
return parsePriority(p.buff, &p.cursor, p.l)
79+
func (p *Parser) parsePriority() (syslogparser.Priority, error) {
80+
return syslogparser.ParsePriority(p.buff, &p.cursor, p.l)
5681
}
5782

58-
func (p *rfc3164Parser) parseHeader() (rfc3164Header, error) {
59-
hdr := rfc3164Header{}
83+
func (p *Parser) parseHeader() (header, error) {
84+
hdr := header{}
6085
var err error
6186

6287
ts, err := p.parseTimestamp()
@@ -75,8 +100,8 @@ func (p *rfc3164Parser) parseHeader() (rfc3164Header, error) {
75100
return hdr, nil
76101
}
77102

78-
func (p *rfc3164Parser) parseMessage() (rfc3164Message, error) {
79-
msg := rfc3164Message{}
103+
func (p *Parser) parsemessage() (rfc3164message, error) {
104+
msg := rfc3164message{}
80105
var err error
81106

82107
tag, err := p.parseTag()
@@ -85,7 +110,7 @@ func (p *rfc3164Parser) parseMessage() (rfc3164Message, error) {
85110
}
86111

87112
content, err := p.parseContent()
88-
if err != ErrEOL {
113+
if err != syslogparser.ErrEOL {
89114
return msg, err
90115
}
91116

@@ -96,7 +121,7 @@ func (p *rfc3164Parser) parseMessage() (rfc3164Message, error) {
96121
}
97122

98123
// https://tools.ietf.org/html/rfc3164#section-4.1.2
99-
func (p *rfc3164Parser) parseTimestamp() (time.Time, error) {
124+
func (p *Parser) parseTimestamp() (time.Time, error) {
100125
var ts time.Time
101126
var err error
102127

@@ -106,7 +131,7 @@ func (p *rfc3164Parser) parseTimestamp() (time.Time, error) {
106131

107132
if p.cursor+tsFmtLen > p.l {
108133
p.cursor = p.l
109-
return ts, ErrEOL
134+
return ts, syslogparser.ErrEOL
110135
}
111136

112137
sub := p.buff[p.cursor : tsFmtLen+p.cursor]
@@ -120,7 +145,7 @@ func (p *rfc3164Parser) parseTimestamp() (time.Time, error) {
120145
p.cursor++
121146
}
122147

123-
return ts, ErrTimestampUnknownFormat
148+
return ts, syslogparser.ErrTimestampUnknownFormat
124149
}
125150

126151
fixTimestampIfNeeded(&ts)
@@ -134,12 +159,12 @@ func (p *rfc3164Parser) parseTimestamp() (time.Time, error) {
134159
return ts, nil
135160
}
136161

137-
func (p *rfc3164Parser) parseHostname() (string, error) {
138-
return parseHostname(p.buff, &p.cursor, p.l)
162+
func (p *Parser) parseHostname() (string, error) {
163+
return syslogparser.ParseHostname(p.buff, &p.cursor, p.l)
139164
}
140165

141166
// http://tools.ietf.org/html/rfc3164#section-4.1.3
142-
func (p *rfc3164Parser) parseTag() (string, error) {
167+
func (p *Parser) parseTag() (string, error) {
143168
var b byte
144169
var endOfTag bool
145170
var bracketOpen bool
@@ -187,15 +212,15 @@ func (p *rfc3164Parser) parseTag() (string, error) {
187212
return string(tag), err
188213
}
189214

190-
func (p *rfc3164Parser) parseContent() (string, error) {
215+
func (p *Parser) parseContent() (string, error) {
191216
if p.cursor > p.l {
192-
return "", ErrEOL
217+
return "", syslogparser.ErrEOL
193218
}
194219

195220
content := bytes.Trim(p.buff[p.cursor:p.l], " ")
196221
p.cursor += len(content)
197222

198-
return string(content), ErrEOL
223+
return string(content), syslogparser.ErrEOL
199224
}
200225

201226
func fixTimestampIfNeeded(ts *time.Time) {

0 commit comments

Comments
 (0)