Skip to content

Commit

Permalink
simulation: close opened files and check for error while creating files
Browse files Browse the repository at this point in the history
From: #8217
Closes: #6784
Thanks: @PrathyushaLakkireddy for the original patch.

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
  • Loading branch information
3 people committed Dec 22, 2020
1 parent a72d411 commit b34f58c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions x/simulation/log.go
Expand Up @@ -34,6 +34,8 @@ func (lw *StandardLogWriter) AddEntry(opEntry OperationEntry) {
// PrintLogs - print the logs to a simulation file
func (lw *StandardLogWriter) PrintLogs() {
f := createLogFile()
defer f.Close()

for i := 0; i < len(lw.OpEntries); i++ {
writeEntry := fmt.Sprintf("%s\n", (lw.OpEntries[i]).MustMarshal())
_, err := f.WriteString(writeEntry)
Expand All @@ -45,17 +47,22 @@ func (lw *StandardLogWriter) PrintLogs() {

func createLogFile() *os.File {
var f *os.File
fileName := fmt.Sprintf("%s.log", time.Now().Format("2006-01-02_15:04:05"))

folderPath := os.ExpandEnv("$HOME/.simapp/simulations")
fileName := fmt.Sprintf("%s.log", time.Now().Format("2006-01-02_15:04:05"))
folderPath := path.Join(os.ExpandEnv("$HOME"), ".simapp", "simulations")
filePath := path.Join(folderPath, fileName)

err := os.MkdirAll(folderPath, os.ModePerm)
if err != nil {
panic(err)
}
f, _ = os.Create(filePath)

f, err = os.Create(filePath)
if err != nil {
panic(err)
}
fmt.Printf("Logs to writing to %s\n", filePath)

return f
}

Expand Down

0 comments on commit b34f58c

Please sign in to comment.