From 341baf69ded49f7ef4b646ccffacc1028bc51415 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Thu, 6 Jun 2024 23:01:48 +0200 Subject: [PATCH] [log] display logs --- backend/api/api.go | 6 ++ backend/service/server.go | 24 +++++++ frontend/src/components/Main.vue | 40 +++++++++++- frontend/src/layouts/modals/Logs.vue | 94 ++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 frontend/src/layouts/modals/Logs.vue diff --git a/backend/api/api.go b/backend/api/api.go index 5ba3b33..cbf2c03 100644 --- a/backend/api/api.go +++ b/backend/api/api.go @@ -151,6 +151,12 @@ func (a *APIHandler) getHandler(c *gin.Context) { case "onlines": onlines, err := a.StatsService.GetOnlines() jsonObj(c, onlines, err) + case "logs": + service := c.Query("s") + count := c.Query("c") + level := c.Query("l") + logs := a.ServerService.GetLogs(service, count, level) + jsonObj(c, logs, nil) default: jsonMsg(c, "API call", nil) } diff --git a/backend/service/server.go b/backend/service/server.go index 73074c7..e4a92fd 100644 --- a/backend/service/server.go +++ b/backend/service/server.go @@ -1,10 +1,13 @@ package service import ( + "bytes" "os" + "os/exec" "runtime" "s-ui/config" "s-ui/logger" + "strconv" "strings" "github.com/shirou/gopsutil/v3/cpu" @@ -135,3 +138,24 @@ func (s *ServerService) GetSystemInfo() map[string]interface{} { return info } + +func (s *ServerService) GetLogs(service string, count string, level string) []string { + c, _ := strconv.Atoi(count) + var lines []string + if service == "sing-box" { + cmdArgs := []string{"journalctl", "-u", service, "--no-pager", "-n", count, "-p", level} + // Run the command + cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + return []string{"Failed to run journalctl command!"} + } + lines = strings.Split(out.String(), "\n") + } else { + lines = logger.GetLogs(c, level) + } + + return lines +} diff --git a/frontend/src/components/Main.vue b/frontend/src/components/Main.vue index 103acb2..a9052b3 100644 --- a/frontend/src/components/Main.vue +++ b/frontend/src/components/Main.vue @@ -1,4 +1,10 @@