gonum.org/v1/gonum@v0.14.0/graph/formats/rdf/debug.go (about)

     1  // Copyright ©2020 The Gonum 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  //go:build debug
     6  // +build debug
     7  
     8  package rdf
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"sort"
    14  	"strings"
    15  	"text/tabwriter"
    16  )
    17  
    18  type debugger bool
    19  
    20  const debug debugger = true
    21  
    22  func (d debugger) log(depth int, args ...interface{}) {
    23  	if !d {
    24  		return
    25  	}
    26  	fmt.Fprint(os.Stderr, strings.Repeat("\t", depth))
    27  	fmt.Fprintln(os.Stderr, args...)
    28  }
    29  
    30  func (d debugger) logf(depth int, format string, args ...interface{}) {
    31  	if !d {
    32  		return
    33  	}
    34  	fmt.Fprint(os.Stderr, strings.Repeat("\t", depth))
    35  	fmt.Fprintf(os.Stderr, format, args...)
    36  }
    37  
    38  func (d debugger) logHashes(depth int, hashes map[string][]byte, size int) {
    39  	if !d {
    40  		return
    41  	}
    42  	prefix := strings.Repeat("\t", depth)
    43  	if len(hashes) != 0 {
    44  		keys := make([]string, len(hashes))
    45  		i := 0
    46  		for k := range hashes {
    47  			keys[i] = k
    48  			i++
    49  		}
    50  		sort.Strings(keys)
    51  		w := tabwriter.NewWriter(os.Stderr, 0, 4, 8, ' ', 0)
    52  		for _, k := range keys {
    53  			fmt.Fprintf(w, prefix+"%s\t%0*x\n", k, 2*size, hashes[k])
    54  		}
    55  		w.Flush()
    56  	} else {
    57  		fmt.Fprintln(os.Stderr, prefix+"none")
    58  	}
    59  	fmt.Fprintln(os.Stderr)
    60  }
    61  
    62  func (d debugger) logParts(depth int, parts byLengthHash) {
    63  	if !d {
    64  		return
    65  	}
    66  	prefix := strings.Repeat("\t", depth)
    67  	if parts.Len() != 0 {
    68  		w := tabwriter.NewWriter(os.Stderr, 0, 4, 8, ' ', 0)
    69  		for i, p := range parts.nodes {
    70  			fmt.Fprintf(w, prefix+"%v\t%x\n", p, parts.hashes[i])
    71  		}
    72  		w.Flush()
    73  	} else {
    74  		fmt.Fprintln(os.Stderr, prefix+"none")
    75  	}
    76  	fmt.Fprintln(os.Stderr)
    77  }