Skip to content

Commit

Permalink
Use event-driven instead of polling to read logs
Browse files Browse the repository at this point in the history
  • Loading branch information
koho committed Dec 30, 2024
1 parent a980ec2 commit 4f60085
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 236 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
require (
github.com/fatedier/frp v0.61.1
github.com/fatedier/golib v0.5.0
github.com/fsnotify/fsnotify v1.8.0
github.com/go-ole/go-ole v1.3.0
github.com/lxn/walk v0.0.0-20210112085537-c389da54e794
github.com/lxn/win v0.0.0-20210218163916-a377121e959e
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/fatedier/frp v0.61.1 h1:o9Kqxe9axV5HVsdXs1ruBfd/UrplpVHN/dlQqHJNgdw=
github.com/fatedier/frp v0.61.1/go.mod h1:Ua8s7hyXQhgVidDMnSPbAiODMWa6x6aYfc3HN/Xluqo=
github.com/fatedier/golib v0.5.0 h1:hNcH7hgfIFqVWbP+YojCCAj4eO94pPf4dEF8lmq2jWs=
github.com/fatedier/golib v0.5.0/go.mod h1:W6kIYkIFxHsTzbgqg5piCxIiDo4LzwgTY6R5W8l9NFQ=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/go-jose/go-jose/v4 v4.0.1 h1:QVEPDE3OluqXBQZDcnNvQrInro2h0e4eqNbnZSWqS6U=
github.com/go-jose/go-jose/v4 v4.0.1/go.mod h1:WVf9LFMHh/QVrmqrOfqun0C45tMe3RoiKJMPvgWwLfY=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
Expand Down
48 changes: 31 additions & 17 deletions pkg/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"regexp"
"strings"
"time"
)

// SplitExt splits the path into base name and file extension
Expand All @@ -31,34 +32,31 @@ func FileExists(path string) bool {
}

