// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package log // import "go.opentelemetry.io/otel/sdk/log"

import (
	
	
	
	

	
	
	
	
	
	
	
)

const (
	defaultAttrCntLim    = 128
	defaultAttrValLenLim = -1

	envarAttrCntLim    = "OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT"
	envarAttrValLenLim = "OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT"
)

type providerConfig struct {
	resource       *resource.Resource
	processors     []Processor
	fltrProcessors []FilterProcessor
	attrCntLim     setting[int]
	attrValLenLim  setting[int]
	allowDupKeys   setting[bool]
}

func newProviderConfig( []LoggerProviderOption) providerConfig {
	var  providerConfig
	for ,  := range  {
		 = .apply()
	}

	if .resource == nil {
		.resource = resource.Default()
	}

	.attrCntLim = .attrCntLim.Resolve(
		getenv[int](envarAttrCntLim),
		fallback[int](defaultAttrCntLim),
	)

	.attrValLenLim = .attrValLenLim.Resolve(
		getenv[int](envarAttrValLenLim),
		fallback[int](defaultAttrValLenLim),
	)

	return 
}

// LoggerProvider handles the creation and coordination of Loggers. All Loggers
// created by a LoggerProvider will be associated with the same Resource.
type LoggerProvider struct {
	embedded.LoggerProvider

	resource                  *resource.Resource
	processors                []Processor
	fltrProcessors            []FilterProcessor
	attributeCountLimit       int
	attributeValueLengthLimit int
	allowDupKeys              bool

	loggersMu sync.Mutex
	loggers   map[instrumentation.Scope]*logger

	stopped atomic.Bool

	noCmp [0]func() //nolint: unused  // This is indeed used.
}

// Compile-time check LoggerProvider implements log.LoggerProvider.
var _ log.LoggerProvider = (*LoggerProvider)(nil)

// NewLoggerProvider returns a new and configured LoggerProvider.
//
// By default, the returned LoggerProvider is configured with the default
// Resource and no Processors. Processors cannot be added after a LoggerProvider is
// created. This means the returned LoggerProvider, one created with no
// Processors, will perform no operations.
func ( ...LoggerProviderOption) *LoggerProvider {
	 := newProviderConfig()
	return &LoggerProvider{
		resource:                  .resource,
		processors:                .processors,
		fltrProcessors:            .fltrProcessors,
		attributeCountLimit:       .attrCntLim.Value,
		attributeValueLengthLimit: .attrValLenLim.Value,
		allowDupKeys:              .allowDupKeys.Value,
	}
}

// Logger returns a new [log.Logger] with the provided name and configuration.
//
// If p is shut down, a [noop.Logger] instance is returned.
//
// This method can be called concurrently.
func ( *LoggerProvider) ( string,  ...log.LoggerOption) log.Logger {
	if  == "" {
		global.Warn("Invalid Logger name.", "name", )
	}

	if .stopped.Load() {
		return noop.NewLoggerProvider().Logger(, ...)
	}

	 := log.NewLoggerConfig(...)
	 := instrumentation.Scope{
		Name:       ,
		Version:    .InstrumentationVersion(),
		SchemaURL:  .SchemaURL(),
		Attributes: .InstrumentationAttributes(),
	}

	.loggersMu.Lock()
	defer .loggersMu.Unlock()

	if .loggers == nil {
		 := newLogger(, )
		.loggers = map[instrumentation.Scope]*logger{: }
		return 
	}

	,  := .loggers[]
	if ! {
		 = newLogger(, )
		.loggers[] = 
	}

	return 
}

// Shutdown shuts down the provider and all processors.
//
// This method can be called concurrently.
func ( *LoggerProvider) ( context.Context) error {
	 := .stopped.Swap(true)
	if  {
		return nil
	}

	var  error
	for ,  := range .processors {
		 = errors.Join(, .Shutdown())
	}
	return 
}

// ForceFlush flushes all processors.
//
// This method can be called concurrently.
func ( *LoggerProvider) ( context.Context) error {
	if .stopped.Load() {
		return nil
	}

	var  error
	for ,  := range .processors {
		 = errors.Join(, .ForceFlush())
	}
	return 
}

// LoggerProviderOption applies a configuration option value to a LoggerProvider.
type LoggerProviderOption interface {
	apply(providerConfig) providerConfig
}

type loggerProviderOptionFunc func(providerConfig) providerConfig

func ( loggerProviderOptionFunc) ( providerConfig) providerConfig {
	return ()
}

// WithResource associates a Resource with a LoggerProvider. This Resource
// represents the entity producing telemetry and is associated with all Loggers
// the LoggerProvider will create.
//
// By default, if this Option is not used, the default Resource from the
// go.opentelemetry.io/otel/sdk/resource package will be used.
func ( *resource.Resource) LoggerProviderOption {
	return loggerProviderOptionFunc(func( providerConfig) providerConfig {
		var  error
		.resource,  = resource.Merge(resource.Environment(), )
		if  != nil {
			otel.Handle()
		}
		return 
	})
}

// WithProcessor associates Processor with a LoggerProvider.
//
// By default, if this option is not used, the LoggerProvider will perform no
// operations; no data will be exported without a processor.
//
// The SDK invokes the processors sequentially in the same order as they were
// registered.
//
// For production, use [NewBatchProcessor] to batch log records before they are exported.
// For testing and debugging, use [NewSimpleProcessor] to synchronously export log records.
//
// See [FilterProcessor] for information about how a Processor can support filtering.
func ( Processor) LoggerProviderOption {
	return loggerProviderOptionFunc(func( providerConfig) providerConfig {
		.processors = append(.processors, )
		if ,  := .(FilterProcessor);  {
			.fltrProcessors = append(.fltrProcessors, )
		}
		return 
	})
}

// WithAttributeCountLimit sets the maximum allowed log record attribute count.
// Any attribute added to a log record once this limit is reached will be dropped.
//
// Setting this to zero means no attributes will be recorded.
//
// Setting this to a negative value means no limit is applied.
//
// If the OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT environment variable is set,
// and this option is not passed, that variable value will be used.
//
// By default, if an environment variable is not set, and this option is not
// passed, 128 will be used.
func ( int) LoggerProviderOption {
	return loggerProviderOptionFunc(func( providerConfig) providerConfig {
		.attrCntLim = newSetting()
		return 
	})
}

// WithAttributeValueLengthLimit sets the maximum allowed attribute value length.
//
// This limit only applies to string and string slice attribute values.
// Any string longer than this value will be truncated to this length.
//
// Setting this to a negative value means no limit is applied.
//
// If the OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT environment variable is set,
// and this option is not passed, that variable value will be used.
//
// By default, if an environment variable is not set, and this option is not
// passed, no limit (-1) will be used.
func ( int) LoggerProviderOption {
	return loggerProviderOptionFunc(func( providerConfig) providerConfig {
		.attrValLenLim = newSetting()
		return 
	})
}

// WithAllowKeyDuplication sets whether deduplication is skipped for log attributes or other key-value collections.
//
// By default, the key-value collections within a log record are deduplicated to comply with the OpenTelemetry Specification.
// Deduplication means that if multiple key–value pairs with the same key are present, only a single pair
// is retained and others are discarded.
//
// Disabling deduplication with this option can improve performance e.g. of adding attributes to the log record.
//
// Note that if you disable deduplication, you are responsible for ensuring that duplicate
// key-value pairs within in a single collection are not emitted,
// or that the telemetry receiver can handle such duplicates.
func () LoggerProviderOption {
	return loggerProviderOptionFunc(func( providerConfig) providerConfig {
		.allowDupKeys = newSetting(true)
		return 
	})
}