// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zapcore

import (
	

	
	
	
)

var _sliceEncoderPool = pool.New(func() *sliceArrayEncoder {
	return &sliceArrayEncoder{
		elems: make([]interface{}, 0, 2),
	}
})

func () *sliceArrayEncoder {
	return _sliceEncoderPool.Get()
}

func ( *sliceArrayEncoder) {
	.elems = .elems[:0]
	_sliceEncoderPool.Put()
}

type consoleEncoder struct {
	*jsonEncoder
}

// NewConsoleEncoder creates an encoder whose output is designed for human -
// rather than machine - consumption. It serializes the core log entry data
// (message, level, timestamp, etc.) in a plain-text format and leaves the
// structured context as JSON.
//
// Note that although the console encoder doesn't use the keys specified in the
// encoder configuration, it will omit any element whose key is set to the empty
// string.
func ( EncoderConfig) Encoder {
	if .ConsoleSeparator == "" {
		// Use a default delimiter of '\t' for backwards compatibility
		.ConsoleSeparator = "\t"
	}
	return consoleEncoder{newJSONEncoder(, true)}
}

func ( consoleEncoder) () Encoder {
	return consoleEncoder{.jsonEncoder.Clone().(*jsonEncoder)}
}

func ( consoleEncoder) ( Entry,  []Field) (*buffer.Buffer, error) {
	 := bufferpool.Get()

	// We don't want the entry's metadata to be quoted and escaped (if it's
	// encoded as strings), which means that we can't use the JSON encoder. The
	// simplest option is to use the memory encoder and fmt.Fprint.
	//
	// If this ever becomes a performance bottleneck, we can implement
	// ArrayEncoder for our plain-text format.
	 := getSliceEncoder()
	if .TimeKey != "" && .EncodeTime != nil {
		.EncodeTime(.Time, )
	}
	if .LevelKey != "" && .EncodeLevel != nil {
		.EncodeLevel(.Level, )
	}
	if .LoggerName != "" && .NameKey != "" {
		 := .EncodeName

		if  == nil {
			// Fall back to FullNameEncoder for backward compatibility.
			 = FullNameEncoder
		}

		(.LoggerName, )
	}
	if .Caller.Defined {
		if .CallerKey != "" && .EncodeCaller != nil {
			.EncodeCaller(.Caller, )
		}
		if .FunctionKey != "" {
			.AppendString(.Caller.Function)
		}
	}
	for  := range .elems {
		if  > 0 {
			.AppendString(.ConsoleSeparator)
		}
		fmt.Fprint(, .elems[])
	}
	putSliceEncoder()

	// Add the message itself.
	if .MessageKey != "" {
		.addSeparatorIfNecessary()
		.AppendString(.Message)
	}

	// Add any structured context.
	.writeContext(, )

	// If there's no stacktrace key, honor that; this allows users to force
	// single-line output.
	if .Stack != "" && .StacktraceKey != "" {
		.AppendByte('\n')
		.AppendString(.Stack)
	}

	.AppendString(.LineEnding)
	return , nil
}

func ( consoleEncoder) ( *buffer.Buffer,  []Field) {
	 := .jsonEncoder.Clone().(*jsonEncoder)
	defer func() {
		// putJSONEncoder assumes the buffer is still used, but we write out the buffer so
		// we can free it.
		.buf.Free()
		putJSONEncoder()
	}()

	addFields(, )
	.closeOpenNamespaces()
	if .buf.Len() == 0 {
		return
	}

	.addSeparatorIfNecessary()
	.AppendByte('{')
	.Write(.buf.Bytes())
	.AppendByte('}')
}

func ( consoleEncoder) ( *buffer.Buffer) {
	if .Len() > 0 {
		.AppendString(.ConsoleSeparator)
	}
}