// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.

// Initially copied from Thanos
//
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package logerrcapture

import (
	
	
	

	
)

// Logger interface compatible with go-kit/logger.
type Logger interface {
	Log(keyvals ...interface{}) error
}

type doFunc func() error

// Do is making sure we log every error, even those from best effort tiny functions.
func ( Logger,  doFunc,  string,  ...interface{}) {
	 := ()
	if  == nil {
		return
	}

	// For os closers, it's a common case to double close. From reliability purpose this is not a problem it may only indicate
	// surprising execution path.
	if errors.Is(, os.ErrClosed) {
		return
	}

	_ = .Log("msg", "detected do error", "err", errors.Wrap(, fmt.Sprintf(, ...)))
}

// ExhaustClose closes the io.ReadCloser with a log message on error but exhausts the reader before.
func ( Logger,  io.ReadCloser,  string,  ...interface{}) {
	,  := io.Copy(io.Discard, )
	if  != nil {
		_ = .Log("msg", "failed to exhaust reader, performance may be impeded", "err", )
	}

	Do(, .Close, , ...)
}