package rlog

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
Jianhui Zhao
2018-10-15 23:02:13 +08:00
parent c65e8e8011
commit bbb1812405
4 changed files with 49 additions and 25 deletions
+1
View File
@@ -25,6 +25,7 @@ import (
"github.com/buger/jsonparser"
"github.com/gorilla/websocket"
"github.com/zhaojh329/rttys/rlog"
)
const RTTY_MESSAGE_VERSION = 2
+1
View File
@@ -28,6 +28,7 @@ import (
"time"
"github.com/gorilla/websocket"
"github.com/zhaojh329/rttys/rlog"
)
const (
+1
View File
@@ -33,6 +33,7 @@ import (
"time"
"github.com/rakyll/statik/fs"
"github.com/zhaojh329/rttys/rlog"
_ "github.com/zhaojh329/rttys/statik"
)
+46 -25
View File
@@ -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)
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...)
}