...
Run Format

Source file src/testing/iotest/logger.go

Documentation: testing/iotest

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package iotest
     6  
     7  import (
     8  	"io"
     9  	"log"
    10  )
    11  
    12  type writeLogger struct {
    13  	prefix string
    14  	w      io.Writer
    15  }
    16  
    17  func (l *writeLogger) Write(p []byte) (n int, err error) {
    18  	n, err = l.w.Write(p)
    19  	if err != nil {
    20  		log.Printf("%s %x: %v", l.prefix, p[0:n], err)
    21  	} else {
    22  		log.Printf("%s %x", l.prefix, p[0:n])
    23  	}
    24  	return
    25  }
    26  
    27  // NewWriteLogger returns a writer that behaves like w except
    28  // that it logs (using log.Printf) each write to standard error,
    29  // printing the prefix and the hexadecimal data written.
    30  func NewWriteLogger(prefix string, w io.Writer) io.Writer {
    31  	return &writeLogger{prefix, w}
    32  }
    33  
    34  type readLogger struct {
    35  	prefix string
    36  	r      io.Reader
    37  }
    38  
    39  func (l *readLogger) Read(p []byte) (n int, err error) {
    40  	n, err = l.r.Read(p)
    41  	if err != nil {
    42  		log.Printf("%s %x: %v", l.prefix, p[0:n], err)
    43  	} else {
    44  		log.Printf("%s %x", l.prefix, p[0:n])
    45  	}
    46  	return
    47  }
    48  
    49  // NewReadLogger returns a reader that behaves like r except
    50  // that it logs (using log.Printf) each read to standard error,
    51  // printing the prefix and the hexadecimal data read.
    52  func NewReadLogger(prefix string, r io.Reader) io.Reader {
    53  	return &readLogger{prefix, r}
    54  }
    55  

View as plain text