From 2fdf68e876ac9753a9409499980b4542d1295ca9 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Fri, 17 Feb 2017 17:48:38 +1100 Subject: [PATCH] move logging server into own file, improve cleanup --- logging/main.go | 48 ++++---------------------------------- logging/server.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 +- 3 files changed, 65 insertions(+), 44 deletions(-) create mode 100644 logging/server.go diff --git a/logging/main.go b/logging/main.go index 98ddd0c..cea31b0 100644 --- a/logging/main.go +++ b/logging/main.go @@ -1,9 +1,6 @@ package logging import ( - "fmt" - "net" - "sync" "time" "github.com/op/go-logging" @@ -11,12 +8,10 @@ import ( const ( size = 1024 - path = "/tmp/ctop.sock" ) var ( Log *CTopLogger - wg sync.WaitGroup exited bool level = logging.INFO format = logging.MustStringFormatter( @@ -47,44 +42,6 @@ func Init() *CTopLogger { return Log } -func (log *CTopLogger) Exit() { - exited = true - wg.Wait() -} - -func (log *CTopLogger) StartServer() { - ln, err := net.Listen("unix", path) - if err != nil { - panic(err) - } - - go func() { - for { - conn, err := ln.Accept() - if err != nil { - if err, ok := err.(net.Error); ok && err.Temporary() { - continue - } - return - } - go log.handler(conn) - } - }() - - log.Notice("logging server started") -} - -func (log *CTopLogger) handler(conn net.Conn) { - wg.Add(1) - defer wg.Done() - defer conn.Close() - for msg := range log.tail() { - msg = fmt.Sprintf("%s\n", msg) - conn.Write([]byte(msg)) - } - conn.Write([]byte("bye\n")) -} - func (log *CTopLogger) tail() chan string { stream := make(chan string) @@ -109,3 +66,8 @@ func (log *CTopLogger) tail() chan string { return stream } + +func (log *CTopLogger) Exit() { + exited = true + StopServer() +} diff --git a/logging/server.go b/logging/server.go new file mode 100644 index 0000000..11fed00 --- /dev/null +++ b/logging/server.go @@ -0,0 +1,59 @@ +package logging + +import ( + "fmt" + "net" + "sync" +) + +const ( + path = "/tmp/ctop.sock" +) + +var server struct { + wg sync.WaitGroup + ln net.Listener +} + +func getListener() net.Listener { + ln, err := net.Listen("unix", path) + if err != nil { + panic(err) + } + return ln +} + +func StartServer() { + server.ln = getListener() + + go func() { + for { + conn, err := server.ln.Accept() + if err != nil { + if err, ok := err.(net.Error); ok && err.Temporary() { + continue + } + return + } + go handler(conn) + } + }() + + Log.Notice("logging server started") +} + +func StopServer() { + server.wg.Wait() + server.ln.Close() +} + +func handler(conn net.Conn) { + server.wg.Add(1) + defer server.wg.Done() + defer conn.Close() + for msg := range Log.tail() { + msg = fmt.Sprintf("%s\n", msg) + conn.Write([]byte(msg)) + } + conn.Write([]byte("bye\n")) +} diff --git a/main.go b/main.go index 784d522..fb60aec 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ func main() { config.Init() log = logging.Init() if config.GetSwitchVal("loggingEnabled") { - log.StartServer() + logging.StartServer() } if err := ui.Init(); err != nil { panic(err)