Commit 17cf2994 by lichenggang

优化代码

parent deeff793
...@@ -2,16 +2,17 @@ package main ...@@ -2,16 +2,17 @@ package main
import ( import (
"code" "code"
) "os"
const (
Directory = "E:\\test_logs" // directory of the log
TestPort = 9997
) )
// example // example
func main() { func main() {
Directory := "E:\\test_logs" // directory of the log
TestPort := 9997
isproduct := os.Getenv("GOLOGENV")
if isproduct == "TRUE" {
Directory = ""
}
code.See(Directory, TestPort) code.See(Directory, TestPort)
} }
...@@ -7,15 +7,13 @@ import ( ...@@ -7,15 +7,13 @@ import (
// 启动seelog // 启动seelog
func See(filePath string, port int) { func See(filePath string, port int) {
var wg sync.WaitGroup
wg.Add(1)
// 检查参数 // 检查参数
if !checkParam(filePath, port) { if !checkParam(filePath, port) {
return return
} }
//开启socket管理器
//go manager.start()
var wg sync.WaitGroup
wg.Add(1)
go server(port, filePath) go server(port, filePath)
wg.Wait() wg.Wait()
} }
......
...@@ -90,18 +90,10 @@ func server(port int, directory string) { ...@@ -90,18 +90,10 @@ func server(port int, directory string) {
}) })
// socket链接 // socket链接
http.Handle("/ws", websocket.Handler(gen(directory, manager.filemap))) 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() go manager.start()
var bt bytes.Buffer
bt.WriteString("127.0.0.1:") bt.WriteString("127.0.0.1:")
bt.WriteString(strconv.Itoa(port)) bt.WriteString(strconv.Itoa(port))
http.ListenAndServe(bt.String(), nil) http.ListenAndServe(bt.String(), nil)
......
...@@ -4,9 +4,7 @@ import ( ...@@ -4,9 +4,7 @@ import (
"fmt" "fmt"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"log" "log"
"os"
"strings" "strings"
"time"
) )
// websocket客户端 // websocket客户端
...@@ -37,6 +35,7 @@ func getfilemap() map[string]map[client]bool { ...@@ -37,6 +35,7 @@ func getfilemap() map[string]map[client]bool {
file_map := make(map[string]map[client]bool) file_map := make(map[string]map[client]bool)
return file_map return file_map
} }
func (manager *clientManager) start() { func (manager *clientManager) start() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
...@@ -130,54 +129,56 @@ func (c *client) read() { ...@@ -130,54 +129,56 @@ func (c *client) read() {
} }
} }
func (c *client) monitor(filePath string) { //func (c *client) monitor(filePath string, filename string) {
defer func() { // defer func() {
if err := recover(); err != nil { // if err := recover(); err != nil {
log.Printf("[seelog] error:%+v", err) // log.Printf("[seelog] error:%+v", err)
} // }
}() // }()
// var fileInfo os.FileInfo
var fileInfo os.FileInfo // var err error
var err error // for i := 1; i <= 10; i++ {
for i := 1; i <= 10; i++ { // fileInfo, err = os.Stat(filePath)
fileInfo, err = os.Stat(filePath) // if err != nil {
if err != nil { // log.Printf("[seelog] error:%v", err.Error())
log.Printf("[seelog] error:%v", err.Error()) // continue
continue // }
} // break
break // }
} //
// offset := fileInfo.Size()
offset := fileInfo.Size() // for {
for { // fileInfo, err = os.Stat(filePath)
fileInfo, err = os.Stat(filePath) // if err != nil {
if err != nil { // log.Printf("[seelog] error:%v", err.Error())
log.Printf("[seelog] error:%v", err.Error()) // continue
continue // }
} // newOffset := fileInfo.Size()
newOffset := fileInfo.Size() // if offset < newOffset {
if offset < newOffset { // msg := make([]byte, newOffset-offset)
msg := make([]byte, newOffset-offset) // file, err := os.Open(filePath)
file, err := os.Open(filePath) // if err != nil {
if err != nil { // log.Printf("[seelog] error:%v", err.Error())
log.Printf("[seelog] error:%v", err.Error()) // continue
continue // }
} // _, err = file.Seek(offset, 0)
_, err = file.Seek(offset, 0) // if err != nil {
if err != nil { // log.Printf("[seelog] error:%v", err.Error())
log.Printf("[seelog] error:%v", err.Error()) // }
} //
// _, err = file.Read(msg)
_, err = file.Read(msg) // if err != nil {
if err != nil { // log.Printf("[seelog] error:%v", err.Error())
log.Printf("[seelog] error:%v", err.Error()) // }
} // str_msg := string(msg)
manager.broadcast <- msg // s := filename + "$$" + str_msg
offset = newOffset //
file.Close() // manager.broadcast <- []byte(s)
} // offset = newOffset
offset = newOffset // file.Close()
time.Sleep(200 * time.Millisecond) // }
} // 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