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