2021-09-16 15:38:43 +02:00
|
|
|
//go:build windows
|
2021-06-14 10:43:35 +02:00
|
|
|
// +build windows
|
|
|
|
|
2022-05-07 07:23:08 +02:00
|
|
|
package logs
|
2021-06-14 10:43:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-03-19 00:38:49 +02:00
|
|
|
|
|
|
|
"github.com/aybabtme/humanlog"
|
2021-06-14 10:43:35 +02:00
|
|
|
)
|
|
|
|
|
2021-06-15 09:31:52 +02:00
|
|
|
func TailLogsForPlatform(logFilePath string, opts *humanlog.HandlerOptions) {
|
2021-06-14 10:43:35 +02:00
|
|
|
var lastModified int64 = 0
|
|
|
|
var lastOffset int64 = 0
|
|
|
|
for {
|
|
|
|
stat, err := os.Stat(logFilePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if stat.ModTime().Unix() > lastModified {
|
|
|
|
err = TailFrom(lastOffset, logFilePath, opts)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastOffset = stat.Size()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenAndSeek(filepath string, offset int64) (*os.File, error) {
|
|
|
|
file, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = file.Seek(offset, 0)
|
|
|
|
if err != nil {
|
|
|
|
_ = file.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TailFrom(lastOffset int64, logFilePath string, opts *humanlog.HandlerOptions) error {
|
|
|
|
file, err := OpenAndSeek(logFilePath, lastOffset)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileScanner := bufio.NewScanner(file)
|
|
|
|
var lines []string
|
|
|
|
for fileScanner.Scan() {
|
|
|
|
lines = append(lines, fileScanner.Text())
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
lineCount := len(lines)
|
|
|
|
lastTen := lines
|
|
|
|
if lineCount > 10 {
|
|
|
|
lastTen = lines[lineCount-10:]
|
|
|
|
}
|
|
|
|
for _, line := range lastTen {
|
|
|
|
reader := strings.NewReader(line)
|
|
|
|
if err := humanlog.Scanner(reader, os.Stdout, opts); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|