Now it is possible to turn off error logging

Signed-off-by: Redume <Danilivasenko09@gmail.com>
This commit is contained in:
Данил 2022-11-21 22:22:00 +03:00 committed by Redume
parent 760459b7f2
commit a5561d76e6
5 changed files with 168 additions and 32 deletions

View file

@ -27,14 +27,14 @@ var SettingsGet = func(c *fiber.Ctx) error {
} }
}(querySettings) }(querySettings)
var startup, wallpaper int var startup, wallpaper, save_logg int
for querySettings.Next() { for querySettings.Next() {
err := querySettings.Scan(&startup, &wallpaper) err := querySettings.Scan(&startup, &wallpaper, &save_logg)
if err != nil { if err != nil {
functions.Logger(err.Error()) 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) utils.Respond(c, data)
} }
@ -49,8 +49,9 @@ var SettingsUpdate = func(c *fiber.Ctx) error {
startup := c.FormValue("startup") startup := c.FormValue("startup")
wallpaper := c.FormValue("wallpaper") 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.")) utils.Respond(c, utils.Message(false, "All fields are required."))
return nil 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.")) utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))
return nil return nil
} }

View file

@ -2,6 +2,8 @@ package functions
import ( import (
"database/sql" "database/sql"
"encoding/json"
"github.com/rodkranz/fetch"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@ -23,7 +25,8 @@ func Database() {
sqlTable := ` sqlTable := `
CREATE TABLE IF NOT EXISTS settings ( CREATE TABLE IF NOT EXISTS settings (
startup INTEGER DEFAULT 0, startup INTEGER DEFAULT 0,
wallpaper INTEGER DEFAULT 0 wallpaper INTEGER DEFAULT 0,
save_logg INTEGER DEFAULT 0
);` );`
_, err = db.Exec(sqlTable) _, err = db.Exec(sqlTable)
@ -31,14 +34,39 @@ func Database() {
Logger(err.Error()) 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 { if err != nil {
Logger(err.Error()) Logger(err.Error())
} }
_, err = stmt.Exec(0, 0) _, err = stmt.Exec(0, 0, 0)
if err != nil { if err != nil {
Logger(err.Error()) 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
}

View file

@ -9,17 +9,32 @@ import (
) )
func Logger(text string) { func Logger(text string) {
if !FileExists("logger.log") { now := time.Now()
err := CreateFile("logger.log")
file := string(now.Format("01.02.2006")) + ".log"
if getDatabase() == 0 {
return
}
if !FileExists("logs") {
err := CreateFolder("logs")
if err != nil { if err != nil {
panic(err) 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 { if err != nil {
panic(err) panic(err)
} }
now := time.Now()
_, file, line, ok := runtime.Caller(1) _, file, line, ok := runtime.Caller(1)
@ -57,15 +72,24 @@ func FileExists(name string) bool {
} }
func CreateFile(name string) error { 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 { if err != nil {
return err return err
} }
defer func() {
err := fo.Close()
if err != nil {
return
}
}()
return nil return nil
} }

View file

@ -50,13 +50,19 @@
<label class="form-check-label" for="settings_autoSetWallpaperTogglerName" id="settings_autoSetWallpaperTogglerName">Off</label> <label class="form-check-label" for="settings_autoSetWallpaperTogglerName" id="settings_autoSetWallpaperTogglerName">Off</label>
</div> </div>
</div> </div>
<div class="settings">
<p class="title"><strong>Create log files</strong></p>
<p class="desc">Every Nasa will create files in which errors will be written that will help developers when correcting errors.</p>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="settings_saveLoggSwitch">
<label class="form-check-label" for="settings_autoSetWallpaperTogglerName" id="settings_saveLoggTogglerName">Off</label>
</div> </div>
<div class="settings"> <div class="settings">
<p class="title"><strong>Create an application shortcut</strong></p> <p class="title"><strong>Create an application shortcut</strong></p>
<p class="desc">EveryNasa will create an application shortcut on your desktop.</p> <p class="desc">EveryNasa will create an application shortcut on your desktop.</p>
<button type="button" class="btn btn-outline-primary" id="createLabelButton">Create</button> <button type="button" class="btn btn-outline-primary" id="createLabelButton">Create</button>
</div> </div>
</header> </header>
</header>
</div> </div>
<div class="toast-container position-fixed bottom-0 end-0 p-3"> <div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true"> <div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">

View file

@ -5,19 +5,20 @@ $(document).ready(async function() {
const $wallpaperSwitch = $("#settings_autoSetWallpaperSwitch"); const $wallpaperSwitch = $("#settings_autoSetWallpaperSwitch");
const $wallpaperSwitchTogglerName = $("#settings_autoSetWallpaperTogglerName"); const $wallpaperSwitchTogglerName = $("#settings_autoSetWallpaperTogglerName");
$.ajax({ const $loggingSwitch = $("#settings_saveLoggSwitch");
url: "http://localhost:3000/api/get/settings", const $loggingSwitchTogglerName = $("#settings_saveLoggTogglerName");
type: "GET",
success: function(data) { getSettings().then((data) => {
if (data["wallpaper"] === 1) { if (data["wallpaper"] === 1) {
$wallpaperSwitch.attr("checked", "true"); $wallpaperSwitch.attr("checked", "true");
$wallpaperSwitchTogglerName.text("On"); $wallpaperSwitchTogglerName.text("On");
} } else if (data["startup"] === 1) {
if (data["startup"] === 1) { $startupSwitch.attr("checked", "true");
$startupSwitch.attr("checked", "true"); $startupSwitchTogglerName.text("On");
$startupSwitchTogglerName.text("On"); } else if (data["save_logg"] === 1) {
} $loggingSwitch.attr("checked", "true");
}, $loggingSwitchTogglerName.text("On");
}
}); });
$wallpaperSwitch.click(async function() { $wallpaperSwitch.click(async function() {
@ -116,16 +117,78 @@ $(document).ready(async function() {
}, },
}); });
}); });
$loggingSwitch.click(async function() {
$.ajax({
url: "http://localhost:3000/api/get/settings",
type: "GET",
success: function (data) {
if (data["save_logg"] === 1) {
$.ajax({
url: "http://localhost:3000/api/update/settings",
type: "POST",
data: {
"save_logg": 0,
},
success: function (data) {
if(data["status"]) {
$loggingSwitchTogglerName.text("Off");
$loggingSwitch.removeAttr("checked");
toast(data.message);
} else {
toast("Failed to apply settings.");
}
},
});
} else {
$.ajax({
url: "http://localhost:3000/api/update/settings",
type: "POST",
data: {
"save_logg": 1,
},
success: function (data) {
if(data["status"]) {
$loggingSwitchTogglerName.text("On");
$loggingSwitch.attr("checked", "true");
toast(data.message);
} else {
toast("Failed to apply settings.");
}
},
});
}
},
})
});
}); });
/**
* @param {String} message
*/
function toast(message) { function toast(message) {
if (message === null) {
return "Required parameter 'message' is missing.";
}
$(".toast-body").text(message); $(".toast-body").text(message);
let toastLiveExample = document.getElementById('liveToast'); let toastLiveExample = document.getElementById('liveToast');
let toast = new bootstrap.Toast(toastLiveExample); let toast = new bootstrap.Toast(toastLiveExample);
toast.show(); toast.show();
} }
/**
* @param {Number} i
*/
function editStartup(i) { function editStartup(i) {
if (i !== 1 || i !== 0 || i === null) {
return "Required parameter 'i' is missing.";
}
return $.ajax({ return $.ajax({
url: "http://localhost:3000/api/update/startup", url: "http://localhost:3000/api/update/startup",
type: "POST", type: "POST",
@ -134,3 +197,10 @@ function editStartup(i) {
}, },
}); });
} }
function getSettings() {
return $.ajax({
url: "http://localhost:3000/api/get/settings",
type: "GET",
});
}