github.com/kamalshkeir/kencoding@v0.0.2-0.20230409043843-44b609a0475a/thrift/debug.go (about)

     1  package thrift
     2  
     3  import (
     4  	"io"
     5  	"log"
     6  )
     7  
     8  func NewDebugReader(r Reader, l *log.Logger) Reader {
     9  	return &debugReader{
    10  		r: r,
    11  		l: l,
    12  	}
    13  }
    14  
    15  func NewDebugWriter(w Writer, l *log.Logger) Writer {
    16  	return &debugWriter{
    17  		w: w,
    18  		l: l,
    19  	}
    20  }
    21  
    22  type debugReader struct {
    23  	r Reader
    24  	l *log.Logger
    25  }
    26  
    27  func (d *debugReader) log(method string, res interface{}, err error) {
    28  	if err != nil {
    29  		d.l.Printf("(%T).%s() → ERROR: %v", d.r, method, err)
    30  	} else {
    31  		d.l.Printf("(%T).%s() → %#v", d.r, method, res)
    32  	}
    33  }
    34  
    35  func (d *debugReader) Protocol() Protocol {
    36  	return d.r.Protocol()
    37  }
    38  
    39  func (d *debugReader) Reader() io.Reader {
    40  	return d.r.Reader()
    41  }
    42  
    43  func (d *debugReader) ReadBool() (bool, error) {
    44  	v, err := d.r.ReadBool()
    45  	d.log("ReadBool", v, err)
    46  	return v, err
    47  }
    48  
    49  func (d *debugReader) ReadInt8() (int8, error) {
    50  	v, err := d.r.ReadInt8()
    51  	d.log("ReadInt8", v, err)
    52  	return v, err
    53  }
    54  
    55  func (d *debugReader) ReadInt16() (int16, error) {
    56  	v, err := d.r.ReadInt16()
    57  	d.log("ReadInt16", v, err)
    58  	return v, err
    59  }
    60  
    61  func (d *debugReader) ReadInt32() (int32, error) {
    62  	v, err := d.r.ReadInt32()
    63  	d.log("ReadInt32", v, err)
    64  	return v, err
    65  }
    66  
    67  func (d *debugReader) ReadInt64() (int64, error) {
    68  	v, err := d.r.ReadInt64()
    69  	d.log("ReadInt64", v, err)
    70  	return v, err
    71  }
    72  
    73  func (d *debugReader) ReadFloat64() (float64, error) {
    74  	v, err := d.r.ReadFloat64()
    75  	d.log("ReadFloat64", v, err)
    76  	return v, err
    77  }
    78  
    79  func (d *debugReader) ReadBytes() ([]byte, error) {
    80  	v, err := d.r.ReadBytes()
    81  	d.log("ReadBytes", v, err)
    82  	return v, err
    83  }
    84  
    85  func (d *debugReader) ReadString() (string, error) {
    86  	v, err := d.r.ReadString()
    87  	d.log("ReadString", v, err)
    88  	return v, err
    89  }
    90  
    91  func (d *debugReader) ReadLength() (int, error) {
    92  	v, err := d.r.ReadLength()
    93  	d.log("ReadLength", v, err)
    94  	return v, err
    95  }
    96  
    97  func (d *debugReader) ReadMessage() (Message, error) {
    98  	v, err := d.r.ReadMessage()
    99  	d.log("ReadMessage", v, err)
   100  	return v, err
   101  }
   102  
   103  func (d *debugReader) ReadField() (Field, error) {
   104  	v, err := d.r.ReadField()
   105  	d.log("ReadField", v, err)
   106  	return v, err
   107  }
   108  
   109  func (d *debugReader) ReadList() (List, error) {
   110  	v, err := d.r.ReadList()
   111  	d.log("ReadList", v, err)
   112  	return v, err
   113  }
   114  
   115  func (d *debugReader) ReadSet() (Set, error) {
   116  	v, err := d.r.ReadSet()
   117  	d.log("ReadSet", v, err)
   118  	return v, err
   119  }
   120  
   121  func (d *debugReader) ReadMap() (Map, error) {
   122  	v, err := d.r.ReadMap()
   123  	d.log("ReadMap", v, err)
   124  	return v, err
   125  }
   126  
   127  type debugWriter struct {
   128  	w Writer
   129  	l *log.Logger
   130  }
   131  
   132  func (d *debugWriter) log(method string, arg interface{}, err error) {
   133  	if err != nil {
   134  		d.l.Printf("(%T).%s(%#v) → ERROR: %v", d.w, method, arg, err)
   135  	} else {
   136  		d.l.Printf("(%T).%s(%#v)", d.w, method, arg)
   137  	}
   138  }
   139  
   140  func (d *debugWriter) Protocol() Protocol {
   141  	return d.w.Protocol()
   142  }
   143  
   144  func (d *debugWriter) Writer() io.Writer {
   145  	return d.w.Writer()
   146  }
   147  
   148  func (d *debugWriter) WriteBool(v bool) error {
   149  	err := d.w.WriteBool(v)
   150  	d.log("WriteBool", v, err)
   151  	return err
   152  }
   153  
   154  func (d *debugWriter) WriteInt8(v int8) error {
   155  	err := d.w.WriteInt8(v)
   156  	d.log("WriteInt8", v, err)
   157  	return err
   158  }
   159  
   160  func (d *debugWriter) WriteInt16(v int16) error {
   161  	err := d.w.WriteInt16(v)
   162  	d.log("WriteInt16", v, err)
   163  	return err
   164  }
   165  
   166  func (d *debugWriter) WriteInt32(v int32) error {
   167  	err := d.w.WriteInt32(v)
   168  	d.log("WriteInt32", v, err)
   169  	return err
   170  }
   171  
   172  func (d *debugWriter) WriteInt64(v int64) error {
   173  	err := d.w.WriteInt64(v)
   174  	d.log("WriteInt64", v, err)
   175  	return err
   176  }
   177  
   178  func (d *debugWriter) WriteFloat64(v float64) error {
   179  	err := d.w.WriteFloat64(v)
   180  	d.log("WriteFloat64", v, err)
   181  	return err
   182  }
   183  
   184  func (d *debugWriter) WriteBytes(v []byte) error {
   185  	err := d.w.WriteBytes(v)
   186  	d.log("WriteBytes", v, err)
   187  	return err
   188  }
   189  
   190  func (d *debugWriter) WriteString(v string) error {
   191  	err := d.w.WriteString(v)
   192  	d.log("WriteString", v, err)
   193  	return err
   194  }
   195  
   196  func (d *debugWriter) WriteLength(n int) error {
   197  	err := d.w.WriteLength(n)
   198  	d.log("WriteLength", n, err)
   199  	return err
   200  }
   201  
   202  func (d *debugWriter) WriteMessage(m Message) error {
   203  	err := d.w.WriteMessage(m)
   204  	d.log("WriteMessage", m, err)
   205  	return err
   206  }
   207  
   208  func (d *debugWriter) WriteField(f Field) error {
   209  	err := d.w.WriteField(f)
   210  	d.log("WriteField", f, err)
   211  	return err
   212  }
   213  
   214  func (d *debugWriter) WriteList(l List) error {
   215  	err := d.w.WriteList(l)
   216  	d.log("WriteList", l, err)
   217  	return err
   218  }
   219  
   220  func (d *debugWriter) WriteSet(s Set) error {
   221  	err := d.w.WriteSet(s)
   222  	d.log("WriteSet", s, err)
   223  	return err
   224  }
   225  
   226  func (d *debugWriter) WriteMap(m Map) error {
   227  	err := d.w.WriteMap(m)
   228  	d.log("WriteMap", m, err)
   229  	return err
   230  }