diff --git a/logger.go b/logger.go index ca2d20a..991ea75 100644 --- a/logger.go +++ b/logger.go @@ -55,7 +55,7 @@ func (c *Client) logStdErr() { func CreateClient() *Client { var client Client client.initialized = true - client.writer = make(LogWriter, 100) + client.writer = make(LogWriter, 1000) sliceTex.Lock() clients = append(clients, &client) sliceTex.Unlock() @@ -66,6 +66,7 @@ func Flush() { cleanup.Do(func() { close(stderrClient.writer) <-stderrFinished + stderrClient.Destroy() }) } @@ -100,7 +101,13 @@ func createLog(e Entry) { func(c *Client, e Entry) { select { case c.writer <- e: + // try to clear out one of the older entries default: + select { + case <-c.writer: + c.writer <- e + default: + } } }(c, e) } @@ -210,6 +217,7 @@ func Panic(args ...interface{}) { // falls through to default below } } + Flush() panic(errors.New(output)) } @@ -224,6 +232,7 @@ func Fatal(args ...interface{}) { level: LFatal, } createLog(e) + Flush() os.Exit(1) } diff --git a/logger_test.go b/logger_test.go index 80053a9..bcdfa52 100644 --- a/logger_test.go +++ b/logger_test.go @@ -1,17 +1,23 @@ package lambo_log_socket import ( + "strconv" + "sync" "testing" ) +// Test CreateClient() and Client.Destroy() func TestCreateDestroy(t *testing.T) { + // Ensure only stderr exists at the beginning if len(clients) != 1 { t.Errorf("Expected 1 client, but found %d", len(clients)) } + // Create a new client, ensure it's added c := CreateClient() if len(clients) != 2 { t.Errorf("Expected 2 clients, but found %d", len(clients)) } + // Destroy it and ensure it's actually removed from the array c.Destroy() if len(clients) != 1 { t.Errorf("Expected 1 client, but found %d", len(clients)) @@ -31,18 +37,31 @@ func TestSetLogLevel(t *testing.T) { c.Destroy() } -// Trace prints out logs on trace level -func TestTrace(t *testing.T) { - testString := "Testing trace!" +func BenchmarkDebugSerial(b *testing.B) { + c := CreateClient() + var x sync.WaitGroup + x.Add(b.N) + for i := 0; i < b.N; i++ { + Debug(i) + go func() { + c.Get() + x.Done() + }() + } + x.Wait() + c.Destroy() +} + +// Trace ensure logs come out in the right order +func TestOrder(t *testing.T) { + testString := "Testing trace: " var c *Client c = CreateClient() c.SetLogLevel(LTrace) - for i := 0; i < 5; i++ { - Trace(testString) - } - for i := 0; i < 5; i++ { - if testString != c.Get().Output { + for i := 0; i < 5000; i++ { + Trace(testString + strconv.Itoa(i)) + if testString+strconv.Itoa(i) != c.Get().Output { t.Error("Trace input doesn't match output") } }