From a5561d76e6c56c7d12364881bd0fd69161728735 Mon Sep 17 00:00:00 2001 From: Redume Date: Mon, 21 Nov 2022 22:22:00 +0300 Subject: [PATCH] Now it is possible to turn off error logging Signed-off-by: Redume --- api/controllers/settings.go | 16 ++++-- functions/database.go | 34 +++++++++++-- functions/logger.go | 46 ++++++++++++----- web/src/settings.html | 8 ++- web/static/scripts/switch.js | 96 +++++++++++++++++++++++++++++++----- 5 files changed, 168 insertions(+), 32 deletions(-) diff --git a/api/controllers/settings.go b/api/controllers/settings.go index 63270f2..c312417 100644 --- a/api/controllers/settings.go +++ b/api/controllers/settings.go @@ -27,14 +27,14 @@ var SettingsGet = func(c *fiber.Ctx) error { } }(querySettings) - var startup, wallpaper int + var startup, wallpaper, save_logg int for querySettings.Next() { - err := querySettings.Scan(&startup, &wallpaper) + err := querySettings.Scan(&startup, &wallpaper, &save_logg) if err != nil { functions.Logger(err.Error()) } - var data = map[string]interface{}{"startup": startup, "wallpaper": wallpaper} + var data = map[string]interface{}{"startup": startup, "wallpaper": wallpaper, "save_logg": save_logg} utils.Respond(c, data) } @@ -49,8 +49,9 @@ var SettingsUpdate = func(c *fiber.Ctx) error { startup := c.FormValue("startup") wallpaper := c.FormValue("wallpaper") + save_logg := c.FormValue("save_logg") - if startup == "" && wallpaper == "" { + if startup == "" && wallpaper == "" && save_logg == "" { utils.Respond(c, utils.Message(false, "All fields are required.")) return nil } @@ -73,6 +74,13 @@ var SettingsUpdate = func(c *fiber.Ctx) error { } } + if save_logg != "" { + _, err := db.Exec("UPDATE settings SET save_logg = ?", save_logg) + if err != nil { + functions.Logger(err.Error()) + } + } + utils.Respond(c, utils.Message(true, "The settings have been applied successfully.")) return nil } diff --git a/functions/database.go b/functions/database.go index 26c836c..dfced22 100644 --- a/functions/database.go +++ b/functions/database.go @@ -2,6 +2,8 @@ package functions import ( "database/sql" + "encoding/json" + "github.com/rodkranz/fetch" _ "github.com/mattn/go-sqlite3" ) @@ -23,7 +25,8 @@ func Database() { sqlTable := ` CREATE TABLE IF NOT EXISTS settings ( startup INTEGER DEFAULT 0, - wallpaper INTEGER DEFAULT 0 + wallpaper INTEGER DEFAULT 0, + save_logg INTEGER DEFAULT 0 );` _, err = db.Exec(sqlTable) @@ -31,14 +34,39 @@ func Database() { Logger(err.Error()) } - stmt, err := db.Prepare("INSERT INTO settings(startup, wallpaper) values(?,?)") + stmt, err := db.Prepare("INSERT INTO settings(startup, wallpaper, save_logg) values(?,?,?)") if err != nil { Logger(err.Error()) } - _, err = stmt.Exec(0, 0) + _, err = stmt.Exec(0, 0, 0) if err != nil { Logger(err.Error()) } } } + +func getDatabase() int { + client := fetch.NewDefault() + res, err := client.Get("http://localhost:3000/api/get/settings", nil) + if err != nil { + panic(err) + } + + body, err := res.ToString() + if err != nil { + panic(err) + } + + type DatabaseStruct struct { + Save_logg int `json:"save_logg"` + } + + var Database DatabaseStruct + err = json.Unmarshal([]byte(body), &Database) + if err != nil { + panic(err) + } + + return Database.Save_logg +} diff --git a/functions/logger.go b/functions/logger.go index 3f25309..e746826 100644 --- a/functions/logger.go +++ b/functions/logger.go @@ -9,17 +9,32 @@ import ( ) func Logger(text string) { - if !FileExists("logger.log") { - err := CreateFile("logger.log") + now := time.Now() + + file := string(now.Format("01.02.2006")) + ".log" + + if getDatabase() == 0 { + return + } + + if !FileExists("logs") { + err := CreateFolder("logs") if err != nil { panic(err) } } - f, err := os.OpenFile("logger.log", os.O_APPEND|os.O_WRONLY, 0600) + + if !FileExists("./logs/" + file) { + err := CreateFile(file) + if err != nil { + panic(err) + } + } + + f, err := os.OpenFile("logs/"+file, os.O_APPEND|os.O_WRONLY, 0600) if err != nil { panic(err) } - now := time.Now() _, file, line, ok := runtime.Caller(1) @@ -57,15 +72,24 @@ func FileExists(name string) bool { } func CreateFile(name string) error { - fo, err := os.Create(name) + file, err := os.Create("logs/" + name) + if err != nil { + return nil + } + + defer func(file *os.File) { + err := file.Close() + if err != nil { + + } + }(file) + return nil +} + +func CreateFolder(name string) error { + err := os.Mkdir(name, 0755) if err != nil { return err } - defer func() { - err := fo.Close() - if err != nil { - return - } - }() return nil } diff --git a/web/src/settings.html b/web/src/settings.html index f6b1374..380b847 100644 --- a/web/src/settings.html +++ b/web/src/settings.html @@ -50,13 +50,19 @@ +
+

Create log files

+

Every Nasa will create files in which errors will be written that will help developers when correcting errors.

+
+ + +

Create an application shortcut

EveryNasa will create an application shortcut on your desktop.

-