mirror of
https://github.com/gl-inet/glkvm-cloud.git
synced 2026-09-20 01:25:22 +00:00
Support log to file when run in background
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user