github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/logger/journald/internal/export/export.go (about) 1 // Package export implements a serializer for the systemd Journal Export Format 2 // as documented at https://systemd.io/JOURNAL_EXPORT_FORMATS/ 3 package export // import "github.com/docker/docker/daemon/logger/journald/internal/export" 4 5 import ( 6 "encoding/binary" 7 "fmt" 8 "io" 9 "unicode/utf8" 10 ) 11 12 // Returns whether s can be serialized as a field value "as they are" without 13 // the special binary safe serialization. 14 func isSerializableAsIs(s string) bool { 15 if !utf8.ValidString(s) { 16 return false 17 } 18 for _, c := range s { 19 if c < ' ' && c != '\t' { 20 return false 21 } 22 } 23 return true 24 } 25 26 // WriteField writes the field serialized to Journal Export format to w. 27 // 28 // The variable name must consist only of uppercase characters, numbers and 29 // underscores. No validation or sanitization is performed. 30 func WriteField(w io.Writer, variable, value string) error { 31 if isSerializableAsIs(value) { 32 _, err := fmt.Fprintf(w, "%s=%s\n", variable, value) 33 return err 34 } 35 36 if _, err := fmt.Fprintln(w, variable); err != nil { 37 return err 38 } 39 if err := binary.Write(w, binary.LittleEndian, uint64(len(value))); err != nil { 40 return err 41 } 42 _, err := fmt.Fprintln(w, value) 43 return err 44 } 45 46 // WriteEndOfEntry terminates the journal entry. 47 func WriteEndOfEntry(w io.Writer) error { 48 _, err := fmt.Fprintln(w) 49 return err 50 }