From d735d04db5cffbdda2228bbbf0975d9def35a2d7 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Fri, 15 Jun 2018 21:19:20 +0800 Subject: [PATCH] Support log to file when run in background Signed-off-by: Jianhui Zhao --- broker.go | 13 ++++++------ client.go | 7 +++---- main.go | 15 +++++++------- rlog.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 rlog.go diff --git a/broker.go b/broker.go index 267084b..974c411 100644 --- a/broker.go +++ b/broker.go @@ -20,7 +20,6 @@ package main import ( - "log" "time" "strconv" "math/rand" @@ -104,7 +103,7 @@ func (br *Broker) newSession(user *Client) bool { }) dev.wsWrite(websocket.BinaryMessage, msg) - log.Println("New session:", sid) + rlog.Println("New session:", sid) return true } else { // Write to user @@ -116,7 +115,7 @@ func (br *Broker) newSession(user *Client) bool { }) user.wsWrite(websocket.BinaryMessage, msg) - log.Println("Device", devid, "offline") + rlog.Println("Device", devid, "offline") return false } } @@ -125,7 +124,7 @@ func delSession(sessions map[string]*Session, sid string) { if session, ok := sessions[sid]; ok { delete(sessions, sid) session.user.wsClose() - log.Println("Delete session: ", sid) + rlog.Println("Delete session: ", sid) if session.dev != nil { msg := RttyMessageInit(&rtty.RttyMessage{ @@ -181,12 +180,12 @@ func (br *Broker) run() { case client := <- br.join: if client.isDev { if _, ok := br.devices[client.devid]; ok { - log.Println("ID conflicting:", client.devid) + rlog.Println("ID conflicting:", client.devid) client.wsClose(); } else { client.isJoined = true br.devices[client.devid] = client - log.Printf("New device:id('%s'), description('%s')", client.devid, client.description) + rlog.Printf("New device:id('%s'), description('%s')", client.devid, client.description) } } else { // From user browse @@ -199,7 +198,7 @@ func (br *Broker) run() { client.wsClose() if dev, ok := br.devices[client.devid]; ok { - log.Printf("Dead device:id('%s'), description('%s')", dev.devid, dev.description) + rlog.Printf("Dead device:id('%s'), description('%s')", dev.devid, dev.description) delete(br.devices, dev.devid) } diff --git a/client.go b/client.go index 53ae602..6dc1d3f 100644 --- a/client.go +++ b/client.go @@ -20,7 +20,6 @@ package main import ( - "log" "time" "sync" "errors" @@ -112,7 +111,7 @@ func (c *Client) readPump() { msgType, data, err := c.conn.ReadMessage() if err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { - log.Printf("error: %v", err) + rlog.Printf("error: %v", err) } break } @@ -160,13 +159,13 @@ func (c *Client) writePump() { func serveWs(br *Broker, w http.ResponseWriter, r *http.Request) { devid := r.URL.Query().Get("devid") if devid == "" { - log.Println("devid required") + rlog.Println("devid required") return } conn, err := upgrader.Upgrade(w, r, nil) if err != nil { - log.Println(err) + rlog.Println(err) return } diff --git a/main.go b/main.go index 481ac1e..fa9c382 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,6 @@ package main import ( "os" "fmt" - "log" "sync" "flag" "time" @@ -106,7 +105,7 @@ func main() { key := flag.String("key", "", "keyFile Path") if syscall.Getuid() != 0 { - log.Println("Operation not permitted") + rlog.Println("Operation not permitted") os.Exit(1) } @@ -114,14 +113,14 @@ func main() { rand.Seed(time.Now().Unix()) - log.Println("rttys version:", rttys_version()) + rlog.Println("rttys version:", rttys_version()) br := newBroker() go br.run() statikFS, err := fs.New() if err != nil { - log.Fatal(err) + rlog.Fatal(err) return } @@ -196,10 +195,10 @@ func main() { }) if *cert != "" && *key != "" { - log.Println("Listen on: ", *port, "SSL on") - log.Fatal(http.ListenAndServeTLS(":" + strconv.Itoa(*port), *cert, *key, nil)) + rlog.Println("Listen on: ", *port, "SSL on") + rlog.Fatal(http.ListenAndServeTLS(":" + strconv.Itoa(*port), *cert, *key, nil)) } else { - log.Println("Listen on: ", *port, "SSL off") - log.Fatal(http.ListenAndServe(":" + strconv.Itoa(*port), nil)) + rlog.Println("Listen on: ", *port, "SSL off") + rlog.Fatal(http.ListenAndServe(":" + strconv.Itoa(*port), nil)) } } diff --git a/rlog.go b/rlog.go new file mode 100644 index 0000000..24f2f9e --- /dev/null +++ b/rlog.go @@ -0,0 +1,62 @@ +/* + * 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 ( + "os" + "fmt" + "log" + "github.com/mattn/go-isatty" +) + +type RttyLog struct { + file string +} + +const LOG_FILE = "/var/log/rtty.log" + +func (l *RttyLog) Write(b []byte) (n int, err error) { + if isatty.IsTerminal(os.Stdout.Fd()) { + fmt.Fprintf(os.Stderr, "%s", b) + return 0, nil + } + + if l.file == "" { + return 0, nil + } + + file, err := os.OpenFile(l.file, os.O_CREATE | os.O_WRONLY | os.O_APPEND, 0666) + if err != nil { + return 0, nil + } + + st, _ := file.Stat() + if st.Size() > 1024 * 1024 { + file.Truncate(0) + } + + defer file.Close() + + fmt.Fprintf(file, "%s", b) + + return 0, nil +} + +var rlog = log.New(&RttyLog{file: LOG_FILE}, "", log.LstdFlags) \ No newline at end of file