nontransparent

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2019 License: MIT Imports: 6 Imported by: 3

Documentation

Overview

Example (BestEffortOnLastOne)
results := []syslog.Result{}
acc := func(res *syslog.Result) {
	results = append(results, *res)
}
r := strings.NewReader("<1>1 - - - - - - -\n<3>1\n")
NewParser(syslog.WithBestEffort(), syslog.WithListener(acc)).Parse(r)
output(results)
Output:

([]syslog.Result) (len=2) {
 (syslog.Result) {
  Message: (*rfc5424.SyslogMessage)({
   priority: (*uint8)(1),
   facility: (*uint8)(0),
   severity: (*uint8)(1),
   version: (uint16) 1,
   timestamp: (*time.Time)(<nil>),
   hostname: (*string)(<nil>),
   appname: (*string)(<nil>),
   procID: (*string)(<nil>),
   msgID: (*string)(<nil>),
   structuredData: (*map[string]map[string]string)(<nil>),
   message: (*string)((len=1) "-")
  }),
  Error: (error) <nil>
 },
 (syslog.Result) {
  Message: (*rfc5424.SyslogMessage)({
   priority: (*uint8)(3),
   facility: (*uint8)(0),
   severity: (*uint8)(3),
   version: (uint16) 1,
   timestamp: (*time.Time)(<nil>),
   hostname: (*string)(<nil>),
   appname: (*string)(<nil>),
   procID: (*string)(<nil>),
   msgID: (*string)(<nil>),
   structuredData: (*map[string]map[string]string)(<nil>),
   message: (*string)(<nil>)
  }),
  Error: (*errors.errorString)(parsing error [col 4])
 }
}
Example (IntoChannelWithLF)
messages := []string{
	"<2>1 - - - - - - A\nB",
	"<1>1 -",
	"<1>1 - - - - - - A\nB\nC\nD",
}

r, w := io.Pipe()

go func() {
	defer w.Close()

	for _, m := range messages {
		// Write message (containing trailers to be interpreted as part of the syslog MESSAGE)
		w.Write([]byte(m))
		// Write non-transparent frame boundary
		w.Write([]byte{10})
		// Wait a random amount of time
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
	}
}()

results := make(chan *syslog.Result)
ln := func(x *syslog.Result) {
	// Emit the result
	results <- x
}

p := NewParser(syslog.WithListener(ln), syslog.WithBestEffort())
go func() {
	defer close(results)
	defer r.Close()
	p.Parse(r)
}()

// Consume results
for r := range results {
	output(r)
}
Output:

(*syslog.Result)({
 Message: (*rfc5424.SyslogMessage)({
  priority: (*uint8)(2),
  facility: (*uint8)(0),
  severity: (*uint8)(2),
  version: (uint16) 1,
  timestamp: (*time.Time)(<nil>),
  hostname: (*string)(<nil>),
  appname: (*string)(<nil>),
  procID: (*string)(<nil>),
  msgID: (*string)(<nil>),
  structuredData: (*map[string]map[string]string)(<nil>),
  message: (*string)((len=3) "A\nB")
 }),
 Error: (error) <nil>
})
(*syslog.Result)({
 Message: (*rfc5424.SyslogMessage)({
  priority: (*uint8)(1),
  facility: (*uint8)(0),
  severity: (*uint8)(1),
  version: (uint16) 1,
  timestamp: (*time.Time)(<nil>),
  hostname: (*string)(<nil>),
  appname: (*string)(<nil>),
  procID: (*string)(<nil>),
  msgID: (*string)(<nil>),
  structuredData: (*map[string]map[string]string)(<nil>),
  message: (*string)(<nil>)
 }),
 Error: (*errors.errorString)(parsing error [col 6])
})
(*syslog.Result)({
 Message: (*rfc5424.SyslogMessage)({
  priority: (*uint8)(1),
  facility: (*uint8)(0),
  severity: (*uint8)(1),
  version: (uint16) 1,
  timestamp: (*time.Time)(<nil>),
  hostname: (*string)(<nil>),
  appname: (*string)(<nil>),
  procID: (*string)(<nil>),
  msgID: (*string)(<nil>),
  structuredData: (*map[string]map[string]string)(<nil>),
  message: (*string)((len=7) "A\nB\nC\nD")
 }),
 Error: (error) <nil>
})
Example (IntoChannelWithNUL)
messages := []string{
	"<2>1 - - - - - - A\x00B",
	"<1>1 -",
	"<1>1 - - - - - - A\x00B\x00C\x00D",
}

