package internal

import (
	
	
	
	
	
)

// TODO (ned): Revisit logging
// Add more standardized approach with log levels and configurability

type Logging interface {
	Printf(ctx context.Context, format string, v ...interface{})
}

type DefaultLogger struct {
	log       *log.Logger
	calldepth int
}

func ( *DefaultLogger) ( context.Context,  string,  ...interface{}) {
	_ = .log.Output(.calldepth, fmt.Sprintf(, ...))
}

// Calldepth values for NewDefaultLogger. Reads through Logger.Printf add one
// forwarding frame (atomicLogger.Printf), so the plain default logger uses
// DefaultLoggerCalldepth; wrappers that add another Printf frame (e.g.
// logging's filterLogger) use FilterLoggerCalldepth.
const (
	DefaultLoggerCalldepth = 3
	FilterLoggerCalldepth  = 4
)

// NewDefaultLogger returns a stderr logger that attributes each line to the
// frame calldepth levels up the stack.
func ( int) Logging {
	return &DefaultLogger{
		log:       log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile),
		calldepth: ,
	}
}

// atomicLogger holds the active Logging behind an atomic pointer so
// redis.SetLogger, logging.Enable and logging.Disable can swap it while pool
// and background goroutines read it through Printf. The interface is stored via
// a pointer rather than atomic.Value, whose single-concrete-type rule the
// swappable implementations (DefaultLogger, VoidLogger, custom loggers) break.
type atomicLogger struct {
	v atomic.Pointer[Logging]
}

func ( *atomicLogger) ( Logging) { .v.Store(&) }

func ( *atomicLogger) () Logging {
	if  := .v.Load();  != nil {
		return *
	}
	return nil
}

func ( *atomicLogger) ( context.Context,  string,  ...interface{}) {
	if  := .Load();  != nil {
		.Printf(, , ...)
	}
}

func newAtomicLogger( Logging) *atomicLogger {
	 := &atomicLogger{}
	.Store()
	return 
}

// Logger calls Output to print to the stderr.
// Arguments are handled in the manner of fmt.Print.
// Swap it with redis.SetLogger; read it through Logger.Printf.
var Logger = newAtomicLogger(NewDefaultLogger(DefaultLoggerCalldepth))

// atomicLogLevel stores the active level as an int32 so redis.SetLogLevel can
// change it while the level guards (isHealthyConn on the Get path, the
// maintnotifications loggers) read it through the *OrAbove helpers.
type atomicLogLevel struct {
	v atomic.Int32
}

func ( *atomicLogLevel) ( LogLevelT) { .v.Store(int32()) }
func ( *atomicLogLevel) () LogLevelT   { return LogLevelT(.v.Load()) }

func ( *atomicLogLevel) () bool  { return .Load().WarnOrAbove() }
func ( *atomicLogLevel) () bool  { return .Load().InfoOrAbove() }
func ( *atomicLogLevel) () bool { return .Load().DebugOrAbove() }

func newAtomicLogLevel( LogLevelT) *atomicLogLevel {
	 := &atomicLogLevel{}
	.Store()
	return 
}

var LogLevel = newAtomicLogLevel(LogLevelError)

// LogLevelT represents the logging level
type LogLevelT int

// Log level constants for the entire go-redis library
const (
	LogLevelError LogLevelT = iota // 0 - errors only
	LogLevelWarn                   // 1 - warnings and errors
	LogLevelInfo                   // 2 - info, warnings, and errors
	LogLevelDebug                  // 3 - debug, info, warnings, and errors
)

// String returns the string representation of the log level
func ( LogLevelT) () string {
	switch  {
	case LogLevelError:
		return "ERROR"
	case LogLevelWarn:
		return "WARN"
	case LogLevelInfo:
		return "INFO"
	case LogLevelDebug:
		return "DEBUG"
	default:
		return "UNKNOWN"
	}
}

// IsValid returns true if the log level is valid
func ( LogLevelT) () bool {
	return  >= LogLevelError &&  <= LogLevelDebug
}

func ( LogLevelT) () bool {
	return  >= LogLevelWarn
}

func ( LogLevelT) () bool {
	return  >= LogLevelInfo
}

func ( LogLevelT) () bool {
	return  >= LogLevelDebug
}