Skip to content

Commit

Permalink
improve systemd or docker detection
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza0 committed Oct 29, 2024
1 parent 1d46d72 commit 282e244
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
9 changes: 9 additions & 0 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ func GetDefaultConfig() string {
func GetEnvApi() string {
return os.Getenv("SINGBOX_API")
}

func IsSystemd() bool {
pid := os.Getppid()
cmdline, err := os.ReadFile(fmt.Sprintf("/proc/%d/comm", pid))
if err != nil {
return false
}
return string(cmdline) == "systemd\n"
}
4 changes: 2 additions & 2 deletions backend/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logger
import (
"fmt"
"os"
"s-ui/config"
"time"

"github.com/op/go-logging"
Expand All @@ -22,14 +23,13 @@ func InitLogger(level logging.Level) {
var err error
var backend logging.Backend
var format logging.Formatter
ppid := os.Getppid()

backend, err = logging.NewSyslogBackend("")
if err != nil {
println("Unable to use syslog: " + err.Error())
backend = logging.NewLogBackend(os.Stderr, "", 0)
}
if ppid > 1 && err != nil {
if config.IsSystemd() && err != nil {
format = logging.MustStringFormatter(`%{time:2006/01/02 15:04:05} %{level} - %{message}`)
} else {
format = logging.MustStringFormatter(`%{level} - %{message}`)
Expand Down
2 changes: 2 additions & 0 deletions backend/service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

var ApiAddr string
var LastUpdate int64
var IsSystemd bool

type ConfigService struct {
ClientService
Expand All @@ -38,6 +39,7 @@ func NewConfigService() *ConfigService {
}

func (s *ConfigService) InitConfig() error {
IsSystemd = config.IsSystemd()
configPath := config.GetBinFolderPath()
data, err := os.ReadFile(configPath + "/config.json")
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions backend/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,9 @@ func (s *ServerService) GetLogs(service string, count string, level string) []st
if service == "s-ui" {
return logger.GetLogs(c, level)
}
ppid := os.Getppid()
var lines []string
var cmdArgs []string
if ppid > 1 {
if IsSystemd {
cmdArgs = []string{"journalctl", "-u", service, "--no-pager", "-n", count, "-p", level}
} else {
cmdArgs = []string{"tail", "/logs/" + service + ".log", "-n", count}
Expand Down

0 comments on commit 282e244

Please sign in to comment.