From 492a696fcf1d70d063777ab6422f8f8a2b68a86b Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Wed, 15 Sep 2021 02:31:15 -0700 Subject: [PATCH] Add option to disable using current time as logfile name --- cmd/{ => HellPot}/hellpot.go | 0 cmd/{ => HellPot}/http.go | 0 cmd/{ => HellPot}/main.go | 3 ++- cmd/{ => HellPot}/markov.go | 0 cmd/{ => HellPot}/src.go | 0 cmd/{ => HellPot}/util.go | 0 config/config.go | 20 +++++++++++--------- config/logger.go | 22 +++++++++++++++++----- 8 files changed, 30 insertions(+), 15 deletions(-) rename cmd/{ => HellPot}/hellpot.go (100%) rename cmd/{ => HellPot}/http.go (100%) rename cmd/{ => HellPot}/main.go (70%) rename cmd/{ => HellPot}/markov.go (100%) rename cmd/{ => HellPot}/src.go (100%) rename cmd/{ => HellPot}/util.go (100%) diff --git a/cmd/hellpot.go b/cmd/HellPot/hellpot.go similarity index 100% rename from cmd/hellpot.go rename to cmd/HellPot/hellpot.go diff --git a/cmd/http.go b/cmd/HellPot/http.go similarity index 100% rename from cmd/http.go rename to cmd/HellPot/http.go diff --git a/cmd/main.go b/cmd/HellPot/main.go similarity index 70% rename from cmd/main.go rename to cmd/HellPot/main.go index d44c8d1..69d0f4e 100644 --- a/cmd/main.go +++ b/cmd/HellPot/main.go @@ -16,7 +16,8 @@ func init() { if config.Debug { log.Debug().Msg("debug enabled") } - log.Info().Str("config", config.Filename).Msg("initialization finished") + log.Info().Str("file", config.Filename).Msg("current config") + log.Info().Str("file", config.CurrentLogFile).Msg("current log") } func main() { diff --git a/cmd/markov.go b/cmd/HellPot/markov.go similarity index 100% rename from cmd/markov.go rename to cmd/HellPot/markov.go diff --git a/cmd/src.go b/cmd/HellPot/src.go similarity index 100% rename from cmd/src.go rename to cmd/HellPot/src.go diff --git a/cmd/util.go b/cmd/HellPot/util.go similarity index 100% rename from cmd/util.go rename to cmd/HellPot/util.go diff --git a/config/config.go b/config/config.go index 4b24096..07ad400 100644 --- a/config/config.go +++ b/config/config.go @@ -83,19 +83,24 @@ func Init() { snek.AddConfigPath(loc) } + Filename = snek.ConfigFileUsed() + if err = snek.MergeInConfig(); err != nil { if _, err := os.Stat(prefConfigLocation); os.IsNotExist(err) { if err = os.Mkdir(prefConfigLocation, 0755); err != nil { panic(err) } } - if err = snek.SafeWriteConfigAs(prefConfigLocation + "/" + "config.toml"); err != nil { + + newconfig := prefConfigLocation + "/" + "config.toml" + + if err = snek.SafeWriteConfigAs(newconfig); err != nil { fmt.Println(err.Error()) os.Exit(1) } - } - Filename = snek.ConfigFileUsed() + Filename = newconfig + } associate() } @@ -104,12 +109,9 @@ func setDefaults() { var configSections = []string{"logger", "http"} Opt["logger"] = map[string]interface{}{ - "debug": true, - "directory": home + "/.config/" + title + "/logs/", - } - Opt["data"] = map[string]interface{}{ - "directory": home + "/.config/" + title + "./.data/", - "maxsizeobj": 20, + "debug": true, + "directory": home + "/.config/" + title + "/logs/", + "use_date_filename": true, } Opt["http"] = map[string]interface{}{ "bind_addr": "127.0.0.1", diff --git a/config/logger.go b/config/logger.go index f17bd14..84569c1 100644 --- a/config/logger.go +++ b/config/logger.go @@ -1,16 +1,18 @@ package config import ( + "github.com/rs/zerolog" "os" + "strings" "time" - - "github.com/rs/zerolog" ) var ( - logFile *os.File - logDir string + logFile *os.File + logDir string + CurrentLogFile string ) + // Logger retrieves an instance of our zerolog loggger so we can hook it in our main package. func Logger() zerolog.Logger { logDir = snek.GetString("logger.directory") @@ -18,7 +20,17 @@ func Logger() zerolog.Logger { println("cannot create log directory: " + logDir + "(" + err.Error() + ")") os.Exit(1) } - if logFile, err = os.OpenFile(logDir+time.Now().Format(time.RFC3339)+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil { + + tnow := "HellPot" + + if snek.GetBool("logger.use_date_filename") { + tnow = strings.Replace(time.Now().Format(time.RFC822), " ", "_", -1) + tnow = strings.Replace(tnow, ":", "-", -1) + } + + CurrentLogFile = logDir + tnow + ".log" + + if logFile, err = os.OpenFile(CurrentLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil { println("cannot create log file: " + err.Error()) os.Exit(1) }