diagio

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 3 Imported by: 7

README

diagio

Go Reference Go Report Card

The Go package diagio implements I/O wrappers which can be useful for debugging and other diagnostics purposes.

CounterReader implements an io.Reader which keeps track of bytes read from it.

CounterWriter implements an io.Writer which keeps track of bytes written to it.

DiagReader implements an io.Reader which produces diagnostics output (number of bytes, current position, error, hex dump of data) whenever it is read from.

DiagWriter implements an io.Writer which produces diagnostics output (number of bytes, current position, error, hex dump of data) whenever it is written to.

Documentation:

https://pkg.go.dev/github.com/snabb/diagio

Simple example:

	cw := diagio.NewCounterWriter(os.Stdout)
	fmt.Fprintln(cw, "hello world")
	fmt.Println(cw.Count())
	// Output:
	// hello world
	// 12

The Git repository is located at: https://github.com/snabb/diagio

License

MIT

Documentation

Overview

Package diagio implements I/O wrappers which can be useful for debugging and other diagnostics purposes.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count(i interface{}) (n int64)

Count takes anything and calls Count() on it and returns the result.

Returns -1 if Count() method does not exist.

This is useful for passing around an ordinary io.Reader or an ordinary io.Writer and later calling Count() on it.

Types

type CounterReader

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

CounterReader implements io.Reader. Count of bytes read is tracked.

Example
package main

import (
	"bytes"
	"fmt"
	"github.com/snabb/diagio"
	"io"
	"io/ioutil"
)

func main() {
	input1 := bytes.NewBufferString("hello world")
	input2 := diagio.NewCounterReader(input1)
	io.Copy(ioutil.Discard, input2)
	fmt.Println(input2.Count())
}
Output:
11

func NewCounterReader

func NewCounterReader(r io.Reader) (cr *CounterReader)

NewCounterReader wraps io.Reader and returns CounterReader

func (*CounterReader) Count

func (cr *CounterReader) Count() (n int64)

Count returns the number of bytes read from the Reader.

func (*CounterReader) Read

func (cr *CounterReader) Read(p []byte) (n int, err error)

Read calls Read on the wrapped io.Reader and adds the number of bytes read to the counter.

type CounterWriter

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

CounterWriter implements io.Writer. Count of bytes written is tracked.

Example
package main

import (
	"fmt"
	"github.com/snabb/diagio"
	"os"
)

func main() {
	cw := diagio.NewCounterWriter(os.Stdout)
	fmt.Fprintln(cw, "hello world")
	fmt.Println(cw.Count())
}
Output:
hello world
12

func NewCounterWriter

func NewCounterWriter(w io.Writer) (cw *CounterWriter)

NewCounterWriter wraps io.Writer and returns CounterWriter.

func (*CounterWriter) Count

func (cw *CounterWriter) Count() (n int64)

Count returns the number of bytes written to the Writer.

func (*CounterWriter) Write

func (cw *CounterWriter) Write(p []byte) (n int, err error)

Write calls Write on the wrapped io.Writer and adds the number of bytes written to the counter.

type DiagReader

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

DiagReader implements io.Reader. Diagnostics output is produced whenever it is read from.

Example
package main

import (
	"bytes"
	"github.com/snabb/diagio"
	"io"
	"io/ioutil"
	"os"
)

func main() {
	input1 := bytes.NewBufferString("hello world")
	input2 := diagio.NewDiagReader(input1, os.Stdout)
	io.Copy(ioutil.Discard, input2)
}
Output:
>>> Read 8192 bytes at 0 (start) ===
00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64                 |hello world|
<<< Read 11 bytes (err = <nil>) (end) ===
>>> Read 8192 bytes at 11 (start) ===
<<< Read 0 bytes (err = EOF) (end) ===

func NewDiagReader

func NewDiagReader(r io.Reader, diagOutput io.Writer) (dr *DiagReader)

NewDiagReader wraps io.Reader and returns DiagReader.

func (*DiagReader) PrintHex

func (dr *DiagReader) PrintHex(v bool)

PrintHex enables or disables hex dump output. The default is enabled.

func (*DiagReader) PrintInfo

func (dr *DiagReader) PrintInfo(v bool)

PrintInfo enables or disables output of number of bytes read, stream position and possible error code. The default is enabled.

func (*DiagReader) Read

func (dr *DiagReader) Read(p []byte) (n int, err error)

Read calls Read on the wrapped io.Reader and produces diagnostics output.

type DiagWriter

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

DiagWriter implements io.Writer. Diagnostics output is produced whenever it is written to.

Example
package main

import (
	"fmt"
	"github.com/snabb/diagio"
	"io/ioutil"
	"os"
)

func main() {
	output := diagio.NewDiagWriter(ioutil.Discard, os.Stdout)
	fmt.Fprint(output, "hello world")
}
Output:
>>> Write 11 bytes at 0 (start) ===
00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64                 |hello world|
<<< Wrote 11 bytes (err = <nil>) (end) ===

func NewDiagWriter

func NewDiagWriter(w io.Writer, diagOutput io.Writer) (dw *DiagWriter)

NewDiagWriter wraps io.Writer and returns DiagWriter.

func (*DiagWriter) PrintHex

func (dw *DiagWriter) PrintHex(v bool)

PrintHex enables or disables hex dump output. The default is enabled.

func (*DiagWriter) PrintInfo

func (dw *DiagWriter) PrintInfo(v bool)

PrintInfo enables or disables output of number of bytes written, stream position and possible error code. The default is enabled.

func (*DiagWriter) Write

func (dw *DiagWriter) Write(p []byte) (n int, err error)

Write calls Write on the wrapped io.Writer and produces diagnostics output.

Jump to

Keyboard shortcuts

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