2022-09-25 11:04:32 +03:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Logger(text string) {
|
|
|
|
if !FileExists("logger.log") {
|
|
|
|
err := CreateFile("logger.log")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile("logger.log", os.O_APPEND|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
|
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-10-30 20:12:26 +03:00
|
|
|
_, err = f.Write([]byte(now.Format("Mon Jan 2 15:04:05 2006") + " | " + 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 {
|
|
|
|
fo, err := os.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := fo.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|