refactor logging listener

This commit is contained in:
Bradley Cicenas 2017-02-04 08:59:33 +00:00
parent 03789b8ed3
commit 2b279efc4e

View File

@ -1,6 +1,7 @@
package logging package logging
import ( import (
"fmt"
"net" "net"
"time" "time"
@ -8,7 +9,7 @@ import (
) )
const ( const (
size = 64 size = 1024
path = "/tmp/ctop.sock" path = "/tmp/ctop.sock"
) )
@ -19,6 +20,29 @@ var format = logging.MustStringFormatter(
type CTopLogger struct { type CTopLogger struct {
*logging.Logger *logging.Logger
backend *logging.MemoryBackend backend *logging.MemoryBackend
done chan bool
}
func New(serverEnabled string) *CTopLogger {
log := &CTopLogger{
logging.MustGetLogger("ctop"),
logging.NewMemoryBackend(size),
make(chan bool),
}
backendFmt := logging.NewBackendFormatter(log.backend, format)
logging.SetBackend(backendFmt)
log.Info("logger initialized")
if serverEnabled == "1" {
log.Serve()
}
return log
}
func (log *CTopLogger) Exit() {
log.done <- true
} }
func (log *CTopLogger) Serve() { func (log *CTopLogger) Serve() {
@ -27,18 +51,33 @@ func (log *CTopLogger) Serve() {
panic(err) panic(err)
} }
for { go func() {
conn, err := ln.Accept() switch {
if err != nil { case <-log.done:
panic(err) ln.Close()
return
default:
//
} }
go log.handler(conn) }()
}
go func() {
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
go log.handler(conn)
}
}()
log.Info("logging server started")
} }
func (log *CTopLogger) handler(conn net.Conn) { func (log *CTopLogger) handler(conn net.Conn) {
defer conn.Close() defer conn.Close()
for msg := range log.tail() { for msg := range log.tail() {
msg = fmt.Sprintf("%s\n", msg)
conn.Write([]byte(msg)) conn.Write([]byte(msg))
} }
} }
@ -63,20 +102,3 @@ func (log *CTopLogger) tail() chan string {
return stream return stream
} }
func New(serverEnabled string) *CTopLogger {
log := &CTopLogger{
logging.MustGetLogger("ctop"),
logging.NewMemoryBackend(size),
}
logging.SetBackend(logging.NewBackendFormatter(log.backend, format))
log.Info("initialized logging")
if serverEnabled == "1" {
go log.Serve()
}
return log
}