From a5999adb8db57607bfba4b6bcc6cdd0a77e3fd49 Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 25 Oct 2018 13:35:21 +0200 Subject: [PATCH] add basic circolog-tail client --- cmd/circolog-tail/main.go | 69 +++++++++++++++++++++++++++++++++++++++ format.go | 2 -- 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 cmd/circolog-tail/main.go diff --git a/cmd/circolog-tail/main.go b/cmd/circolog-tail/main.go new file mode 100644 index 0000000..d5d9d9a --- /dev/null +++ b/cmd/circolog-tail/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/url" + "os" + "os/signal" + "time" + + "github.com/gorilla/websocket" +) + +func main() { + addr := flag.String("addr", "localhost:9080", "http service address") + flag.Parse() + fmt.Println("vim-go") + u := url.URL{Scheme: "ws", Host: *addr, Path: "/ws"} + log.Printf("connecting to %s", u.String()) + + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt) + + c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) + if err != nil { + log.Fatal("dial:", err) + } + defer c.Close() + + done := make(chan struct{}) + + go func() { + defer close(done) + for { + _, message, err := c.ReadMessage() + if err != nil { + log.Println("close:", err) + return + } + fmt.Println(string(message)) + } + }() + + for { + select { + case <-done: + return + case <-interrupt: + log.Println("interrupt") + + // Cleanly close the connection by sending a close message and then waiting (with timeout) for the + // server to close the connection. + err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + if err != nil { + log.Println("write close:", err) + return + } + select { + case <-done: + log.Println("Successfully close") + case <-time.After(1 * time.Second): + log.Println("Forced close") + } + return + } + } + +} diff --git a/format.go b/format.go index 6a1e083..90237ce 100644 --- a/format.go +++ b/format.go @@ -2,7 +2,6 @@ package circolog import ( "bytes" - "fmt" "text/template" "time" @@ -34,7 +33,6 @@ func init() { // FormatSyslog format a message in the typical format used in /var/log/messages func FormatSyslog(msg format.LogParts) string { - fmt.Printf("%#v\n", msg["timestamp"]) var buf bytes.Buffer syslogTmpl.Execute(&buf, msg) return buf.String()