2018-02-05 16:05:59 -05:00
|
|
|
package jsonlog // import "github.com/docker/docker/daemon/logger/jsonfilelog/jsonlog"
|
2015-03-31 17:57:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-10-04 17:07:09 -04:00
|
|
|
"encoding/json"
|
2017-09-22 15:37:16 -04:00
|
|
|
"time"
|
2015-03-31 17:57:39 -04:00
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
2017-09-22 15:37:16 -04:00
|
|
|
// JSONLogs marshals encoded JSONLog objects
|
2015-08-07 23:28:22 -04:00
|
|
|
type JSONLogs struct {
|
2017-09-22 15:37:16 -04:00
|
|
|
Log []byte `json:"log,omitempty"`
|
|
|
|
Stream string `json:"stream,omitempty"`
|
|
|
|
Created time.Time `json:"time"`
|
2015-10-04 17:07:09 -04:00
|
|
|
|
|
|
|
// json-encoded bytes
|
|
|
|
RawAttrs json.RawMessage `json:"attrs,omitempty"`
|
2015-03-31 17:57:39 -04:00
|
|
|
}
|
|
|
|
|
2017-09-25 15:52:42 -04:00
|
|
|
// MarshalJSONBuf is an optimized JSON marshaller that avoids reflection
|
|
|
|
// and unnecessary allocation.
|
2015-08-07 23:28:22 -04:00
|
|
|
func (mj *JSONLogs) MarshalJSONBuf(buf *bytes.Buffer) error {
|
2015-03-31 17:57:39 -04:00
|
|
|
var first = true
|
|
|
|
|
|
|
|
buf.WriteString(`{`)
|
|
|
|
if len(mj.Log) != 0 {
|
2015-06-27 03:18:52 -04:00
|
|
|
first = false
|
2015-03-31 17:57:39 -04:00
|
|
|
buf.WriteString(`"log":`)
|
2015-08-07 23:28:22 -04:00
|
|
|
ffjsonWriteJSONBytesAsString(buf, mj.Log)
|
2015-03-31 17:57:39 -04:00
|
|
|
}
|
|
|
|
if len(mj.Stream) != 0 {
|
2017-03-30 05:26:16 -04:00
|
|
|
if first {
|
2015-03-31 17:57:39 -04:00
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
buf.WriteString(`,`)
|
|
|
|
}
|
|
|
|
buf.WriteString(`"stream":`)
|
2017-09-25 15:52:42 -04:00
|
|
|
ffjsonWriteJSONBytesAsString(buf, []byte(mj.Stream))
|
2015-03-31 17:57:39 -04:00
|
|
|
}
|
2015-10-04 17:07:09 -04:00
|
|
|
if len(mj.RawAttrs) > 0 {
|
2016-03-16 22:43:26 -04:00
|
|
|
if first {
|
2015-10-04 17:07:09 -04:00
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
buf.WriteString(`,`)
|
|
|
|
}
|
|
|
|
buf.WriteString(`"attrs":`)
|
|
|
|
buf.Write(mj.RawAttrs)
|
|
|
|
}
|
2016-03-16 22:43:26 -04:00
|
|
|
if !first {
|
2015-03-31 17:57:39 -04:00
|
|
|
buf.WriteString(`,`)
|
|
|
|
}
|
2017-09-22 15:37:16 -04:00
|
|
|
|
|
|
|
created, err := fastTimeMarshalJSON(mj.Created)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:57:39 -04:00
|
|
|
buf.WriteString(`"time":`)
|
2017-09-22 15:37:16 -04:00
|
|
|
buf.WriteString(created)
|
2015-03-31 17:57:39 -04:00
|
|
|
buf.WriteString(`}`)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-07 23:28:22 -04:00
|
|
|
func ffjsonWriteJSONBytesAsString(buf *bytes.Buffer, s []byte) {
|
2015-03-31 17:57:39 -04:00
|
|
|
const hex = "0123456789abcdef"
|
|
|
|
|
|
|
|
buf.WriteByte('"')
|
|
|
|
start := 0
|
|
|
|
for i := 0; i < len(s); {
|
|
|
|
if b := s[i]; b < utf8.RuneSelf {
|
|
|
|
if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if start < i {
|
|
|
|
buf.Write(s[start:i])
|
|
|
|
}
|
|
|
|
switch b {
|
|
|
|
case '\\', '"':
|
|
|
|
buf.WriteByte('\\')
|
|
|
|
buf.WriteByte(b)
|
|
|
|
case '\n':
|
|
|
|
buf.WriteByte('\\')
|
|
|
|
buf.WriteByte('n')
|
|
|
|
case '\r':
|
|
|
|
buf.WriteByte('\\')
|
|
|
|
buf.WriteByte('r')
|
|
|
|
default:
|
|
|
|
|
|
|
|
buf.WriteString(`\u00`)
|
|
|
|
buf.WriteByte(hex[b>>4])
|
|
|
|
buf.WriteByte(hex[b&0xF])
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
start = i
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c, size := utf8.DecodeRune(s[i:])
|
|
|
|
if c == utf8.RuneError && size == 1 {
|
|
|
|
if start < i {
|
|
|
|
buf.Write(s[start:i])
|
|
|
|
}
|
|
|
|
buf.WriteString(`\ufffd`)
|
|
|
|
i += size
|
|
|
|
start = i
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == '\u2028' || c == '\u2029' {
|
|
|
|
if start < i {
|
|
|
|
buf.Write(s[start:i])
|
|
|
|
}
|
|
|
|
buf.WriteString(`\u202`)
|
|
|
|
buf.WriteByte(hex[c&0xF])
|
|
|
|
i += size
|
|
|
|
start = i
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i += size
|
|
|
|
}
|
|
|
|
if start < len(s) {
|
|
|
|
buf.Write(s[start:])
|
|
|
|
}
|
|
|
|
buf.WriteByte('"')
|
|
|
|
}
|