// FindLogFiles returns the files and dates archived by date
func FindLogFiles(path string) ([]string, []string, error) {
func FindLogFiles(path string) ([]string, []time.Time, error) {
if path == "" || path == "console" {
return nil, nil, os.ErrInvalid
}
if !FileExists(path) {
return nil, nil, os.ErrNotExist
}
fileDir, fileName := filepath.Split(path)
baseName, ext := SplitExt(fileName)
pattern := regexp.MustCompile(`^\.\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$`)
pattern := regexp.MustCompile(`^\.\d{4}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])-([0-1][0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$`)
if fileDir == "" {
fileDir = "."
}
files, err := os.ReadDir(fileDir)
if err != nil {
return nil, nil, err
}
logs := make([]string, 0)
dates := make([]string, 0)
logs = append(logs, path)
dates = append(dates, "")
logs := []string{filepath.Clean(path)}
dates := []time.Time{{}}
for _, file := range files {
if strings.HasPrefix(file.Name(), baseName) && strings.HasSuffix(file.Name(), ext) {
tailPart := strings.TrimPrefix(file.Name(), baseName)
datePart := strings.TrimSuffix(tailPart, ext)
if pattern.MatchString(datePart) {
logs = append(logs, filepath.Join(fileDir, file.Name()))
dates = append(dates, datePart[1:])
if date, err := time.Parse("20060102-150405", datePart[1:]); err == nil {
logs = append(logs, filepath.Join(fileDir, file.Name()))
dates = append(dates, date)
}
}
}
}
Expand All @@ -82,26 +80,42 @@ func RenameFiles(old []string, new []string) {
}
}

// ReadFileLines reads all lines in a file and returns a slice of string
func ReadFileLines(path string) ([]string, error) {
// ReadFileLines reads the last n lines in a file starting at a given offset
func ReadFileLines(path string, offset int64, n int) ([]string, int, int64, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
return nil, -1, 0, err
}
defer file.Close()

_, err = file.Seek(offset, io.SeekStart)
if err != nil {
return nil, -1, 0, err
}
reader := bufio.NewReader(file)

var line string
lines := make([]string, 0)
i := -1
for {
line, err = reader.ReadString('\n')
if err != nil {
break
}
lines = append(lines, line)
if n < 0 || len(lines) < n {
lines = append(lines, line)
} else {
i = (i + 1) % n
lines[i] = line
}
}
offset, err = file.Seek(0, io.SeekCurrent)
if err != nil {
return nil, -1, 0, err
}
if i >= 0 {
i = (i + 1) % n
}
return lines, nil
return lines, i, offset, nil
}

// ZipFiles compresses the given file list to a zip file
Expand Down
6 changes: 3 additions & 3 deletions ui/editclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,11 @@ func (cd *EditClientDialog) onSave() {
// Rename old log files
// The service should be stopped first
cd.shutdownService(true)
util.RenameFiles(logs, lo.Map(dates, func(item string, i int) string {
if item == "" {
util.RenameFiles(logs, lo.Map(dates, func(item time.Time, i int) string {
if item.IsZero() {
return newConf.LogFile
} else {
return filepath.Join(filepath.Dir(newConf.LogFile), baseName+"."+item+ext)
return filepath.Join(filepath.Dir(newConf.LogFile), baseName+"."+item.Format("20060102-150405")+ext)
}
}))
}
Expand Down
25 changes: 12 additions & 13 deletions ui/editproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@ func (pd *EditProxyDialog) basicProxyPage() TabPage {
}

func (pd *EditProxyDialog) advancedProxyPage() TabPage {
bandwidthMode := NewStringPairModel(consts.BandwidthMode,
[]string{i18n.Sprintf("Client"), i18n.Sprintf("Server")}, "")
bandwidthMode := NewListModel(consts.BandwidthMode, i18n.Sprintf("Client"), i18n.Sprintf("Server"))
var xtcpVisitor = Bind("proxyType.Value == 'xtcp' && vm.ServerNameVisible")
return TabPage{
Title: i18n.Sprintf("Advanced"),
Expand All @@ -293,26 +292,26 @@ func (pd *EditProxyDialog) advancedProxyPage() TabPage {
Label{Text: "@"},
ComboBox{
Model: bandwidthMode,
BindingMember: "Name",
DisplayMember: "DisplayName",
BindingMember: "Value",
DisplayMember: "Title",
Value: Bind("BandwidthLimitMode"),
},
},
},
Label{Visible: Bind("vm.PluginEnable"), Text: i18n.SprintfColon("Proxy Protocol")},
ComboBox{
Visible: Bind("vm.PluginEnable"),
Model: NewDefaultListModel([]string{"v1", "v2"}, "", i18n.Sprintf("auto")),
BindingMember: "Name",
DisplayMember: "DisplayName",
Model: NewListModel([]string{"", "v1", "v2"}, i18n.Sprintf("auto")),
BindingMember: "Value",
DisplayMember: "Title",
Value: Bind("ProxyProtocolVersion"),
},
Label{Visible: xtcpVisitor, Text: i18n.SprintfColon("Protocol")},
ComboBox{
Visible: xtcpVisitor,
Model: NewDefaultListModel([]string{consts.ProtoQUIC, consts.ProtoKCP}, "", i18n.Sprintf("default")),
BindingMember: "Name",
DisplayMember: "DisplayName",
Model: NewListModel([]string{"", consts.ProtoQUIC, consts.ProtoKCP}, i18n.Sprintf("default")),
BindingMember: "Value",
DisplayMember: "Title",
Value: Bind("Protocol"),
},
Composite{
Expand Down Expand Up @@ -376,10 +375,10 @@ func (pd *EditProxyDialog) pluginProxyPage() TabPage {
ComboBox{
AssignTo: &pd.pluginView,
Enabled: Bind("vm.PluginEnable"),
Model: NewDefaultListModel(consts.PluginTypes, "", i18n.Sprintf("None")),
Model: NewListModel(append([]string{""}, consts.PluginTypes...), i18n.Sprintf("None")),
Value: Bind("Plugin"),
BindingMember: "Name",
DisplayMember: "DisplayName",
BindingMember: "Value",
DisplayMember: "Title",
OnCurrentIndexChanged: pd.switchType,
Greedy: true,
},
Expand Down
Loading

0 comments on commit 4f60085

Please sign in to comment.