github.com/parquet-go/parquet-go@v0.21.1-0.20240501160520-b3c3a0c3ed6f/internal/debug/debug.go (about)

     1  package debug
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"os"
     9  	"strconv"
    10  	"strings"
    11  )
    12  
    13  func ReaderAt(reader io.ReaderAt, prefix string) io.ReaderAt {
    14  	return &ioReaderAt{
    15  		reader: reader,
    16  		prefix: prefix,
    17  	}
    18  }
    19  
    20  type ioReaderAt struct {
    21  	reader io.ReaderAt
    22  	prefix string
    23  }
    24  
    25  func (d *ioReaderAt) ReadAt(b []byte, off int64) (int, error) {
    26  	n, err := d.reader.ReadAt(b, off)
    27  	fmt.Printf("%s: Read(%d) @%d => %d %v \n%s\n", d.prefix, len(b), off, n, err, hex.Dump(b[:n]))
    28  	return n, err
    29  }
    30  
    31  func Reader(reader io.Reader, prefix string) io.Reader {
    32  	return &ioReader{
    33  		reader: reader,
    34  		prefix: prefix,
    35  	}
    36  }
    37  
    38  type ioReader struct {
    39  	reader io.Reader
    40  	prefix string
    41  	offset int64
    42  }
    43  
    44  func (d *ioReader) Read(b []byte) (int, error) {
    45  	n, err := d.reader.Read(b)
    46  	fmt.Printf("%s: Read(%d) @%d => %d %v \n%s\n", d.prefix, len(b), d.offset, n, err, hex.Dump(b[:n]))
    47  	d.offset += int64(n)
    48  	return n, err
    49  }
    50  
    51  func Writer(writer io.Writer, prefix string) io.Writer {
    52  	return &ioWriter{
    53  		writer: writer,
    54  		prefix: prefix,
    55  	}
    56  }
    57  
    58  type ioWriter struct {
    59  	writer io.Writer
    60  	prefix string
    61  	offset int64
    62  }
    63  
    64  func (d *ioWriter) Write(b []byte) (int, error) {
    65  	n, err := d.writer.Write(b)
    66  	fmt.Printf("%s: Write(%d) @%d => %d %v \n  %q\n", d.prefix, len(b), d.offset, n, err, b[:n])
    67  	d.offset += int64(n)
    68  	return n, err
    69  }
    70  
    71  var (
    72  	TRACEBUF int
    73  )
    74  
    75  func init() {
    76  	for _, arg := range strings.Split(os.Getenv("PARQUETGODEBUG"), ",") {
    77  		k := arg
    78  		v := ""
    79  		i := strings.IndexByte(arg, '=')
    80  		if i >= 0 {
    81  			k, v = arg[:i], arg[i+1:]
    82  		}
    83  		var err error
    84  		switch k {
    85  		case "":
    86  			// ignore empty entries
    87  		case "tracebuf":
    88  			if TRACEBUF, err = strconv.Atoi(v); err != nil {
    89  				log.Printf("PARQUETGODEBUG: invalid value for tracebuf: %q", v)
    90  			}
    91  		default:
    92  			log.Printf("PARQUETGODEBUG: unrecognized debug option: %q", k)
    93  		}
    94  	}
    95  }