Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
lichenggang
/
web_log
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
17cf2994
authored
Dec 23, 2020
by
lichenggang
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
优化代码
parent
deeff793
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
72 deletions
main.go
src/code/see.go
src/code/server.go
src/code/ws.go
main.go
View file @
17cf2994
...
...
@@ -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
)
}
src/code/see.go
View file @
17cf2994
...
...
@@ -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
()
}
...
...
src/code/server.go
View file @
17cf2994
...
...
@@ -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
)
...
...
src/code/ws.go
View file @
17cf2994
...
...
@@ -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)
// }
//
//}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment