2014-09-15 16:35:51 +00:00
|
|
|
// This code was initially generated by ffjson <https://github.com/pquerna/ffjson>
|
|
|
|
// This code was generated via the following steps:
|
|
|
|
// $ go get -u github.com/pquerna/ffjson
|
2015-02-13 22:50:42 +00:00
|
|
|
// $ make BIND_DIR=. shell
|
2014-09-15 16:35:51 +00:00
|
|
|
// $ ffjson pkg/jsonlog/jsonlog.go
|
|
|
|
// $ mv pkg/jsonglog/jsonlog_ffjson.go pkg/jsonlog/jsonlog_marshalling.go
|
|
|
|
//
|
|
|
|
// It has been modified to improve the performance of time marshalling to JSON
|
|
|
|
// and to clean it up.
|
|
|
|
// Should this code need to be regenerated when the JSONLog struct is changed,
|
|
|
|
// the relevant changes which have been made are:
|
|
|
|
// import (
|
|
|
|
// "bytes"
|
|
|
|
//-
|
|
|
|
// "unicode/utf8"
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// func (mj *JSONLog) MarshalJSON() ([]byte, error) {
|
|
|
|
//@@ -20,13 +16,13 @@ func (mj *JSONLog) MarshalJSON() ([]byte, error) {
|
|
|
|
// }
|
|
|
|
// return buf.Bytes(), nil
|
|
|
|
// }
|
|
|
|
//+
|
|
|
|
// func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
|
|
|
|
//- var err error
|
|
|
|
//- var obj []byte
|
|
|
|
//- var first bool = true
|
|
|
|
//- _ = obj
|
|
|
|
//- _ = err
|
|
|
|
//- _ = first
|
|
|
|
//+ var (
|
|
|
|
//+ err error
|
|
|
|
//+ timestamp string
|
|
|
|
//+ first bool = true
|
|
|
|
//+ )
|
|
|
|
// buf.WriteString(`{`)
|
|
|
|
// if len(mj.Log) != 0 {
|
|
|
|
// if first == true {
|
|
|
|
//@@ -52,11 +48,11 @@ func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
|
|
|
|
// buf.WriteString(`,`)
|
|
|
|
// }
|
|
|
|
// buf.WriteString(`"time":`)
|
|
|
|
//- obj, err = mj.Created.MarshalJSON()
|
2015-12-15 19:49:41 +00:00
|
|
|
//+ timestamp, err = FastTimeMarshalJSON(mj.Created)
|
2014-09-15 16:35:51 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
//- buf.Write(obj)
|
|
|
|
//+ buf.WriteString(timestamp)
|
|
|
|
// buf.WriteString(`}`)
|
|
|
|
// return nil
|
|
|
|
// }
|
2015-06-27 07:18:52 +00:00
|
|
|
// @@ -81,9 +81,10 @@ func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
|
|
|
|
// if len(mj.Log) != 0 {
|
|
|
|
// - if first == true {
|
|
|
|
// - first = false
|
|
|
|
// - } else {
|
|
|
|
// - buf.WriteString(`,`)
|
|
|
|
// - }
|
|
|
|
// + first = false
|
|
|
|
// buf.WriteString(`"log":`)
|
2015-08-08 03:28:22 +00:00
|
|
|
// ffjsonWriteJSONString(buf, mj.Log)
|
2015-06-27 07:18:52 +00:00
|
|
|
// }
|
2014-09-15 16:35:51 +00:00
|
|
|
|
|
|
|
package jsonlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
2015-08-08 03:28:22 +00:00
|
|
|
// MarshalJSON marshals the JSONLog.
|
2014-09-15 16:35:51 +00:00
|
|
|
func (mj *JSONLog) MarshalJSON() ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.Grow(1024)
|
2015-04-26 16:50:25 +00:00
|
|
|
if err := mj.MarshalJSONBuf(&buf); err != nil {
|
2014-09-15 16:35:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2015-08-08 03:28:22 +00:00
|
|
|
// MarshalJSONBuf marshals the JSONLog and stores the result to a bytes.Buffer.
|
2014-09-15 16:35:51 +00:00
|
|
|
func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
timestamp string
|
2015-08-08 03:28:22 +00:00
|
|
|
first = true
|
2014-09-15 16:35:51 +00:00
|
|
|
)
|
|
|
|
buf.WriteString(`{`)
|
|
|
|
if len(mj.Log) != 0 {
|
2015-06-27 07:18:52 +00:00
|
|
|
first = false
|
2014-09-15 16:35:51 +00:00
|
|
|
buf.WriteString(`"log":`)
|
2015-08-08 03:28:22 +00:00
|
|
|
ffjsonWriteJSONString(buf, mj.Log)
|
2014-09-15 16:35:51 +00:00
|
|
|
}
|
|
|
|
if len(mj.Stream) != 0 {
|
|
|
|
if first == true {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
buf.WriteString(`,`)
|
|
|
|
}
|
|
|
|
buf.WriteString(`"stream":`)
|
2015-08-08 03:28:22 +00:00
|
|
|
ffjsonWriteJSONString(buf, mj.Stream)
|
2014-09-15 16:35:51 +00:00
|
|
|
}
|
|
|
|
if first == true {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
buf.WriteString(`,`)
|
|
|
|
}
|
|
|
|
buf.WriteString(`"time":`)
|
2015-12-15 19:49:41 +00:00
|
|
|
timestamp, err = FastTimeMarshalJSON(mj.Created)
|
2014-09-15 16:35:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buf.WriteString(timestamp)
|
|
|
|
buf.WriteString(`}`)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-08 03:28:22 +00:00
|
|
|
func ffjsonWriteJSONString(buf *bytes.Buffer, s string) {
|
2014-09-15 16:35:51 +00: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.WriteString(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.DecodeRuneInString(s[i:])
|
|
|
|
if c == utf8.RuneError && size == 1 {
|
|
|
|
if start < i {
|
|
|
|
buf.WriteString(s[start:i])
|
|
|
|
}
|
|
|
|
buf.WriteString(`\ufffd`)
|
|
|
|
i += size
|
|
|
|
start = i
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == '\u2028' || c == '\u2029' {
|
|
|
|
if start < i {
|
|
|
|
buf.WriteString(s[start:i])
|
|
|
|
}
|
|
|
|
buf.WriteString(`\u202`)
|
|
|
|
buf.WriteByte(hex[c&0xF])
|
|
|
|
i += size
|
|
|
|
start = i
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i += size
|
|
|
|
}
|
|
|
|
if start < len(s) {
|
|
|
|
buf.WriteString(s[start:])
|
|
|
|
}
|
|
|
|
buf.WriteByte('"')
|
|
|
|
}
|