netmaker/logger/logger.go

147 lines
3.1 KiB
Go
Raw Normal View History

2021-12-07 04:31:08 +08:00
package logger
import (
"fmt"
"os"
"path/filepath"
"runtime"
2021-12-07 04:31:08 +08:00
"sort"
"strings"
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 ==
2022-05-25 22:37:37 +08:00
var currentLogs = make(map[string]entry)
2022-02-11 00:07:21 +08:00
var mu sync.Mutex
var program string
func init() {
fullpath, err := os.Executable()
if err != nil {
fullpath = ""
}
program = filepath.Base(fullpath)
}
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...)
2022-07-14 05:22:17 +08:00
if getVerbose() >= 4 {
pc, file, line, ok := runtime.Caller(1)
if !ok {
file = "?"
line = 0
}
fn := runtime.FuncForPC(pc)
var fnName string
if fn == nil {
fnName = "?()"
} else {
fnName = strings.TrimLeft(filepath.Ext(fn.Name()), ".") + "()"
}
currentMessage = fmt.Sprintf("[%s-%d] %s: %s",
filepath.Base(file), line, fnName, currentMessage)
}
2021-12-07 04:31:08 +08:00
if int32(verbosity) <= getVerbose() && getVerbose() >= 0 {
fmt.Printf("[%s] %s %s \n", program, currentTime.Format(TimeFormat), currentMessage)
}
if program == "netmaker" {
2022-05-25 22:37:37 +08:00
currentLogs[currentMessage] = entry{
Time: currentTime.Format("2006-01-02 15:04:05.999999999"),
Count: currentLogs[currentMessage].Count + 1,
}
2021-12-07 04:31:08 +08:00
}
}
// Dump - dumps all logs into a formatted string
func Dump() string {
if program != "netmaker" {
return ""
}
2022-05-26 00:46:57 +08:00
mu.Lock()
defer mu.Unlock()
2021-12-07 04:31:08 +08:00
var dumpString = ""
type keyVal struct {
Key string
Value time.Time
2022-05-25 22:37:37 +08:00
Count int
2021-12-07 04:31:08 +08:00
}
var dumpLogs = make([]keyVal, 0, len(currentLogs))
2022-05-25 22:37:37 +08:00
for key := range currentLogs {
currentEntry := currentLogs[key]
parsedTime, err := time.Parse(TimeFormat, currentEntry.Time)
2021-12-07 04:31:08 +08:00
if err == nil {
dumpLogs = append(dumpLogs, keyVal{
Key: key,
Value: parsedTime,
2022-05-25 22:37:37 +08:00
Count: currentEntry.Count,
2021-12-07 04:31:08 +08:00
})
}
}
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-05-25 22:37:37 +08:00
dumpString += MakeString(" ", "[netmaker]", currLog.Value.Format(TimeFormat), currLog.Key, fmt.Sprintf("(%d)", currLog.Count), "\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) {
if program != "netmaker" {
return
}
2021-12-07 04:31:08 +08:00
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-09-20 03:37:00 +08:00
fmt.Printf("[%s] Fatal: %s \n", program, 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() {
2022-05-25 22:37:37 +08:00
currentLogs = make(map[string]entry)
2022-02-11 00:07:21 +08:00
}