Skip to content

Commit 2ffe3d4

Browse files
committed
fix in log rotate detection
1 parent 8006d09 commit 2ffe3d4

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

chia-log-analyzer.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,17 +335,23 @@ func readFile(fname string) {
335335
stat, err := os.Stat(fname)
336336
start := int64(0)
337337

338-
//is file big enough ?
339-
if stat.Size() >= int64(bytesToRead) {
340-
start = stat.Size() - int64(bytesToRead)
341-
}
338+
if stat.Size() > 0 { //not empty file
339+
//is file big enough ?
340+
if stat.Size() >= int64(bytesToRead) {
341+
start = stat.Size() - int64(bytesToRead)
342+
} else {
343+
buf = make([]byte, stat.Size())
344+
}
342345

343-
log.Info(fmt.Sprintf("Reading file. %d bytes from the position: %d", bytesToRead, start))
346+
log.Info(fmt.Sprintf("Reading file. %d bytes from the position: %d", len(buf), start))
344347

345-
_, err = file.ReadAt(buf, start)
346-
if err == nil {
347-
lines := strings.Split(string(buf), "\n")
348-
parseLines(lines)
348+
_, err = file.ReadAt(buf, start)
349+
if err == nil {
350+
lines := strings.Split(strings.ReplaceAll(string(buf), "\r\n", "\n"), "\n")
351+
parseLines(lines)
352+
} else {
353+
log.Error(fmt.Sprintf("Error when reading bytes from log file: %s", err))
354+
}
349355
}
350356

351357
file.Close()
@@ -359,6 +365,7 @@ func parseLines(lines []string) {
359365

360366
startParsingLines := false
361367
for i, s := range lines {
368+
log.Info(fmt.Sprintf("Last row: %s", lastRow))
362369
if i == 0 { //skip the first row - can be uncomplete due reading by bytes
363370
continue
364371
}
@@ -377,6 +384,7 @@ func parseLines(lines []string) {
377384
}
378385

379386
lastRow = s
387+
log.Info(fmt.Sprintf("Last row2: %s", lastRow))
380388

381389
if regexPlotsFarming.MatchString(s) {
382390
lastParsedLinesStack.push(s)

0 commit comments

Comments
 (0)