/
parser.go
95 lines (81 loc) · 2.28 KB
/
parser.go
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
//go:build !no_logs
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package parser
import (
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
// NoopParser is the default parser and does nothing
var NoopParser *noopParser
// Encoding is our internal type for supported encoding for the DecodingParser
type Encoding int
const (
// UTF16LE UTF16 little endian, most common (windows)
UTF16LE = iota
// UTF16BE UTF16 big endian
UTF16BE
//
GBK18030
//
GB2312
//
HZGB2312
//
GBK
//
BIG5
)
// Parser parse messages
type Parser interface {
// It returns 1. raw message, 2. severity, 3. timestamp, 4. partial, 5. error
Parse([]byte) ([]byte, string, string, bool, error)
SupportsPartialLine() bool
}
type noopParser struct{}
// Parse does nothing for NoopParser
func (p *noopParser) Parse(msg []byte) ([]byte, string, string, bool, error) {
return msg, "", "", false, nil
}
func (p *noopParser) SupportsPartialLine() bool {
return false
}
// DecodingParser a generic decoding Parser
type DecodingParser struct {
decoder *encoding.Decoder
}
// Parse parses the incoming message with the decoder
func (p *DecodingParser) Parse(msg []byte) ([]byte, string, string, bool, error) {
decoded, _, err := transform.Bytes(p.decoder, msg)
return decoded, "", "", false, err
}
// SupportsPartialLine returns false as it does not support partial lines
func (p *DecodingParser) SupportsPartialLine() bool {
return false
}
// NewDecodingParser build a new DecodingParser
func NewDecodingParser(e Encoding) *DecodingParser {
p := &DecodingParser{}
var enc encoding.Encoding
switch e {
case UTF16LE:
enc = unicode.UTF16(unicode.LittleEndian, unicode.UseBOM)
case UTF16BE:
enc = unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
case GBK, GB2312:
enc = simplifiedchinese.GBK
case HZGB2312:
enc = simplifiedchinese.HZGB2312
case GBK18030:
enc = simplifiedchinese.GB18030
case BIG5:
enc = traditionalchinese.Big5
}
p.decoder = enc.NewDecoder()
return p
}