limitbuf

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: Apache-2.0 Imports: 3 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is like strings.Builder, but with maximum length. If the caller tries to add data beyond the capacity, they will be dropped, and Logger.String() will append "(truncated)" at the end.

TODO: Consider renaming to Builder or Buffer since this type's behavior is analogous to those.

func NewLogger

func NewLogger(maxLen int, opts ...LoggerOption) *Logger

NewLogger creates a new Logger object with the given capacity.

func (*Logger) String

func (b *Logger) String() string

String reports the data written so far. If the length of the data exceeds the buffer capacity, the prefix of the data, plus "(truncated)" will be reported.

func (*Logger) Write

func (b *Logger) Write(data []byte) (int, error)

Write implements io.Writer interface.

type LoggerOption added in v0.0.11

type LoggerOption func(*Logger)

LoggerOption is passed to NewLogger to configure a Logger.

func LogIfTruncatingMaxMultiple added in v0.0.11

func LogIfTruncatingMaxMultiple(m float64) LoggerOption

LogIfTruncatingMaxMultiple controls informative logging about how much data passed to Write has been truncated.

If zero, this logging is disabled. Otherwise, if the sum of len(data) passed to prior Write calls is greater than LogIfTruncatingMaxMultiple * maxLen (passed to NewLogger), a log message is written in the next call to String(). After logging, LogIfTruncatingMaxMultiple is set to zero to avoid repeating the same message.

This can be a useful diagnostic for both CPU and memory usage if a huge amount of data is written and only a tiny fraction is used. For example, if a caller writes to the log with fmt.Fprint(logger, ...) they may not realize that fmt.Fprint* actually buffers the *entire* formatted string in memory first, then writes it to logger. TODO: Consider serving the fmt use case better for e.g. bigslice.

Note that the log message is written to log.Error, not the Logger itself (it's not part of String's return).

Jump to

Keyboard shortcuts

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