Use logrus

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
Jianhui Zhao
2019-04-22 23:33:22 +08:00
parent 665d2007fe
commit ddead9042d
5 changed files with 29 additions and 82 deletions
+10 -10
View File
@@ -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:
+3 -3
View File
@@ -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
}
+5 -5
View File
@@ -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
}
}
-59
View File
@@ -1,59 +0,0 @@
/*
* Copyright (C) 2017 Jianhui Zhao <jianhuizhao329@gmail.com>
*
* 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)
}
+11 -5
View File
@@ -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))
}
}