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 ¶
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.
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.