package logrus

import (
	
	
	
	
)

// Writer at INFO level. See WriterLevel for details.
func ( *Logger) () *io.PipeWriter {
	return .WriterLevel(InfoLevel)
}

// WriterLevel returns an io.Writer that can be used to write arbitrary text to
// the logger at the given log level. Each line written to the writer will be
// printed in the usual way using formatters and hooks. The writer is part of an
// io.Pipe and it is the callers responsibility to close the writer when done.
// This can be used to override the standard library logger easily.
func ( *Logger) ( Level) *io.PipeWriter {
	return NewEntry().WriterLevel()
}

// Writer returns an io.Writer that writes to the logger at the info log level
func ( *Entry) () *io.PipeWriter {
	return .WriterLevel(InfoLevel)
}

// WriterLevel returns an io.Writer that writes to the logger at the given log level
func ( *Entry) ( Level) *io.PipeWriter {
	,  := io.Pipe()

	 := .Print

	// Determine which log function to use based on the specified log level
	switch  {
	case TraceLevel:
		 = .Trace
	case DebugLevel:
		 = .Debug
	case InfoLevel:
		 = .Info
	case WarnLevel:
		 = .Warn
	case ErrorLevel:
		 = .Error
	case FatalLevel:
		 = .Fatal
	case PanicLevel:
		 = .Panic
	}

	// Start a new goroutine to scan the input and write it to the logger using the specified print function.
	// It splits the input into chunks of up to 64KB to avoid buffer overflows.
	go .writerScanner(, )

	// Set a finalizer function to close the writer when it is garbage collected
	runtime.SetFinalizer(, writerFinalizer)

	return 
}

// writerScanner scans the input from the reader and writes it to the logger
func ( *Entry) ( *io.PipeReader,  func( ...any)) {
	 := bufio.NewScanner()

	// Set the buffer size to the maximum token size to avoid buffer overflows
	.Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)

	// Define a split function to split the input into chunks of up to 64KB
	 := bufio.MaxScanTokenSize // 64KB
	 := func( []byte,  bool) (int, []byte, error) {
		if len() >=  {
			return , [:], nil
		}

		return bufio.ScanLines(, )
	}

	// Use the custom split function to split the input
	.Split()

	// Scan the input and write it to the logger using the specified print function
	for .Scan() {
		(strings.TrimRight(.Text(), "\r\n"))
	}

	// If there was an error while scanning the input, log an error
	if  := .Err();  != nil {
		.Errorf("Error while reading from Writer: %s", )
	}

	// Close the reader when we are done
	.Close()
}

// WriterFinalizer is a finalizer function that closes then given writer when it is garbage collected
func writerFinalizer( *io.PipeWriter) {
	.Close()
}