rwc.go 748 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package debug
  2. import (
  3. "io"
  4. "github.com/miolini/datacounter"
  5. )
  6. type RWC struct {
  7. io.Reader
  8. io.Writer
  9. c io.Closer
  10. }
  11. func WrapRWC(c io.ReadWriteCloser) io.ReadWriteCloser {
  12. rl := NewReadLogger("<", c)
  13. wl := NewWriteLogger(">", c)
  14. return &RWC{
  15. Reader: rl,
  16. Writer: wl,
  17. c: c,
  18. }
  19. }
  20. func (c *RWC) Close() error {
  21. return c.c.Close()
  22. }
  23. type Counter struct {
  24. io.Reader
  25. io.Writer
  26. c io.Closer
  27. Cr *datacounter.ReaderCounter
  28. Cw *datacounter.WriterCounter
  29. }
  30. func WrapCounter(c io.ReadWriteCloser) *Counter {
  31. rc := datacounter.NewReaderCounter(c)
  32. wc := datacounter.NewWriterCounter(c)
  33. return &Counter{
  34. Reader: rc,
  35. Writer: wc,
  36. c: c,
  37. Cr: rc,
  38. Cw: wc,
  39. }
  40. }
  41. func (c *Counter) Close() error {
  42. return c.c.Close()
  43. }