netmaker/logger/logger.go

105 lines
2.3 KiB
Go
Raw Normal View History

2021-12-07 04:31:08 +08:00
package logger
import (
"fmt"
"os"
"sort"
2022-01-27 00:20:46 +08:00
"sync"
2021-12-07 04:31:08 +08:00
"time"
)
2022-02-11 00:07:21 +08:00
// TimeFormatDay - format of the day for timestamps
2021-12-07 04:31:08 +08:00
const TimeFormatDay = "2006-01-02"
2022-02-11 00:07:21 +08:00
// TimeFormat - total time format
2021-12-07 04:31:08 +08:00
const TimeFormat = "2006-01-02 15:04:05"
2022-02-11 00:07:21 +08:00
// == fields ==
2021-12-07 04:31:08 +08:00
var currentLogs = make(map[string]string)
2022-02-11 00:07:21 +08:00
var mu sync.Mutex
2021-12-07 04:31:08 +08:00
// Log - handles adding logs
func Log(verbosity int, message ...string) {
2022-01-31 22:21:19 +08:00
mu.Lock()
defer mu.Unlock()
2021-12-07 04:31:08 +08:00
var currentTime = time.Now()
2022-02-11 00:07:21 +08:00
var currentMessage = MakeString(" ", message...)
2021-12-07 04:31:08 +08:00
if int32(verbosity) <= getVerbose() && getVerbose() >= 0 {
fmt.Printf("[netmaker] %s %s \n", currentTime.Format(TimeFormat), currentMessage)
}
currentLogs[currentMessage] = currentTime.Format("2006-01-02 15:04:05.999999999")
}
// Dump - dumps all logs into a formatted string
func Dump() string {
var dumpString = ""
type keyVal struct {
Key string
Value time.Time
}
2022-01-29 21:50:58 +08:00
var mu sync.Mutex
mu.Lock()
defer mu.Unlock()
2021-12-07 04:31:08 +08:00
var dumpLogs = make([]keyVal, 0, len(currentLogs))
for key, value := range currentLogs {
parsedTime, err := time.Parse(TimeFormat, value)
if err == nil {
dumpLogs = append(dumpLogs, keyVal{
Key: key,
Value: parsedTime,
})
}
}
sort.Slice(dumpLogs, func(i, j int) bool {
return dumpLogs[i].Value.Before(dumpLogs[j].Value)
})
for i := range dumpLogs {
var currLog = dumpLogs[i]
2022-02-11 00:07:21 +08:00
dumpString += MakeString(" ", "[netmaker]", currLog.Value.Format(TimeFormat), currLog.Key, "\n")
2021-12-07 04:31:08 +08:00
}
2022-02-11 00:07:21 +08:00
resetLogs()
2021-12-07 04:31:08 +08:00
return dumpString
}
// DumpFile - appends log dump log file
func DumpFile(filePath string) {
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
2022-02-12 00:11:28 +08:00
fmt.Println(MakeString(" ", "could not open log file", filePath))
2022-02-11 00:07:21 +08:00
return
2021-12-07 04:31:08 +08:00
}
defer f.Close()
if _, err = f.WriteString(Dump()); err != nil {
2022-02-11 00:07:21 +08:00
fmt.Println("could not dump logs")
2021-12-07 04:31:08 +08:00
}
}
// Retrieve - retrieves logs from given file
func Retrieve(filePath string) string {
2022-01-07 04:05:38 +08:00
contents, err := os.ReadFile(filePath)
2021-12-07 04:31:08 +08:00
if err != nil {
panic(err)
}
return string(contents)
}
// FatalLog - exits os after logging
func FatalLog(message ...string) {
2022-01-31 22:21:19 +08:00
var mu sync.Mutex
mu.Lock()
defer mu.Unlock()
2022-02-11 00:07:21 +08:00
fmt.Printf("[netmaker] Fatal: %s \n", MakeString(" ", message...))
2021-12-07 04:31:08 +08:00
os.Exit(2)
}
2022-02-11 00:07:21 +08:00
// == private ==
// resetLogs - reallocates logs map
func resetLogs() {
currentLogs = make(map[string]string)
}