From 8031477f38728a7a218487df51da497191dc811e Mon Sep 17 00:00:00 2001 From: xoy Date: Sun, 29 Jan 2023 16:09:39 +0100 Subject: [PATCH] fehlerbehebung --- file.go | 30 ++++++++++++++++++++++++++---- logger.go | 3 ++- main.go | 2 -- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/file.go b/file.go index 48b1881..d451a68 100644 --- a/file.go +++ b/file.go @@ -29,9 +29,31 @@ func fileAddLine(input string, filepath string) { } func fileCreate(filepath string) { - _, err := os.Create(filepath) + if !fileExist(filepath) { + _, err := os.Create(filepath) + errorPanic(err) - errorPanic(err) - - logger("file created -> " + filepath) + logger("fileCreate : file created -> " + filepath) + } else { + logger("fileCreate : file already exists -> " + filepath) + } +} + +func fileCreateDir(dirpath string) { + if fileExist(dirpath) { + err := os.Mkdir(dirpath, 0755) + errorPanic(err) + logger("fileCreateDir : folder created -> " + dirpath) + } else { + logger("fileCreateDir : folder not created -> " + dirpath) + } +} + +func fileExist(filepath string) bool { + if _, err := os.Stat(filepath); err == nil { + return true + } else if errors.Is(err, os.ErrNotExist) { + return false + } + return false } diff --git a/logger.go b/logger.go index f1ee85f..621236a 100644 --- a/logger.go +++ b/logger.go @@ -3,6 +3,7 @@ package main import "time" func logger(input string) { + fileCreateDir("./log") println("[" + time.Now().Format("15:04:05") + "] " + input) fileAddLine("["+time.Now().Format("15:04:05")+"] "+input, "./log/"+time.Now().Format("2006-02-01")+".log") } @@ -13,6 +14,6 @@ func errorPanic(err error, logBefore ...string) { logger(log) } logger(err.Error()) - panic("----------------ERROR----------------") + panic("\n----------------ERROR----------------\n") } } diff --git a/main.go b/main.go index 6f3a4a7..d426157 100644 --- a/main.go +++ b/main.go @@ -23,8 +23,6 @@ func main() { err := http.ListenAndServe(addr, nil) errorPanic(err) - - defer logger("----------------END----------------") } func handler() {