28 lines
505 B
Go
28 lines
505 B
Go
package frp
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
func ReadLogTail(path string, limit int) ([]string, error) {
|
||
|
|
if limit <= 0 {
|
||
|
|
limit = 100
|
||
|
|
}
|
||
|
|
data, err := os.ReadFile(path)
|
||
|
|
if err != nil {
|
||
|
|
if os.IsNotExist(err) {
|
||
|
|
return []string{}, nil
|
||
|
|
}
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
lines := strings.Split(strings.TrimRight(string(data), "\n"), "\n")
|
||
|
|
if len(lines) == 0 || (len(lines) == 1 && lines[0] == "") {
|
||
|
|
return []string{}, nil
|
||
|
|
}
|
||
|
|
if len(lines) > limit {
|
||
|
|
lines = lines[len(lines)-limit:]
|
||
|
|
}
|
||
|
|
return lines, nil
|
||
|
|
}
|