2021-09-18 10:38:22 +00:00
|
|
|
package pxndscvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
useDebugChannel = false
|
2021-09-18 13:47:19 +00:00
|
|
|
debugChan chan string
|
|
|
|
debugMutex *sync.RWMutex
|
2021-09-18 10:38:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
debugMutex = &sync.RWMutex{}
|
|
|
|
}
|
|
|
|
|
2021-09-18 12:38:09 +00:00
|
|
|
// DebugChannel will return a channel which will receive debug messages once debug is enabled.
|
2021-09-18 10:38:22 +00:00
|
|
|
// This will alter the flow of debug messages, they will no longer print to console, they will be pushed into this channel.
|
|
|
|
// Make sure you pull from the channel eventually to avoid build up of blocked goroutines.
|
|
|
|
func (s *Swamp) DebugChannel() chan string {
|
2021-09-18 11:11:09 +00:00
|
|
|
debugMutex.Lock()
|
2021-09-18 12:38:09 +00:00
|
|
|
defer debugMutex.Unlock()
|
2021-09-18 10:38:22 +00:00
|
|
|
debugChan = make(chan string, 1000)
|
|
|
|
useDebugChannel = true
|
|
|
|
return debugChan
|
|
|
|
}
|
|
|
|
|
2021-09-18 13:47:19 +00:00
|
|
|
// DebugEnabled returns the current state of our debug switch
|
2021-09-18 12:38:09 +00:00
|
|
|
func (s *Swamp) DebugEnabled() bool {
|
|
|
|
debugMutex.RLock()
|
|
|
|
defer debugMutex.RUnlock()
|
2021-09-18 13:47:19 +00:00
|
|
|
return s.swampopt.debug
|
2021-09-18 12:38:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 10:38:22 +00:00
|
|
|
// DisableDebugChannel redirects debug messages back to the console.
|
|
|
|
// DisableProxyChannel does not disable debug, use DisableDebug().
|
2021-09-18 12:38:09 +00:00
|
|
|
func (s *Swamp) DisableDebugChannel() {
|
2021-09-18 11:11:09 +00:00
|
|
|
debugMutex.Lock()
|
|
|
|
defer debugMutex.Unlock()
|
2021-09-18 10:38:22 +00:00
|
|
|
useDebugChannel = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableDebug enables printing of verbose messages during operation
|
|
|
|
func (s *Swamp) EnableDebug() {
|
|
|
|
debugMutex.Lock()
|
|
|
|
defer debugMutex.Unlock()
|
2021-09-18 13:47:19 +00:00
|
|
|
s.swampopt.debug = true
|
2021-09-18 10:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DisableDebug enables printing of verbose messages during operation.
|
|
|
|
// WARNING: if you are using a DebugChannel, you must read all of the messages in the channel's cache or this will block.
|
|
|
|
func (s *Swamp) DisableDebug() {
|
|
|
|
debugMutex.Lock()
|
|
|
|
defer debugMutex.Unlock()
|
2021-09-18 13:47:19 +00:00
|
|
|
s.swampopt.debug = false
|
2021-09-18 10:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Swamp) dbgPrint(str string) {
|
|
|
|
debugMutex.RLock()
|
2021-09-18 11:11:09 +00:00
|
|
|
defer debugMutex.RUnlock()
|
2021-09-18 13:47:19 +00:00
|
|
|
if !s.swampopt.debug {
|
2021-09-18 10:38:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if useDebugChannel {
|
2021-09-20 01:23:18 +00:00
|
|
|
go func() {
|
|
|
|
debugChan <- str
|
|
|
|
}()
|
2021-09-18 10:38:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
println("pxndscvm: " + str)
|
|
|
|
}
|