r, w := io.Pipe()

go func() {
	defer w.Close()

	for _, m := range messages {
		// Write message (containing trailers to be interpreted as part of the syslog MESSAGE)
		w.Write([]byte(m))
		// Write non-transparent frame boundary
		w.Write([]byte{0})
		// Wait a random amount of time
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
	}
}()

results := make(chan *syslog.Result)
ln := func(x *syslog.Result) {
	// Emit the result
	results <- x
}

p := NewParser(syslog.WithListener(ln), WithTrailer(NUL))

go func() {
	defer close(results)
	defer r.Close()
	p.Parse(r)
}()

// Range over the results channel
for r := range results {
	output(r)
}
Output:

(*syslog.Result)({
 Message: (*rfc5424.SyslogMessage)({
  priority: (*uint8)(2),
  facility: (*uint8)(0),
  severity: (*uint8)(2),
  version: (uint16) 1,
  timestamp: (*time.Time)(<nil>),
  hostname: (*string)(<nil>),
  appname: (*string)(<nil>),
  procID: (*string)(<nil>),
  msgID: (*string)(<nil>),
  structuredData: (*map[string]map[string]string)(<nil>),
  message: (*string)((len=3) "A\x00B")
 }),
 Error: (error) <nil>
})
(*syslog.Result)({
 Message: (syslog.Message) <nil>,
 Error: (*errors.errorString)(parsing error [col 6])
})
(*syslog.Result)({
 Message: (*rfc5424.SyslogMessage)({
  priority: (*uint8)(1),
  facility: (*uint8)(0),
  severity: (*uint8)(1),
  version: (uint16) 1,
  timestamp: (*time.Time)(<nil>),
  hostname: (*string)(<nil>),
  appname: (*string)(<nil>),
  procID: (*string)(<nil>),
  msgID: (*string)(<nil>),
  structuredData: (*map[string]map[string]string)(<nil>),
  message: (*string)((len=7) "A\x00B\x00C\x00D")
 }),
 Error: (error) <nil>
})
Example (WithoutTrailerAtEnd)
results := []syslog.Result{}
acc := func(res *syslog.Result) {
	results = append(results, *res)
}
// Notice the message ends without trailer but we catch it anyway
r := strings.NewReader("<1>1 2003-10-11T22:14:15.003Z host.local - - - - mex")
NewParser(syslog.WithListener(acc)).Parse(r)
output(results)
Output:

([]syslog.Result) (len=1) {
 (syslog.Result) {
  Message: (*rfc5424.SyslogMessage)({
   priority: (*uint8)(1),
   facility: (*uint8)(0),
   severity: (*uint8)(1),
   version: (uint16) 1,
   timestamp: (*time.Time)(2003-10-11 22:14:15.003 +0000 UTC),
   hostname: (*string)((len=10) "host.local"),
   appname: (*string)(<nil>),
   procID: (*string)(<nil>),
   msgID: (*string)(<nil>),
   structuredData: (*map[string]map[string]string)(<nil>),
   message: (*string)((len=3) "mex")
  }),
  Error: (*ragel.ReadingError)(unexpected EOF)
 }
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewParser

func NewParser(options ...syslog.ParserOption) syslog.Parser

NewParser returns a syslog.Parser suitable to parse syslog messages sent with non-transparent framing - ie. RFC 6587.

func WithTrailer

func WithTrailer(t TrailerType) syslog.ParserOption

WithTrailer ... todo(leodido)

Types

type TrailerType

type TrailerType int

TrailerType is the king of supported trailers for non-transparent frames.

const (
	// LF is the line feed - ie., byte 10. Also the default one.
	LF TrailerType = iota
	// NUL is the nul byte - ie., byte 0.
	NUL
)

func TrailerTypeFromString

func TrailerTypeFromString(s string) (TrailerType, error)

TrailerTypeFromString returns a TrailerType given a string.

func (TrailerType) MarshalText

func (t TrailerType) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler

func (TrailerType) String

func (t TrailerType) String() string

func (*TrailerType) UnmarshalTOML

func (t *TrailerType) UnmarshalTOML(data []byte) (err error)

UnmarshalTOML decodes trailer type from TOML data.

func (*TrailerType) UnmarshalText

func (t *TrailerType) UnmarshalText(data []byte) (err error)

UnmarshalText implements encoding.TextUnmarshaler

func (TrailerType) Value

func (t TrailerType) Value() (int, error)

Value returns the byte corresponding to the receiving TrailerType.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL