Skip to content

Commit

Permalink
fix error checking conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkpc138 committed Oct 14, 2024
1 parent 5ae59f2 commit c1743ba
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions pkg/lobster/logline/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ func ParseLogMessage(str string) (string, error) {
}

func ParseTimestamp(str string) (time.Time, error) {
if err := checkTimestamp(str); err != nil {
return time.Time{}, err
}

switch *conf.LogFormat {
case LogFormatText:
return getTimestampInTextLogLine(str)
Expand Down Expand Up @@ -169,6 +165,10 @@ func getLogMessageInJsonLogLine(str string) (string, error) {
}

func getTimestampInTextLogLine(str string) (time.Time, error) {
if len(str) < MinTimestampLen || !(str[4] == '-' && str[7] == '-' && str[10] == 'T' && str[13] == ':' && str[16] == ':') {
return time.Time{}, fmt.Errorf("could not parse improper input %s", str)
}

year := (((int(str[0])-'0')*10+int(str[1])-'0')*10+int(str[2])-'0')*10 + int(str[3]) - '0'
month := time.Month((int(str[5])-'0')*10 + int(str[6]) - '0')
day := (int(str[8])-'0')*10 + int(str[9]) - '0'
Expand Down Expand Up @@ -223,14 +223,6 @@ func interpretLocalOffset(offset string) (int, int) {
return int(offset[1]-'0')*10 + int(offset[2]-'0')*op, int(offset[3+colonOffset]-'0')*10 + int(offset[4+colonOffset]-'0')*op
}

func checkTimestamp(str string) error {
if len(str) < MinTimestampLen || !(str[4] == '-' && str[7] == '-' && str[10] == 'T' && str[13] == ':' && str[16] == ':') {
return fmt.Errorf("could not parse improper input %s", str)
}

return nil
}

func getTimestampInJsonLogLine(str string) (time.Time, error) {
matches := regexpTime.FindStringSubmatch(str)
if len(matches) < 2 {
Expand All @@ -242,7 +234,7 @@ func getTimestampInJsonLogLine(str string) (time.Time, error) {
return time.Time{}, err
}

return t, nil
return t.Local(), nil
}

func ParseLogMessageTest(str, format string) (string, error) {
Expand All @@ -253,14 +245,14 @@ func ParseLogMessageTest(str, format string) (string, error) {
}

func ParseTimestampTest(str, format string) (time.Time, error) {
if err := checkTimestamp(str); err != nil {
return time.Time{}, err
}

if format == LogFormatText {
switch format {
case LogFormatText:
return getTimestampInTextLogLine(str)
case LogFormatJson:
return getTimestampInJsonLogLine(str)
}
return getTimestampInJsonLogLine(str)

return time.Time{}, errors.New("unsupported logline.format")
}

func HasProperLogLine(f model.LogFile) bool {
Expand Down

0 comments on commit c1743ba

Please sign in to comment.