From 29a4360bd0ea6f24f806be8fee9227ab882d65f2 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Wed, 25 May 2022 10:37:37 -0400 Subject: [PATCH 1/4] added count to log entries --- logger/logger.go | 26 ++++++++++++++------------ logger/types.go | 6 ++++++ 2 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 logger/types.go diff --git a/logger/logger.go b/logger/logger.go index 173d63c1..72eed3e8 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -16,7 +16,7 @@ const TimeFormatDay = "2006-01-02" const TimeFormat = "2006-01-02 15:04:05" // == fields == -var currentLogs = make(map[string]string) +var currentLogs = make(map[string]entry) var mu sync.Mutex var program string @@ -38,7 +38,10 @@ func Log(verbosity int, message ...string) { fmt.Printf("[%s] %s %s \n", program, currentTime.Format(TimeFormat), currentMessage) } if program == "netmaker" { - currentLogs[currentMessage] = currentTime.Format("2006-01-02 15:04:05.999999999") + currentLogs[currentMessage] = entry{ + Time: currentTime.Format("2006-01-02 15:04:05.999999999"), + Count: currentLogs[currentMessage].Count + 1, + } } } @@ -51,17 +54,17 @@ func Dump() string { type keyVal struct { Key string Value time.Time + Count int } - var mu sync.Mutex - mu.Lock() - defer mu.Unlock() var dumpLogs = make([]keyVal, 0, len(currentLogs)) - for key, value := range currentLogs { - parsedTime, err := time.Parse(TimeFormat, value) + for key := range currentLogs { + currentEntry := currentLogs[key] + parsedTime, err := time.Parse(TimeFormat, currentEntry.Time) if err == nil { dumpLogs = append(dumpLogs, keyVal{ Key: key, Value: parsedTime, + Count: currentEntry.Count, }) } } @@ -71,7 +74,7 @@ func Dump() string { for i := range dumpLogs { var currLog = dumpLogs[i] - dumpString += MakeString(" ", "[netmaker]", currLog.Value.Format(TimeFormat), currLog.Key, "\n") + dumpString += MakeString(" ", "[netmaker]", currLog.Value.Format(TimeFormat), currLog.Key, fmt.Sprintf("(%d)", currLog.Count), "\n") } resetLogs() @@ -107,9 +110,6 @@ func Retrieve(filePath string) string { // FatalLog - exits os after logging func FatalLog(message ...string) { - var mu sync.Mutex - mu.Lock() - defer mu.Unlock() fmt.Printf("[netmaker] Fatal: %s \n", MakeString(" ", message...)) os.Exit(2) } @@ -118,5 +118,7 @@ func FatalLog(message ...string) { // resetLogs - reallocates logs map func resetLogs() { - currentLogs = make(map[string]string) + mu.Lock() + defer mu.Unlock() + currentLogs = make(map[string]entry) } diff --git a/logger/types.go b/logger/types.go new file mode 100644 index 00000000..14868896 --- /dev/null +++ b/logger/types.go @@ -0,0 +1,6 @@ +package logger + +type entry struct { + Time string + Count int +} From ec90f4dc8e7c887019a550027221712fa6186276 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Wed, 25 May 2022 10:46:00 -0400 Subject: [PATCH 2/4] better logging handling --- logger/logger.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/logger/logger.go b/logger/logger.go index 72eed3e8..73b20584 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -50,6 +50,8 @@ func Dump() string { if program != "netmaker" { return "" } + mu.Lock() + defer mu.Unlock() var dumpString = "" type keyVal struct { Key string From 679251cadf39b0ec8f55dfc5da7d0c4a10f3bdff Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Wed, 25 May 2022 10:50:33 -0400 Subject: [PATCH 3/4] removed mutex --- logger/logger.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 73b20584..72eed3e8 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -50,8 +50,6 @@ func Dump() string { if program != "netmaker" { return "" } - mu.Lock() - defer mu.Unlock() var dumpString = "" type keyVal struct { Key string From 333a140c7414c4cd7514e29b569a62b520b1641d Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Wed, 25 May 2022 12:46:57 -0400 Subject: [PATCH 4/4] moved mutex --- logger/logger.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 72eed3e8..6700c48b 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -50,6 +50,8 @@ func Dump() string { if program != "netmaker" { return "" } + mu.Lock() + defer mu.Unlock() var dumpString = "" type keyVal struct { Key string @@ -118,7 +120,5 @@ func FatalLog(message ...string) { // resetLogs - reallocates logs map func resetLogs() { - mu.Lock() - defer mu.Unlock() currentLogs = make(map[string]entry) }