Skip to content

Commit

Permalink
Fix log file error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefMarlin committed Aug 11, 2023
1 parent e493b93 commit 353f05a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ func tlog(logType string, logData string) {
if logFile != "" {
// Check if log file exists, if not create it along with the path
if _, err := os.Stat(logFile); os.IsNotExist(err) {
os.MkdirAll(logFile, 0755)
err := os.MkdirAll(logFile, 0755)
if err != nil {
fmt.Fprintf(os.Stderr, "error: cannot create log file path: %s\n", err)
os.Exit(4)
}
}

f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
tlog("error", fmt.Sprintf("%s", err))
fmt.Fprintf(os.Stderr, "error: cannot open log file: %s\n", err)
os.Exit(4)
}
defer f.Close()
fmt.Fprintf(f, "%s [%s]% -s %s\n", time.Now().Format("02/01/2006_15:04:05.000000"), logType, "", logData)
Expand Down Expand Up @@ -197,8 +202,8 @@ func handleArgs() {
fmt.Println("Updated!")
os.Exit(0)

case "version", "-v", "--version":
fmt.Fprintf(os.Stdout, "%s\n", "VERSION")
case "version", "v", "-v", "--version":
fmt.Fprintf(os.Stdout, "%s\n", "VERSION: "+Version)
os.Exit(0)

case "help", "h", "-h", "--help":
Expand Down

0 comments on commit 353f05a

Please sign in to comment.