1
0
mirror of https://github.com/taigrr/log-socket synced 2025-01-18 04:53:14 -08:00

Adds Flush() to panic and Fatal calls

This commit is contained in:
Tai Groot 2021-04-08 20:42:04 -07:00
parent 617b1466ce
commit 537840de8b
Signed by: taigrr
GPG Key ID: D00C269A87614812
2 changed files with 37 additions and 9 deletions

View File

@ -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)
}

View File

@ -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")
}
}