gorgonia.org/gorgonia@v0.9.17/cuda/debug.go (about)

     1  // +build debug
     2  
     3  package cuda
     4  
     5  import (
     6  	"fmt"
     7  	"log"
     8  	"os"
     9  	"strings"
    10  	"sync/atomic"
    11  )
    12  
    13  const DEBUG = true
    14  
    15  var TABCOUNT uint32
    16  var logger = log.New(os.Stderr, "", 0)
    17  var replacement = "\n"
    18  
    19  var (
    20  	allocatorDev = false
    21  )
    22  
    23  func tabcount() int {
    24  	return int(atomic.LoadUint32(&TABCOUNT))
    25  }
    26  
    27  func enterLogScope() {
    28  	atomic.AddUint32(&TABCOUNT, 1)
    29  	tabcount := tabcount()
    30  	logger.SetPrefix(strings.Repeat("\t", tabcount))
    31  	replacement = "\n" + strings.Repeat("\t", tabcount)
    32  }
    33  
    34  func leaveLogScope() {
    35  	tabcount := tabcount()
    36  	tabcount--
    37  
    38  	if tabcount < 0 {
    39  		atomic.StoreUint32(&TABCOUNT, 0)
    40  		tabcount = 0
    41  	} else {
    42  		atomic.StoreUint32(&TABCOUNT, uint32(tabcount))
    43  	}
    44  	logger.SetPrefix(strings.Repeat("\t", tabcount))
    45  	replacement = "\n" + strings.Repeat("\t", tabcount)
    46  }
    47  
    48  func logf(format string, others ...interface{}) {
    49  	if DEBUG {
    50  		// format = strings.Replace(format, "\n", replacement, -1)
    51  		s := fmt.Sprintf(format, others...)
    52  		s = strings.Replace(s, "\n", replacement, -1)
    53  		logger.Println(s)
    54  		// logger.Printf(format, others...)
    55  	}
    56  }
    57  
    58  func allocatorLogf(format string, attrs ...interface{}) {
    59  	if allocatorDev {
    60  		logf(format, attrs...)
    61  	}
    62  }