Skip to content

Commit

Permalink
Fix reading log after rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
kubec committed Jun 2, 2021
1 parent 69818d3 commit 7bc9a41
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.vscode
*.log
chia-log-analyzer
builds
builds
*debug*
54 changes: 46 additions & 8 deletions chia-log-analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"bufio"
"flag"
"fmt"
"log"
"io/ioutil"
"os"
"os/user"
"regexp"
Expand All @@ -21,6 +21,7 @@ import (

ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
log "github.com/sirupsen/logrus"
)

var debuglogFile *string
Expand Down Expand Up @@ -88,6 +89,8 @@ var lastFarmStack = stackStructFloats{count: 29}
var lastFarmingTimesStack = stackStructFloats{count: 113}

func main() {

initLogging()
detectLogFileLocation()

if err := ui.Init(); err != nil {
Expand Down Expand Up @@ -177,13 +180,35 @@ func main() {

}

func initLogging() {
writeLog := flag.Bool("writelog", false, "write log?")
flag.Parse()
if *writeLog {
// If the file doesn't exist, create it or append to the file
file, err := os.OpenFile("chia-log-analyzer.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
log.SetOutput(file)
} else {
log.SetOutput(ioutil.Discard)
}

log.SetLevel(log.InfoLevel)
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
log.Info("Application start")
}

func detectLogFileLocation() {
debuglogFile = flag.String("log", "./debug.log", "path to debug.log")
flag.Parse()
missingLogFile := false

//1 - try open debug.log in the actual directory or get file from the paremeter "log"
fmt.Printf("trying: %s\n", *debuglogFile)
log.Info(fmt.Sprintf("Trying to open: %s", *debuglogFile))
if _, err := os.Stat(*debuglogFile); os.IsNotExist(err) {
missingLogFile = true
} else {
Expand All @@ -195,7 +220,7 @@ func detectLogFileLocation() {
dir := usr.HomeDir
defaultLogLocation := fmt.Sprintf("%s/.chia/mainnet/log/debug.log", dir)
debuglogFile = &defaultLogLocation
fmt.Printf("trying: %s\n", *debuglogFile)
log.Info(fmt.Sprintf("Trying to open: %s", *debuglogFile))
if _, err := os.Stat(*debuglogFile); os.IsNotExist(err) {
missingLogFile = true
} else {
Expand All @@ -217,10 +242,13 @@ func loopReadFile() {
var actualLogFileSize int64
for range c {
actualLogFileSize, _ = getFileSize(*debuglogFile)
log.Info(fmt.Sprintf("Actual log file size: %d bytes", actualLogFileSize))
if actualLogFileSize == 0 || actualLogFileSize == lastLogFileSize {
log.Info("No file change, skipping parsing")
continue
}
if actualLogFileSize < lastLogFileSize { // new file ?
log.Info("New file detected")
readFullFile(*debuglogFile)
} else {
readFile(*debuglogFile)
Expand All @@ -232,6 +260,7 @@ func loopReadFile() {

func setLastLogFileSize(filepath string) {
lastLogFileSize, _ = getFileSize(filepath)
log.Info(fmt.Sprintf("Last file size: %d bytes", lastLogFileSize))
}

func getFileSize(filepath string) (int64, error) {
Expand All @@ -248,14 +277,17 @@ func readFullFile(fname string) {
return
}

log.Info("Reading full file")
lastRow = ""

// os.Open() opens specific file in
// read-only mode and this return
// a pointer of type os.
file, err := os.Open(fname)

if err != nil {
log.Fatalf("failed to open")

log.Error(fmt.Sprintf("Failed to open log file: %s, skipping reading", fname))
return
}
defer file.Close()

Expand Down Expand Up @@ -286,12 +318,14 @@ func readFullFile(fname string) {

func readFile(fname string) {
if _, err := os.Stat(fname); os.IsNotExist(err) {
log.Error(fmt.Sprintf("Failed to open log file: %s, skipping reading", fname))
return
}

file, err := os.Open(fname)
if err != nil {
panic(err)
log.Error(fmt.Sprintf("Failed to open log file: %s, skipping reading", err))
return
}
defer file.Close()

Expand All @@ -306,6 +340,8 @@ func readFile(fname string) {
start = stat.Size() - int64(bytesToRead)
}

log.Info(fmt.Sprintf("Reading file. %d bytes from the position: %d", bytesToRead, start))

_, err = file.ReadAt(buf, start)
if err == nil {
lines := strings.Split(string(buf), "\n")
Expand All @@ -316,6 +352,8 @@ func readFile(fname string) {
}

func parseLines(lines []string) {
log.Info(fmt.Sprintf("Number of rows for parsing: %d", len(lines)))

//0 plots were eligible for farming 27274481c3... Found 0 proofs. Time: 0.14447 s. Total 105 plots
regexPlotsFarming, _ := regexp.Compile("([0-9]+)\\s+plots\\s+were\\seligible.*Found\\s([0-9])+\\sproofs.*Time:\\s([0-9]+\\.[0-9]+)\\ss\\.\\sTotal\\s([0-9]+)\\splots")

Expand Down Expand Up @@ -382,14 +420,15 @@ func parseLines(lines []string) {
}
}

log.Info("Log parsing done")

renderWidgets()
renderLastFarmBarChart()
renderLastFarmBarChart2()
renderSparkLines()

var tmpTxt strings.Builder
for i := range lastParsedLinesStack.lines {
//tmpTxt.WriteString(lastParsedLinesStack.lines[i])
twoCols := strings.Split(lastParsedLinesStack.lines[i], " ")
tmpTxt.WriteString(twoCols[0])
tmpTxt.WriteString("\n")
Expand Down Expand Up @@ -460,7 +499,6 @@ func renderOverallHealth() {
percent = 100 //overwrite down to 100% is OK
}*/

//percent := avg
widgetOverallHealthPercent.TextStyle.Fg = ui.ColorRed
if percent > 80 {
widgetOverallHealthPercent.TextStyle.Fg = ui.ColorCyan
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gizak/termui v3.1.0+incompatible h1:N3CFm+j087lanTxPpHOmQs0uS3s5I9TxoAFy6DqPqv8=
github.com/gizak/termui v3.1.0+incompatible/go.mod h1:PkJoWUt/zacQKysNfQtcw1RW+eK2SxkieVBtl+4ovLA=
github.com/gizak/termui/v3 v3.1.0 h1:ZZmVDgwHl7gR7elfKf1xc4IudXZ5qqfDh4wExk4Iajc=
Expand All @@ -15,5 +16,11 @@ github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyh
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 comments on commit 7bc9a41

Please sign in to comment.