feat: log output in ws mode

This commit is contained in:
zhangwenjian
2020-08-20 23:31:46 +08:00
parent a2f1721ee1
commit 2c8c7688b4
3 changed files with 125 additions and 47 deletions
+50
View File
@@ -2,13 +2,16 @@ package tools
import (
"archive/zip"
"bufio"
"bytes"
"context"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
)
func PathExists(path string) (bool, error) {
@@ -158,3 +161,50 @@ func (h ReplaceHelper) walkCallback(path string, f os.FileInfo, err error) error
return err
}
func FileMonitoring(filePth string, hookfn func([]byte)) {
f, err := os.Open(filePth)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
rd := bufio.NewReader(f)
f.Seek(0, 2)
for {
line, err := rd.ReadBytes('\n')
// 如果是文件末尾不返回
if err == io.EOF {
time.Sleep(500 * time.Millisecond)
continue
} else if err != nil {
log.Fatalln(err)
}
go hookfn(line)
}
}
func FileMonitoringById(ctx context.Context, filePth string, id string, group string, hookfn func(context.Context, string, string, []byte)) {
f, err := os.Open(filePth)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
rd := bufio.NewReader(f)
f.Seek(0, 2)
for {
if ctx.Err() != nil {
break
}
line, err := rd.ReadBytes('\n')
// 如果是文件末尾不返回
if err == io.EOF {
time.Sleep(500 * time.Millisecond)
continue
} else if err != nil {
log.Fatalln(err)
}
go hookfn(ctx, id, group, line)
}
}