// 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

// Core is a minimal, fast logger interface. It's designed for library authors
// to wrap in a more user-friendly API.
type Core interface {
	LevelEnabler

	// With adds structured context to the Core.
	With([]Field) Core
	// Check determines whether the supplied Entry should be logged (using the
	// embedded LevelEnabler and possibly some extra logic). If the entry
	// should be logged, the Core adds itself to the CheckedEntry and returns
	// the result.
	//
	// Callers must use Check before calling Write.
	Check(Entry, *CheckedEntry) *CheckedEntry
	// Write serializes the Entry and any Fields supplied at the log site and
	// writes them to their destination.
	//
	// If called, Write should always log the Entry and Fields; it should not
	// replicate the logic of Check.
	Write(Entry, []Field) error
	// Sync flushes buffered logs (if any).
	Sync() error
}

type nopCore struct{}

// NewNopCore returns a no-op Core.
func () Core                                        { return nopCore{} }
func (nopCore) (Level) bool                            { return false }
func ( nopCore) ([]Field) Core                           { return  }
func (nopCore) ( Entry,  *CheckedEntry) *CheckedEntry { return  }
func (nopCore) (Entry, []Field) error                    { return nil }
func (nopCore) () error                                   { return nil }

// NewCore creates a Core that writes logs to a WriteSyncer.
func ( Encoder,  WriteSyncer,  LevelEnabler) Core {
	return &ioCore{
		LevelEnabler: ,
		enc:          ,
		out:          ,
	}
}

type ioCore struct {
	LevelEnabler
	enc Encoder
	out WriteSyncer
}

var (
	_ Core           = (*ioCore)(nil)
	_ leveledEnabler = (*ioCore)(nil)
)

func ( *ioCore) () Level {
	return LevelOf(.LevelEnabler)
}

func ( *ioCore) ( []Field) Core {
	 := .clone()
	addFields(.enc, )
	return 
}

func ( *ioCore) ( Entry,  *CheckedEntry) *CheckedEntry {
	if .Enabled(.Level) {
		return .AddCore(, )
	}
	return 
}

func ( *ioCore) ( Entry,  []Field) error {
	,  := .enc.EncodeEntry(, )
	if  != nil {
		return 
	}
	_,  = .out.Write(.Bytes())
	.Free()
	if  != nil {
		return 
	}
	if .Level > ErrorLevel {
		// Since we may be crashing the program, sync the output.
		// Ignore Sync errors, pending a clean solution to issue #370.
		_ = .Sync()
	}
	return nil
}

func ( *ioCore) () error {
	return .out.Sync()
}

func ( *ioCore) () *ioCore {
	return &ioCore{
		LevelEnabler: .LevelEnabler,
		enc:          .enc.Clone(),
		out:          .out,
	}
}