This repository has been archived by the owner on Sep 26, 2021. It is now read-only.
forked from saymedia/journald-cloudwatch-logs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunmarshal.go
96 lines (84 loc) · 2.03 KB
/
unmarshal.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
96
package main
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/coreos/go-systemd/sdjournal"
)
func UnmarshalRecord(journal *sdjournal.Journal, to *Record) error {
entry, err := journal.GetEntry()
if err != nil {
return err
}
to.TimeUsec = int64(entry.RealtimeTimestamp)
if err := unmarshalRecord(entry, reflect.ValueOf(to).Elem()); err != nil {
return err
}
i := 0
for strings.HasPrefix(to.Message, `{"`) && !strings.HasSuffix(to.Message, `}`) {
// the journal splits up messages of length >2K. Let's try and join them back again
// ..for up to 10 records
seeked, err := journal.Next()
if err != nil {
time.Sleep(2 * time.Second)
continue
}
if seeked == 0 {
journal.Wait(2 * time.Second)
continue
}
entry, err := journal.GetEntry()
to.Message += entry.Fields["MESSAGE"]
i++
if i > 10 {
break
}
}
return nil
}
func unmarshalRecord(entry *sdjournal.JournalEntry, toVal reflect.Value) error {
toType := toVal.Type()
numField := toVal.NumField()
// This intentionally supports only the few types we actually
// use on the Record struct. It's not intended to be generic.
for i := 0; i < numField; i++ {
fieldVal := toVal.Field(i)
fieldDef := toType.Field(i)
fieldType := fieldDef.Type
fieldTag := fieldDef.Tag
fieldTypeKind := fieldType.Kind()
if fieldTypeKind == reflect.Struct {
// Recursively unmarshal from the same journal
unmarshalRecord(entry, fieldVal)
}
jdKey := fieldTag.Get("journald")
if jdKey == "" {
continue
}
value, ok := entry.Fields[jdKey]
if !ok {
fieldVal.Set(reflect.Zero(fieldType))
continue
}
switch fieldTypeKind {
case reflect.Int:
intVal, err := strconv.Atoi(value)
if err != nil {
// Should never happen, but not much we can do here.
fieldVal.Set(reflect.Zero(fieldType))
continue
}
fieldVal.SetInt(int64(intVal))
break
case reflect.String:
fieldVal.SetString(value)
break
default:
// Should never happen
panic(fmt.Errorf("Can't unmarshal to %s", fieldType))
}
}
return nil
}