lineprinter

package
v0.7.0-master Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package lineprinter wraps a Print implementation to provide an io.WriteCloser.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LinePrinter

type LinePrinter struct {
	Print Print

	sync.Mutex
	// contains filtered or unexported fields
}

LinePrinter is an io.WriteCloser that buffers written bytes. During each Write, newline-terminated lines are removed from the buffer and passed to Print. On Close, any content remaining in the buffer is also passed to Print.

One use-case is connecting a subprocess's standard streams to a logger:

linePrinter := &linePrinter{
  Print: &Trimmer{WrappedPrint: logrus.StandardLogger().Debug}.Print,
}
defer linePrinter.Close()
cmd := exec.Command(...)
cmd.Stdout = linePrinter

LinePrinter buffers the subcommand's byte stream and splits it into lines for the logger. Sometimes we might have a partial line written to the buffer. We don't want to push that partial line into the logs if the next read attempt will pull in the remainder of the line. But we do want to push that partial line into the logs if there will never be a next read. So LinePrinter.Write pushes any complete lines to the wrapped printer, and LinePrinter.Close pushes any remaining partial line.

func (*LinePrinter) Close

func (lp *LinePrinter) Close() error

Close prints anything that remains in the buffer.

func (*LinePrinter) Write

func (lp *LinePrinter) Write(p []byte) (int, error)

Write writes len(p) bytes from p to an internal buffer. Then it retrieves any newline-terminated lines from the internal buffer and prints them with lp.Print. Partial lines are left in the internal buffer.

type Print

type Print func(args ...interface{})

Print is a type that can hold fmt.Print and other implementations which match that signature. For example, you can use:

trimmer := &lineprinter.Trimmer{WrappedPrint: logrus.StandardLogger().Debug}
linePrinter := &linePrinter{Print: trimmer.Print}

to connect the line printer to logrus at the debug level.

type Trimmer

type Trimmer struct {
	WrappedPrint Print
}

Trimmer is a Print wrapper that removes trailing newlines from the final argument (if it is a string argument). This is useful for connecting a LinePrinter to a logger whose Print-analog does not expect trailing newlines.

func (*Trimmer) Print

func (t *Trimmer) Print(args ...interface{})

Print removes trailing newlines from the final argument (if it is a string argument) and then passes the arguments through to WrappedPrint.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL