package ioutil

import "go.abhg.dev/io/ioutil"

Package ioutil contains extensions for the io package.

Index

Functions

func LineWriter

func LineWriter(write func([]byte)) (w io.Writer, done func())

LineWriter returns an io.Writer that will feed its writes through to the provided write function, one line at a time, not including the newline character.

The done function must be called when no more writes are expected. This will flush any buffered writes to the write function.

The returned io.Writer is NOT safe for concurrent use. Wrap it with SyncWriter to use it in a concurrent setting.

func PrintfWriter

func PrintfWriter(printf func(string, ...any), prefix string) (w io.Writer, done func())

PrintfWriter builds an io.Writer that will feed its writes through to the provided printf-style function, one line at a time, not including the newline character.

The provided prefix will be prepended to each line written. Leave prefix empty to write lines as is.

The done function must be called when no more writes are expected. This will flush any buffered writes to the write function.

Use this to redirect the output of an io.Writer into a logger, or other printf-style function. For example:

w, done := ioutil.PrintfWriter(myLogger.Printf)
defer done()

cmd := exec.Command("some", "--long", "--running", "command")
cmd.Stdout = w
cmd.Stderr = w
err := cmd.Run()

The returned io.Writer is NOT safe for concurrent use. Wrap it with SyncWriter to use it in a concurrent setting.

func SyncWriter

func SyncWriter(w io.Writer) io.Writer

SyncWriter makes an io.Writer safe for concurrent use by adding a mutex around the Write calls.

func TestLogWriter

func TestLogWriter(t TestLogger, prefix string) io.Writer

TestLogWriter builds an io.Writer that will feed its writes through to the given test log.

Prefix is prepended to each line written. Leave prefix empty to write lines as is.

The returned io.Writer is NOT safe for concurrent use. Wrap it with SyncWriter to use it in a concurrent setting.

Types

type TestLogger

type TestLogger interface {
	Logf(string, ...any)
	Cleanup(func())
}

TestLogger is a subset of the testing.TB interface with support for logging messages and cleaning up resources.