github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/stream/audit_writer.go (about)

     1  package stream
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // Auditor is a callback type that receives written byte counts from write
     8  // operations. Audiot implementations should be fast and minimal to avoid any
     9  // impact on performance.
    10  type Auditor func(uint64)
    11  
    12  // auditWriter is an io.Writer that implements write operation auditing.
    13  type auditWriter struct {
    14  	// writer is the underlying writer.
    15  	writer io.Writer
    16  	// auditor is the auditing callback.
    17  	auditor Auditor
    18  }
    19  
    20  // NewAuditWriter creates a new io.Writer that invokes an auditing callback with
    21  // written byte counts. If auditor is nil, then this function will return writer
    22  // unmodified.
    23  func NewAuditWriter(writer io.Writer, auditor Auditor) io.Writer {
    24  	if auditor == nil {
    25  		return writer
    26  	}
    27  	return &auditWriter{writer, auditor}
    28  }
    29  
    30  // Write implements io.Writer.Write.
    31  func (w *auditWriter) Write(buffer []byte) (int, error) {
    32  	result, err := w.writer.Write(buffer)
    33  	w.auditor(uint64(result))
    34  	return result, err
    35  }