From ddead9042d4ce3cd985dd8b91d55d8c173e690f1 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Mon, 22 Apr 2019 23:33:22 +0800 Subject: [PATCH] Use logrus Signed-off-by: Jianhui Zhao --- broker.go | 20 +++++++++---------- client.go | 6 +++--- device.go | 10 +++++----- log.go | 59 ------------------------------------------------------- main.go | 16 ++++++++++----- 5 files changed, 29 insertions(+), 82 deletions(-) delete mode 100644 log.go diff --git a/broker.go b/broker.go index 23f5597..c366d3c 100644 --- a/broker.go +++ b/broker.go @@ -21,10 +21,10 @@ package main import ( "fmt" - "time" - "github.com/buger/jsonparser" "github.com/gorilla/websocket" + log "github.com/sirupsen/logrus" + "time" ) const ( @@ -69,7 +69,7 @@ func (br *Broker) newSession(user *User) bool { if dev, ok := br.devices[devid]; ok { devsid := dev.getFreeSid() if devsid < 1 { - log.Println("Not found available devsid") + log.Warn("Not found available devsid") return false } @@ -82,13 +82,13 @@ func (br *Broker) newSession(user *User) bool { // Notify the device to create a pty and associate it with a session id dev.wsWrite(websocket.TextMessage, []byte(msg)) - log.Println("New session:", sid) + log.Info("New session:", sid) return true } else { // Notify the user that the device is offline msg := `{"type":"login","err":1,"msg":"offline"}` user.wsWrite(websocket.TextMessage, []byte(msg)) - log.Println("Device", devid, "offline") + log.Info("Device", devid, "offline") return false } } @@ -98,24 +98,24 @@ func (br *Broker) run() { select { case dev := <-br.connecting: if _, ok := br.devices[dev.devid]; ok { - log.Println("ID conflicting:", dev.devid) + log.Warn("ID conflicting:", dev.devid) dev.Close() } else { br.devices[dev.devid] = dev - log.Println("New device:", dev.devid) + log.Info("New device:", dev.devid) } case dev := <-br.disconnecting: if dev, ok := br.devices[dev.devid]; ok { delete(br.devices, dev.devid) - log.Println("Died device:", dev.devid) + log.Info("Died device:", dev.devid) for sid, session := range br.sessions { if session.dev.devid == dev.devid { session.user.Close() delete(br.sessions, sid) - log.Println("Delete session: ", sid) + log.Info("Delete session: ", sid) } } } @@ -137,7 +137,7 @@ func (br *Broker) run() { delete(br.sessions, sid) delete(session.dev.sessions, devsid) - log.Println("Delete session: ", sid) + log.Info("Delete session: ", sid) } case msg := <-br.inDevMessage: diff --git a/client.go b/client.go index 65ffefd..d161dcf 100644 --- a/client.go +++ b/client.go @@ -20,12 +20,12 @@ package main import ( + "github.com/gorilla/websocket" + log "github.com/sirupsen/logrus" "net/http" "strconv" "sync" "time" - - "github.com/gorilla/websocket" ) var upgrader = websocket.Upgrader{ @@ -95,7 +95,7 @@ func serveWs(br *Broker, w http.ResponseWriter, r *http.Request) { if devid == "" { conn.Close() - log.Println("devid required") + log.Error("devid required") return } diff --git a/device.go b/device.go index ef38422..64b3ecc 100644 --- a/device.go +++ b/device.go @@ -20,9 +20,9 @@ package main import ( - "time" - "github.com/gorilla/websocket" + log "github.com/sirupsen/logrus" + "time" ) const ( @@ -92,7 +92,7 @@ func (dev *Device) keepAlive(keepalive int64) { case <-ticker.C: now := time.Now().Unix() if now-last > keepalive { - log.Printf("Inactive device in long time, now kill it: %s\n", dev.devid) + log.Error("Inactive device in long time, now kill it: %s", dev.devid) return } } @@ -106,7 +106,7 @@ func (dev *Device) readAlway() { msgType, data, err := dev.ws.ReadMessage() if err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { - log.Printf("error: %v", err) + log.Errorf("error: %v", err) } break } @@ -116,7 +116,7 @@ func (dev *Device) readAlway() { select { case dev.br.inDevMessage <- msg: case <-dev.closeChan: - log.Println("closeChan from readAlway") + log.Error("closeChan from readAlway") return } } diff --git a/log.go b/log.go deleted file mode 100644 index 0fdfe2d..0000000 --- a/log.go +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2017 Jianhui Zhao - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 - * USA - */ - -package main - -import ( - "fmt" - slog "log" - "os" - - "github.com/mattn/go-isatty" -) - -type RttysLog struct { - file string -} - -var log = LogInit() - -const logFile = "/var/log/rttys.log" - -func (l *RttysLog) Write(b []byte) (n int, err error) { - if isatty.IsTerminal(os.Stdout.Fd()) { - return fmt.Fprintf(os.Stderr, "%s", b) - } - - file, err := os.OpenFile(l.file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) - if err != nil { - return 0, nil - } - defer file.Close() - - st, _ := file.Stat() - if st.Size() > 1024*1024 { - file.Truncate(0) - } - - return fmt.Fprintf(file, "%s", b) -} - -func LogInit() *slog.Logger { - return slog.New(&RttysLog{logFile}, "", slog.LstdFlags) -} diff --git a/main.go b/main.go index 5128722..c3f1871 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,8 @@ import ( "github.com/kylelemons/go-gypsy/yaml" "github.com/rakyll/statik/fs" + "github.com/rifflock/lfshook" + log "github.com/sirupsen/logrus" _ "github.com/zhaojh329/rttys/statik" ) @@ -56,16 +58,20 @@ const MAX_SESSION_TIME = 30 * time.Minute var httpSessions sync.Map +func init() { + log.AddHook(lfshook.NewHook("/var/log/rttys.log", &log.TextFormatter{})) +} + func main() { cfg := parseConfig() if !checkUser() && cfg.username == "" { - log.Println("Operation not permitted. Please start as root or define Username and Password in configuration file") + log.Error("Operation not permitted. Please start as root or define Username and Password in configuration file") os.Exit(1) } - log.Printf("go version: %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH) - log.Println("rttys version:", rttys_version()) + log.Infof("go version: %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH) + log.Info("rttys version:", rttys_version()) br := newBroker() go br.run() @@ -145,10 +151,10 @@ func main() { }) if cfg.cert != "" && cfg.key != "" { - log.Println("Listen on: ", cfg.addr, "SSL on") + log.Info("Listen on: ", cfg.addr, "SSL on") log.Fatal(http.ListenAndServeTLS(cfg.addr, cfg.cert, cfg.key, nil)) } else { - log.Println("Listen on: ", cfg.addr, "SSL off") + log.Info("Listen on: ", cfg.addr, "SSL off") log.Fatal(http.ListenAndServe(cfg.addr, nil)) } }