From 44d18c3af63c12adf01d026cdfe7b35f3803ce1a Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Wed, 5 Oct 2016 17:56:25 -0600 Subject: [PATCH 1/2] [FIXED] SetLogger to be able set debug/trace to 0 In NATS Streaming, we have a test - started early on - that was testing logging and called SetLogger(l, true, true), then reset by calling SetLogger(l, false, false) to reset the values. That obviously had not the expected effect. During profiling, I noticed that there were tons of allocated objects due to NATS server debug/trace statements caused by that. --- server/log.go | 8 ++++++-- server/log_test.go | 12 +++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/server/log.go b/server/log.go index a7a29966..1bccc46c 100644 --- a/server/log.go +++ b/server/log.go @@ -37,13 +37,17 @@ type Logger interface { // SetLogger sets the logger of the server func (s *Server) SetLogger(logger Logger, debugFlag, traceFlag bool) { + debugVal := 0 if debugFlag { - atomic.StoreInt32(&debug, 1) + debugVal = 1 } + atomic.StoreInt32(&debug, int32(debugVal)) + traceVal := 0 if traceFlag { - atomic.StoreInt32(&trace, 1) + traceVal = 1 } + atomic.StoreInt32(&trace, int32(traceVal)) log.Lock() log.logger = logger diff --git a/server/log_test.go b/server/log_test.go index 0d952810..587f5c2b 100644 --- a/server/log_test.go +++ b/server/log_test.go @@ -8,7 +8,8 @@ import ( func TestSetLogger(t *testing.T) { server := &Server{} - server.SetLogger(&DummyLogger{}, true, true) + dl := &DummyLogger{} + server.SetLogger(dl, true, true) // We assert that the logger has change to the DummyLogger _ = log.logger.(*DummyLogger) @@ -20,6 +21,15 @@ func TestSetLogger(t *testing.T) { if trace != 1 { t.Fatalf("Expected trace 1, received value %d\n", trace) } + + // Make sure that we can reset to fal + server.SetLogger(dl, false, false) + if debug != 0 { + t.Fatalf("Expected debug 0, got %v", debug) + } + if trace != 0 { + t.Fatalf("Expected trace 0, got %v", trace) + } } type DummyLogger struct{} From 9f758bbd0db9402f23d690aebdae6f4cb78e6782 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Thu, 6 Oct 2016 09:30:49 -0600 Subject: [PATCH 2/2] Use simple if/else --- server/log.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/server/log.go b/server/log.go index 1bccc46c..04db93cf 100644 --- a/server/log.go +++ b/server/log.go @@ -37,17 +37,16 @@ type Logger interface { // SetLogger sets the logger of the server func (s *Server) SetLogger(logger Logger, debugFlag, traceFlag bool) { - debugVal := 0 if debugFlag { - debugVal = 1 + atomic.StoreInt32(&debug, 1) + } else { + atomic.StoreInt32(&debug, 0) } - atomic.StoreInt32(&debug, int32(debugVal)) - - traceVal := 0 if traceFlag { - traceVal = 1 + atomic.StoreInt32(&trace, 1) + } else { + atomic.StoreInt32(&trace, 0) } - atomic.StoreInt32(&trace, int32(traceVal)) log.Lock() log.logger = logger