[log] display logs

This commit is contained in:
Alireza Ahmadi
2024-06-06 23:01:48 +02:00
parent dedd4b3ee3
commit 341baf69de
4 changed files with 161 additions and 3 deletions
+24
View File
@@ -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
}