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)
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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -50,13 +50,19 @@
<label class="form-check-label" for="settings_autoSetWallpaperTogglerName" id="settings_autoSetWallpaperTogglerName">Off</label>
</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">
<p class="title"><strong>Create an application shortcut</strong></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>
</div>
</header>
</header>
</div>
<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">

View file

@ -5,19 +5,20 @@ $(document).ready(async function() {
const $wallpaperSwitch = $("#settings_autoSetWallpaperSwitch");
const $wallpaperSwitchTogglerName = $("#settings_autoSetWallpaperTogglerName");
$.ajax({
url: "http://localhost:3000/api/get/settings",
type: "GET",
success: function(data) {
if (data["wallpaper"] === 1) {
$wallpaperSwitch.attr("checked", "true");
$wallpaperSwitchTogglerName.text("On");
}
if (data["startup"] === 1) {
$startupSwitch.attr("checked", "true");
$startupSwitchTogglerName.text("On");
}
},
const $loggingSwitch = $("#settings_saveLoggSwitch");
const $loggingSwitchTogglerName = $("#settings_saveLoggTogglerName");
getSettings().then((data) => {
if (data["wallpaper"] === 1) {
$wallpaperSwitch.attr("checked", "true");
$wallpaperSwitchTogglerName.text("On");
} else if (data["startup"] === 1) {
$startupSwitch.attr("checked", "true");
$startupSwitchTogglerName.text("On");
} else if (data["save_logg"] === 1) {
$loggingSwitch.attr("checked", "true");
$loggingSwitchTogglerName.text("On");
}
});
$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) {
if (message === null) {
return "Required parameter 'message' is missing.";
}
$(".toast-body").text(message);
let toastLiveExample = document.getElementById('liveToast');
let toast = new bootstrap.Toast(toastLiveExample);
toast.show();
}
/**
* @param {Number} i
*/
function editStartup(i) {
if (i !== 1 || i !== 0 || i === null) {
return "Required parameter 'i' is missing.";
}
return $.ajax({
url: "http://localhost:3000/api/update/startup",
type: "POST",
@ -133,4 +196,11 @@ function editStartup(i) {
"startup": i
},
});
}
function getSettings() {
return $.ajax({
url: "http://localhost:3000/api/get/settings",
type: "GET",
});
}