commit 00e8415d8eafb7fc4d75b6786706ef861b253849 Author: Shelikhoo xiaokangwang@outlook.com Date: Wed Feb 2 11:35:12 2022 +0000
Add verbosity switch to suppress diagnostic output --- proxy/lib/pt_event_logger.go | 9 ++++++--- proxy/main.go | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/proxy/lib/pt_event_logger.go b/proxy/lib/pt_event_logger.go index b0dbf60..7990a44 100644 --- a/proxy/lib/pt_event_logger.go +++ b/proxy/lib/pt_event_logger.go @@ -2,14 +2,16 @@ package snowflake_proxy
import ( "git.torproject.org/pluggable-transports/snowflake.git/v2/common/task" + "io" "log" "time"
"git.torproject.org/pluggable-transports/snowflake.git/v2/common/event" )
-func NewProxyEventLogger(logPeriod time.Duration) event.SnowflakeEventReceiver { - el := &logEventLogger{logPeriod: logPeriod} +func NewProxyEventLogger(logPeriod time.Duration, output io.Writer) event.SnowflakeEventReceiver { + logger := log.New(output, "", log.LstdFlags|log.LUTC) + el := &logEventLogger{logPeriod: logPeriod, logger: logger} el.task = &task.Periodic{Interval: logPeriod, Execute: el.logTick} el.task.Start() return el @@ -21,6 +23,7 @@ type logEventLogger struct { connectionCount int logPeriod time.Duration task *task.Periodic + logger *log.Logger }
func (p *logEventLogger) OnNewSnowflakeEvent(e event.SnowflakeEvent) { @@ -36,7 +39,7 @@ func (p *logEventLogger) OnNewSnowflakeEvent(e event.SnowflakeEvent) { func (p *logEventLogger) logTick() error { inbound, inboundUnit := formatTraffic(p.inboundSum) outbound, outboundUnit := formatTraffic(p.inboundSum) - log.Printf("In the last %v, there are %v connections. Traffic Relayed ↑ %v %v, ↓ %v %v.\n", + p.logger.Printf("In the last %v, there are %v connections. Traffic Relayed ↑ %v %v, ↓ %v %v.\n", p.logPeriod.String(), p.connectionCount, inbound, inboundUnit, outbound, outboundUnit) p.outboundSum = 0 p.inboundSum = 0 diff --git a/proxy/main.go b/proxy/main.go index de31913..7d025ea 100644 --- a/proxy/main.go +++ b/proxy/main.go @@ -4,6 +4,7 @@ import ( "flag" "git.torproject.org/pluggable-transports/snowflake.git/v2/common/event" "io" + "io/ioutil" "log" "os" "time" @@ -24,15 +25,12 @@ func main() { "the time interval in second before NAT type is retested, 0s disables retest. Valid time units are "s", "m", "h". ") SummaryInterval := flag.Duration("summary-interval", time.Hour, "the time interval to output summary, 0s disables retest. Valid time units are "s", "m", "h". ") + verboseLogging := flag.Bool("verbose", false, "increase log verbosity")
flag.Parse()
- periodicEventLogger := sf.NewProxyEventLogger(*SummaryInterval) - eventLogger := event.NewSnowflakeEventDispatcher()
- eventLogger.AddSnowflakeEventListener(periodicEventLogger) - proxy := sf.SnowflakeProxy{ Capacity: uint(*capacity), STUNURL: *stunURL, @@ -45,16 +43,21 @@ func main() { }
var logOutput io.Writer = os.Stderr + var eventlogOutput io.Writer = os.Stderr log.SetFlags(log.LstdFlags | log.LUTC)
- log.SetFlags(log.LstdFlags | log.LUTC) + if !*verboseLogging { + logOutput = ioutil.Discard + } + if *logFilename != "" { f, err := os.OpenFile(*logFilename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) if err != nil { log.Fatal(err) } defer f.Close() - logOutput = io.MultiWriter(os.Stderr, f) + logOutput = io.MultiWriter(logOutput, f) + eventlogOutput = io.MultiWriter(eventlogOutput, f) } if *unsafeLogging { log.SetOutput(logOutput) @@ -62,6 +65,9 @@ func main() { log.SetOutput(&safelog.LogScrubber{Output: logOutput}) }
+ periodicEventLogger := sf.NewProxyEventLogger(*SummaryInterval, eventlogOutput) + eventLogger.AddSnowflakeEventListener(periodicEventLogger) + err := proxy.Start() if err != nil { log.Fatal(err)