master
go 65 lines 1.45 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package logs
4
5 import (
6 "errors"
7 "fmt"
8 "io"
9 "strconv"
10 )
11
12 type ParseError struct {
13 msg string
14 err error
15 }
16
17 func (e ParseError) Error() string { return e.msg }
18
19 func (e ParseError) Unwrap() error { return e.err }
20
21 func IsParseError(err error) bool { var v *ParseError; return errors.As(err, &v) }
22
23 type (
24 LogLine interface {
25 Assign(name string, value string) error
26 }
27
28 Parser interface {
29 ReadLine(LogLine) error
30 Parse(row []byte, line LogLine) error
31 Info() string
32 }
33 )
34
35 const (
36 TypeCSV = "csv"
37 TypeLTSV = "ltsv"
38 TypeRegExp = "regexp"
39 TypeJSON = "json"
40 )
41
42 type ParserConfig struct {
43 LogType string `yaml:"log_type,omitempty" json:"log_type"`
44 CSV CSVConfig `yaml:"csv_config,omitempty" json:"csv_config"`
45 LTSV LTSVConfig `yaml:"ltsv_config,omitempty" json:"ltsv_config"`
46 RegExp RegExpConfig `yaml:"regexp_config,omitempty" json:"regexp_config"`
47 JSON JSONConfig `yaml:"json_config,omitempty" json:"json_config"`
48 }
49
50 func NewParser(config ParserConfig, in io.Reader) (Parser, error) {
51 switch config.LogType {
52 case TypeCSV:
53 return NewCSVParser(config.CSV, in)
54 case TypeLTSV:
55 return NewLTSVParser(config.LTSV, in)
56 case TypeRegExp:
57 return NewRegExpParser(config.RegExp, in)
58 case TypeJSON:
59 return NewJSONParser(config.JSON, in)
60 default:
61 return nil, fmt.Errorf("invalid type: %q", config.LogType)
62 }
63 }
64
65 func isNumber(s string) bool { _, err := strconv.Atoi(s); return err == nil }