prox5/debug.go

82 lines
1.9 KiB
Go
Raw Normal View History

2022-05-23 01:05:50 +00:00
package prox5
import (
2022-06-26 02:11:01 +00:00
"fmt"
"sync"
)
2022-06-26 02:11:01 +00:00
func init() {
debugMutex = &sync.RWMutex{}
}
var (
useDebugChannel = false
debugChan chan string
debugMutex *sync.RWMutex
)
2022-06-26 02:11:01 +00:00
type DebugPrinter interface {
Print(str string)
Printf(format string, items ...any)
}
2022-06-26 02:11:01 +00:00
type basicPrinter struct{}
func (b basicPrinter) Print(str string) {
println("prox5: " + str)
}
func (b basicPrinter) Printf(format string, items ...any) {
println(fmt.Sprintf("prox5: "+format, items))
}
// DebugChannel will return a channel which will receive debug messages once debug is enabled.
// 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 {
2022-06-26 02:11:01 +00:00
debugChan = make(chan string, 1000000)
useDebugChannel = true
return debugChan
}
2022-06-26 02:11:01 +00:00
// DebugEnabled returns the current state of our debug switch.
func (s *Swamp) DebugEnabled() bool {
return s.swampopt.debug.Load().(bool)
}
// DisableDebugChannel redirects debug messages back to the console.
// DisableProxyChannel does not disable debug, use DisableDebug().
func (s *Swamp) DisableDebugChannel() {
2021-09-18 11:11:09 +00:00
debugMutex.Lock()
defer debugMutex.Unlock()
useDebugChannel = false
}
// EnableDebug enables printing of verbose messages during operation
func (s *Swamp) EnableDebug() {
2022-06-26 02:11:01 +00:00
s.swampopt.debug.Store(true)
}
// 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() {
2022-06-26 02:11:01 +00:00
s.swampopt.debug.Store(false)
}
func (s *Swamp) dbgPrint(str string) {
2022-06-26 02:11:01 +00:00
if !s.swampopt.debug.Load().(bool) {
return
}
if useDebugChannel {
2021-09-23 15:12:05 +00:00
select {
case debugChan <- str:
return
default:
2022-06-26 02:11:01 +00:00
println("prox5 debug overflow: " + str)
2021-09-23 15:12:05 +00:00
return
}
}
2022-06-26 02:11:01 +00:00
}