mirror of
https://github.com/taigrr/log-socket
synced 2025-01-18 04:53:14 -08:00
Remove mutexes, allow the GC to take care of things
This commit is contained in:
parent
15a9017e2e
commit
9ff091038a
37
logger.go
37
logger.go
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@ -21,7 +22,8 @@ const (
|
||||
)
|
||||
|
||||
var logger = logrus.New()
|
||||
var clients []Client
|
||||
var clients []*Client
|
||||
var sliceTex sync.Mutex
|
||||
|
||||
func init() {
|
||||
stderrClient := CreateClient()
|
||||
@ -30,22 +32,40 @@ func init() {
|
||||
}
|
||||
|
||||
func (c *Client) logStdErr() {
|
||||
for {
|
||||
select {
|
||||
case entry := <-c.writer:
|
||||
case entry, more := <-c.writer:
|
||||
if c.LogLevel >= entry.level {
|
||||
fmt.Println(entry.Output)
|
||||
}
|
||||
if !more {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CreateClient() *Client {
|
||||
var client Client
|
||||
client.writer = make(LogWriter, 10)
|
||||
clients = append(clients, client)
|
||||
client.writer = make(LogWriter, 100)
|
||||
sliceTex.Lock()
|
||||
clients = append(clients, &client)
|
||||
sliceTex.Unlock()
|
||||
return &client
|
||||
}
|
||||
|
||||
func (c *Client) Destroy() error {
|
||||
var otherClients []*Client
|
||||
sliceTex.Lock()
|
||||
c.writer = nil
|
||||
c = nil
|
||||
for _, x := range clients {
|
||||
if x != nil {
|
||||
otherClients = append(otherClients, x)
|
||||
}
|
||||
}
|
||||
clients = otherClients
|
||||
sliceTex.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -54,11 +74,16 @@ func (c *Client) GetLogLevel() Level {
|
||||
}
|
||||
|
||||
func createLog(e Entry) {
|
||||
sliceTex.Lock()
|
||||
for _, c := range clients {
|
||||
go func(c Client, e Entry) {
|
||||
c.writer <- e
|
||||
go func(c *Client, e Entry) {
|
||||
select {
|
||||
case c.writer <- e:
|
||||
default:
|
||||
}
|
||||
}(c, e)
|
||||
}
|
||||
sliceTex.Unlock()
|
||||
}
|
||||
|
||||
// SetLogLevel set log level of logger
|
||||
|
@ -4,10 +4,15 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateDestroy(t *testing.T) {
|
||||
c := CreateClient()
|
||||
c.Destroy()
|
||||
}
|
||||
|
||||
// SetLogLevel set log level of logger
|
||||
func TestSetLogLevel(t *testing.T) {
|
||||
logLevels := [...]Level{LTrace, LDebug, LInfo, LWarn, LError, LPanic, LFatal}
|
||||
var c Client
|
||||
c := CreateClient()
|
||||
for _, x := range logLevels {
|
||||
c.SetLogLevel(x)
|
||||
if c.GetLogLevel() != x {
|
||||
|
Loading…
x
Reference in New Issue
Block a user