From bbb18124054d42e0368f0cc984141dec56b6cf2d Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Mon, 15 Oct 2018 23:02:13 +0800 Subject: [PATCH] package rlog Signed-off-by: Jianhui Zhao --- broker.go | 1 + client.go | 1 + main.go | 1 + rlog.go => rlog/log.go | 71 +++++++++++++++++++++++++++--------------- 4 files changed, 49 insertions(+), 25 deletions(-) rename rlog.go => rlog/log.go (54%) diff --git a/broker.go b/broker.go index 5e2d9f0..b0fa920 100644 --- a/broker.go +++ b/broker.go @@ -25,6 +25,7 @@ import ( "github.com/buger/jsonparser" "github.com/gorilla/websocket" + "github.com/zhaojh329/rttys/rlog" ) const RTTY_MESSAGE_VERSION = 2 diff --git a/client.go b/client.go index d85cc5f..aade524 100644 --- a/client.go +++ b/client.go @@ -28,6 +28,7 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/zhaojh329/rttys/rlog" ) const ( diff --git a/main.go b/main.go index fc18252..e36a8cf 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,7 @@ import ( "time" "github.com/rakyll/statik/fs" + "github.com/zhaojh329/rttys/rlog" _ "github.com/zhaojh329/rttys/statik" ) diff --git a/rlog.go b/rlog/log.go similarity index 54% rename from rlog.go rename to rlog/log.go index 24f2f9e..f07410f 100644 --- a/rlog.go +++ b/rlog/log.go @@ -17,46 +17,67 @@ * USA */ -package main +package rlog import ( - "os" - "fmt" - "log" - "github.com/mattn/go-isatty" + "fmt" + "log" + "os" + + "github.com/mattn/go-isatty" ) type RttyLog struct { - file string + file string } const LOG_FILE = "/var/log/rtty.log" +var rLog *log.Logger + 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 isatty.IsTerminal(os.Stdout.Fd()) { + fmt.Fprintf(os.Stderr, "%s", b) + return 0, nil + } - if l.file == "" { - 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 - } + 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) - } + st, _ := file.Stat() + if st.Size() > 1024*1024 { + file.Truncate(0) + } - defer file.Close() + defer file.Close() - fmt.Fprintf(file, "%s", b) + fmt.Fprintf(file, "%s", b) - return 0, nil + return 0, nil } -var rlog = log.New(&RttyLog{file: LOG_FILE}, "", log.LstdFlags) \ No newline at end of file +func init() { + rLog = log.New(&RttyLog{LOG_FILE}, "", log.LstdFlags) +} + +func Print(v ...interface{}) { + rLog.Print(v...) +} + +func Println(v ...interface{}) { + rLog.Println(v...) +} + +func Printf(format string, v ...interface{}) { + rLog.Printf(format, v...) +} + +func Fatal(v ...interface{}) { + rLog.Fatal(v...) +}