Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devzat logger CSV File Integration #211

Closed
wants to merge 17 commits into from
42 changes: 41 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
_ "embed"
"encoding/csv"
"encoding/json"
"fmt"
"image"
Expand Down Expand Up @@ -895,11 +896,50 @@
}

// bansContains reports if the addr or id is found in the bans list
func bansContains(b []Ban, addr string, id string) bool {
func bansContains(b []Ban, addr string, id string) bool {
for i := 0; i < len(b); i++ {
if b[i].Addr == addr || b[i].ID == id {
return true
}
}
return false
}

func logUserActivity(user *User, action string, loggingEnabled bool) {
if !loggingEnabled {
// logging on devzat is disabled by default, to enable logging please switch the statement to true
return // If logging is not enabled, exit the function immediately
}

timestamp := time.Now().Format("2010-03-07 15:04:05")
message := ""

Check failure on line 915 in main.go

View workflow job for this annotation

GitHub Actions / build_devzat

message declared and not used

switch action {
case "login":
message = fmt.Sprintf("%s joined at %s", user.Name, timestamp)
case "logout":
message = fmt.Sprintf("%s left at %s", user.Name, timestamp)
default:
message = "Invalid Action"
}

file, err := os.OpenFile("users.csv", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error Opening CSV File:", err)
return
}
defer file.Close()

writer := csv.NewWriter(file)

record := []string{user.Name, action, timestamp}
if err := writer.Write(record); err != nil {
fmt.Println("Error writing to CSV File:", err)
}

writer.Flush()

if err := writer.Error(); err != nil {
fmt.Println("Error flushing CSV writer:", err)
}
}
Loading