Commit 17cf2994 by lichenggang

优化代码

parent deeff793
......@@ -2,16 +2,17 @@ package main
import (
"code"
)
const (
Directory = "E:\\test_logs" // directory of the log
TestPort = 9997
"os"
)
// example
func main() {
Directory := "E:\\test_logs" // directory of the log
TestPort := 9997
isproduct := os.Getenv("GOLOGENV")
if isproduct == "TRUE" {
Directory = ""
}
code.See(Directory, TestPort)
}
......@@ -7,15 +7,13 @@ import (
// 启动seelog
func See(filePath string, port int) {
var wg sync.WaitGroup
wg.Add(1)
// 检查参数
if !checkParam(filePath, port) {
return
}
//开启socket管理器
//go manager.start()
var wg sync.WaitGroup
wg.Add(1)
go server(port, filePath)
wg.Wait()
}
......
......@@ -90,18 +90,10 @@ func server(port int, directory string) {
})
// socket链接
http.Handle("/ws", websocket.Handler(gen(directory, manager.filemap)))
// 测试
http.HandleFunc("/test", func(writer http.ResponseWriter, request *http.Request) {
t, err := template.ParseFiles("index.html")
if err != nil {
log.Println(err)
}
t.Execute(writer, nil)
})
var bt bytes.Buffer
go manager.start()
var bt bytes.Buffer
bt.WriteString("127.0.0.1:")
bt.WriteString(strconv.Itoa(port))
http.ListenAndServe(bt.String(), nil)
......
......@@ -4,9 +4,7 @@ import (
"fmt"
"golang.org/x/net/websocket"
"log"
"os"
"strings"
"time"
)
// websocket客户端
......@@ -37,6 +35,7 @@ func getfilemap() map[string]map[client]bool {
file_map := make(map[string]map[client]bool)
return file_map
}
func (manager *clientManager) start() {
defer func() {
if err := recover(); err != nil {
......@@ -130,54 +129,56 @@ func (c *client) read() {
}
}
func (c *client) monitor(filePath string) {
defer func() {
if err := recover(); err != nil {
log.Printf("[seelog] error:%+v", err)
}
}()
var fileInfo os.FileInfo
var err error
for i := 1; i <= 10; i++ {
fileInfo, err = os.Stat(filePath)
if err != nil {
log.Printf("[seelog] error:%v", err.Error())
continue
}
break
}
offset := fileInfo.Size()
for {
fileInfo, err = os.Stat(filePath)
if err != nil {
log.Printf("[seelog] error:%v", err.Error())
continue
}
newOffset := fileInfo.Size()
if offset < newOffset {
msg := make([]byte, newOffset-offset)
file, err := os.Open(filePath)
if err != nil {
log.Printf("[seelog] error:%v", err.Error())
continue
}
_, err = file.Seek(offset, 0)
if err != nil {
log.Printf("[seelog] error:%v", err.Error())
}
_, err = file.Read(msg)
if err != nil {
log.Printf("[seelog] error:%v", err.Error())
}
manager.broadcast <- msg
offset = newOffset
file.Close()
}
offset = newOffset
time.Sleep(200 * time.Millisecond)
}
}
//func (c *client) monitor(filePath string, filename string) {
// defer func() {
// if err := recover(); err != nil {
// log.Printf("[seelog] error:%+v", err)
// }
// }()
// var fileInfo os.FileInfo
// var err error
// for i := 1; i <= 10; i++ {
// fileInfo, err = os.Stat(filePath)
// if err != nil {
// log.Printf("[seelog] error:%v", err.Error())
// continue
// }
// break
// }
//
// offset := fileInfo.Size()
// for {
// fileInfo, err = os.Stat(filePath)
// if err != nil {
// log.Printf("[seelog] error:%v", err.Error())
// continue
// }
// newOffset := fileInfo.Size()
// if offset < newOffset {
// msg := make([]byte, newOffset-offset)
// file, err := os.Open(filePath)
// if err != nil {
// log.Printf("[seelog] error:%v", err.Error())
// continue
// }
// _, err = file.Seek(offset, 0)
// if err != nil {
// log.Printf("[seelog] error:%v", err.Error())
// }
//
// _, err = file.Read(msg)
// if err != nil {
// log.Printf("[seelog] error:%v", err.Error())
// }
// str_msg := string(msg)
// s := filename + "$$" + str_msg
//
// manager.broadcast <- []byte(s)
// offset = newOffset
// file.Close()
// }
// offset = newOffset
// time.Sleep(200 * time.Millisecond)
// }
//
//}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment