2015-03-31 17:57:39 -04:00
|
|
|
package jsonlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
2015-08-07 23:28:22 -04:00
|
|
|
// JSONLogs is based on JSONLog.
|
2015-03-31 17:57:39 -04:00
|
|
|
// It allows marshalling JSONLog from Log as []byte
|
|
|
|
// and an already marshalled Created timestamp.
|
2015-08-07 23:28:22 -04:00
|
|
|
type JSONLogs struct {
|
2015-03-31 17:57:39 -04:00
|
|
|
Log []byte `json:"log,omitempty"`
|
|
|
|
Stream string `json:"stream,omitempty"`
|
|
|
|
Created string `json:"time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSONBuf is based on the same method from JSONLog
|
|
|
|
// It has been modified to take into account the necessary changes.
|
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 {
|
|
|
|
if first == true {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
buf.WriteString(`,`)
|
|
|
|
}
|
|
|
|
buf.WriteString(`"stream":`)
|
2015-08-07 23:28:22 -04:00
|
|
|
ffjsonWriteJSONString(buf, mj.Stream)
|
2015-03-31 17:57:39 -04:00
|
|
|
}
|
|
|
|
if first == true {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
buf.WriteString(`,`)
|
|
|
|
}
|
|
|
|
buf.WriteString(`"time":`)
|
|
|
|
buf.WriteString(mj.Created)
|
|
|
|
buf.WriteString(`}`)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-07 23:28:22 -04:00
|
|
|
// This is based on ffjsonWriteJSONBytesAsString. It has been changed
|
2015-03-31 17:57:39 -04:00
|
|
|
// to accept a string passed as a slice of bytes.
|
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('"')
|
|
|
|
}
|