/*
 *
 * Copyright 2024 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package internal

import (
	
	
	
	
	
)

// LoggerV2 does underlying logging work for grpclog.
type LoggerV2 interface {
	// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
	Info(args ...any)
	// Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println.
	Infoln(args ...any)
	// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
	Infof(format string, args ...any)
	// Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print.
	Warning(args ...any)
	// Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println.
	Warningln(args ...any)
	// Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
	Warningf(format string, args ...any)
	// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
	Error(args ...any)
	// Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
	Errorln(args ...any)
	// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
	Errorf(format string, args ...any)
	// Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print.
	// gRPC ensures that all Fatal logs will exit with os.Exit(1).
	// Implementations may also call os.Exit() with a non-zero exit code.
	Fatal(args ...any)
	// Fatalln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
	// gRPC ensures that all Fatal logs will exit with os.Exit(1).
	// Implementations may also call os.Exit() with a non-zero exit code.
	Fatalln(args ...any)
	// Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
	// gRPC ensures that all Fatal logs will exit with os.Exit(1).
	// Implementations may also call os.Exit() with a non-zero exit code.
	Fatalf(format string, args ...any)
	// V reports whether verbosity level l is at least the requested verbose level.
	V(l int) bool
}

// DepthLoggerV2 logs at a specified call frame. If a LoggerV2 also implements
// DepthLoggerV2, the below functions will be called with the appropriate stack
// depth set for trivial functions the logger may ignore.
//
// # Experimental
//
// Notice: This type is EXPERIMENTAL and may be changed or removed in a
// later release.
type DepthLoggerV2 interface {
	LoggerV2
	// InfoDepth logs to INFO log at the specified depth. Arguments are handled in the manner of fmt.Println.
	InfoDepth(depth int, args ...any)
	// WarningDepth logs to WARNING log at the specified depth. Arguments are handled in the manner of fmt.Println.
	WarningDepth(depth int, args ...any)
	// ErrorDepth logs to ERROR log at the specified depth. Arguments are handled in the manner of fmt.Println.
	ErrorDepth(depth int, args ...any)
	// FatalDepth logs to FATAL log at the specified depth. Arguments are handled in the manner of fmt.Println.
	FatalDepth(depth int, args ...any)
}

const (
	// infoLog indicates Info severity.
	infoLog int = iota
	// warningLog indicates Warning severity.
	warningLog
	// errorLog indicates Error severity.
	errorLog
	// fatalLog indicates Fatal severity.
	fatalLog
)

// severityName contains the string representation of each severity.
var severityName = []string{
	infoLog:    "INFO",
	warningLog: "WARNING",
	errorLog:   "ERROR",
	fatalLog:   "FATAL",
}

// sprintf is fmt.Sprintf.
// These vars exist to make it possible to test that expensive format calls aren't made unnecessarily.
var sprintf = fmt.Sprintf

// sprint is fmt.Sprint.
// These vars exist to make it possible to test that expensive format calls aren't made unnecessarily.
var sprint = fmt.Sprint

// sprintln is fmt.Sprintln.
// These vars exist to make it possible to test that expensive format calls aren't made unnecessarily.
var sprintln = fmt.Sprintln

// exit is os.Exit.
// This var exists to make it possible to test functions calling os.Exit.
var exit = os.Exit

// loggerT is the default logger used by grpclog.
type loggerT struct {
	m          []*log.Logger
	v          int
	jsonFormat bool
}

func ( *loggerT) ( int,  string) {
	 := severityName[]
	if !.jsonFormat {
		.m[].Output(2, +": "+)
		return
	}
	// TODO: we can also include the logging component, but that needs more
	// (API) changes.
	,  := json.Marshal(map[string]string{
		"severity": ,
		"message":  ,
	})
	.m[].Output(2, string())
}

func ( *loggerT) ( int,  string,  ...any) {
	// Note the discard check is duplicated in each print func, rather than in
	// output, to avoid the expensive Sprint calls.
	// De-duplicating this by moving to output would be a significant performance regression!
	if  := .m[]; .Writer() == io.Discard {
		return
	}
	.output(, sprintf(, ...))
}

func ( *loggerT) ( int,  ...any) {
	if  := .m[]; .Writer() == io.Discard {
		return
	}
	.output(, sprint(...))
}

func ( *loggerT) ( int,  ...any) {
	if  := .m[]; .Writer() == io.Discard {
		return
	}
	.output(, sprintln(...))
}

func ( *loggerT) ( ...any) {
	.print(infoLog, ...)
}

func ( *loggerT) ( ...any) {
	.println(infoLog, ...)
}

func ( *loggerT) ( string,  ...any) {
	.printf(infoLog, , ...)
}

func ( *loggerT) ( ...any) {
	.print(warningLog, ...)
}

func ( *loggerT) ( ...any) {
	.println(warningLog, ...)
}

func ( *loggerT) ( string,  ...any) {
	.printf(warningLog, , ...)
}

func ( *loggerT) ( ...any) {
	.print(errorLog, ...)
}

func ( *loggerT) ( ...any) {
	.println(errorLog, ...)
}

func ( *loggerT) ( string,  ...any) {
	.printf(errorLog, , ...)
}

func ( *loggerT) ( ...any) {
	.print(fatalLog, ...)
	exit(1)
}

func ( *loggerT) ( ...any) {
	.println(fatalLog, ...)
	exit(1)
}

func ( *loggerT) ( string,  ...any) {
	.printf(fatalLog, , ...)
	exit(1)
}

func ( *loggerT) ( int) bool {
	return  <= .v
}

// LoggerV2Config configures the LoggerV2 implementation.
type LoggerV2Config struct {
	// Verbosity sets the verbosity level of the logger.
	Verbosity int
	// FormatJSON controls whether the logger should output logs in JSON format.
	FormatJSON bool
}

// combineLoggers returns a combined logger for both higher & lower severity logs,
// or only one if the other is io.Discard.
//
// This uses io.Discard instead of io.MultiWriter when all loggers
// are set to io.Discard. Both this package and the standard log package have
// significant optimizations for io.Discard, which io.MultiWriter lacks (as of
// this writing).
func (,  io.Writer) io.Writer {
	if  == io.Discard {
		return 
	}
	if  == io.Discard {
		return 
	}
	return io.MultiWriter(, )
}

// NewLoggerV2 creates a new LoggerV2 instance with the provided configuration.
// The infoW, warningW, and errorW writers are used to write log messages of
// different severity levels.
func (, ,  io.Writer,  LoggerV2Config) LoggerV2 {
	 := log.LstdFlags
	if .FormatJSON {
		 = 0
	}

	 = combineLoggers(, )
	 = combineLoggers(, )

	 := 

	 := []*log.Logger{
		log.New(, "", ),
		log.New(, "", ),
		log.New(, "", ),
		log.New(, "", ),
	}
	return &loggerT{m: , v: .Verbosity, jsonFormat: .FormatJSON}
}