log_debug.go raw

   1  //go:build debug
   2  
   3  package nostr
   4  
   5  import (
   6  	"fmt"
   7  
   8  	jsoniter "github.com/json-iterator/go"
   9  )
  10  
  11  func debugLogf(str string, args ...any) {
  12  	// this is such that we don't modify the actual args that may be used outside of this function
  13  	printableArgs := make([]any, len(args))
  14  
  15  	for i, v := range args {
  16  		printableArgs[i] = stringify(v)
  17  	}
  18  
  19  	DebugLogger.Printf(str, printableArgs...)
  20  }
  21  
  22  func stringify(anything any) any {
  23  	switch v := anything.(type) {
  24  	case []any:
  25  		// this is such that we don't modify the actual values that may be used outside of this function
  26  		printableValues := make([]any, len(v))
  27  		for i, subv := range v {
  28  			printableValues[i] = stringify(subv)
  29  		}
  30  		return printableValues
  31  	case []jsoniter.RawMessage:
  32  		j, _ := json.Marshal(v)
  33  		return string(j)
  34  	case []byte:
  35  		return string(v)
  36  	case fmt.Stringer:
  37  		return v.String()
  38  	default:
  39  		return v
  40  	}
  41  }
  42