2022-09-25 11:04:32 +03:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import (
|
2022-11-22 21:36:35 +03:00
|
|
|
"encoding/json"
|
2022-10-30 20:12:26 +03:00
|
|
|
"fmt"
|
2022-09-25 11:04:32 +03:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2022-10-02 12:18:29 +03:00
|
|
|
"strings"
|
2022-09-25 11:04:32 +03:00
|
|
|
"time"
|
2022-11-22 21:36:35 +03:00
|
|
|
|
|
|
|
"github.com/rodkranz/fetch"
|
2022-09-25 11:04:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func Logger(text string) {
|
2022-11-21 22:22:00 +03:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
file := string(now.Format("01.02.2006")) + ".log"
|
|
|
|
|
|
|
|
if getDatabase() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !FileExists("logs") {
|
|
|
|
err := CreateFolder("logs")
|
2022-09-25 11:04:32 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 22:22:00 +03:00
|
|
|
|
|
|
|
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)
|
2022-09-25 11:04:32 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-10-30 20:12:26 +03:00
|
|
|
_, file, line, ok := runtime.Caller(1)
|
|
|
|
|
2022-09-25 11:04:32 +03:00
|
|
|
if !ok {
|
|
|
|
file = "???"
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
Logger(err.Error())
|
|
|
|
}
|
|
|
|
|
2022-10-02 12:18:29 +03:00
|
|
|
if strings.Contains(file, dir) {
|
2022-10-30 20:12:26 +03:00
|
|
|
file = strings.Replace(file, dir, "", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var lineString string
|
|
|
|
if ok {
|
|
|
|
lineString = fmt.Sprintf("%d", line)
|
2022-10-02 12:18:29 +03:00
|
|
|
}
|
2022-09-25 11:04:32 +03:00
|
|
|
|
2022-12-15 21:08:36 +03:00
|
|
|
_, err = f.Write([]byte(now.Format("15:04:05") + " | " + text + " [" + file + "] [" + lineString + "]\n"))
|
2022-09-25 11:04:32 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func FileExists(name string) bool {
|
|
|
|
if _, err := os.Stat(name); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateFile(name string) error {
|
2022-11-21 22:22:00 +03:00
|
|
|
file, err := os.Create("logs/" + name)
|
2022-09-25 11:04:32 +03:00
|
|
|
if err != nil {
|
2022-11-21 22:22:00 +03:00
|
|
|
return nil
|
2022-09-25 11:04:32 +03:00
|
|
|
}
|
2022-11-21 22:22:00 +03:00
|
|
|
|
|
|
|
defer func(file *os.File) {
|
|
|
|
err := file.Close()
|
2022-09-25 11:04:32 +03:00
|
|
|
if err != nil {
|
2022-11-21 22:22:00 +03:00
|
|
|
|
2022-09-25 11:04:32 +03:00
|
|
|
}
|
2022-11-21 22:22:00 +03:00
|
|
|
}(file)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateFolder(name string) error {
|
|
|
|
err := os.Mkdir(name, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-25 11:04:32 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-22 21:36:35 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|