Skip to content

Commit

Permalink
Support log to file when run in background
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed Jun 15, 2018
1 parent 5bb230c commit d735d04
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 19 deletions.
13 changes: 6 additions & 7 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package main

import (
"log"
"time"
"strconv"
"math/rand"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
7 changes: 3 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package main

import (
"log"
"time"
"sync"
"errors"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
15 changes: 7 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package main
import (
"os"
"fmt"
"log"
"sync"
"flag"
"time"
Expand Down Expand Up @@ -106,22 +105,22 @@ 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)
}

flag.Parse()

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
}

Expand Down Expand Up @@ -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))
}
}
62 changes: 62 additions & 0 deletions rlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 (
"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)

0 comments on commit d735d04

Please sign in to comment.