Skip to content

Commit

Permalink
[logs] combine with xray logs #1300
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza0 committed Dec 10, 2023
1 parent 4cb67fd commit 070fd52
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 58 deletions.
53 changes: 53 additions & 0 deletions xray/log_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package xray

import (
"strings"
"x-ui/logger"
)

func NewLogWriter() *LogWriter {
return &LogWriter{}
}

type LogWriter struct {
lastLine string
}

func (lw *LogWriter) Write(m []byte) (n int, err error) {
// Convert the data to a string
message := strings.TrimSpace(string(m))
messages := strings.Split(message, "\n")
lw.lastLine = messages[len(messages)-1]

for _, msg := range messages {
// Remove timestamp
messageBody := strings.TrimSpace(strings.SplitN(msg, " ", 3)[2])

// Find level in []
startIndex := strings.Index(messageBody, "[")
endIndex := strings.Index(messageBody, "]")
if startIndex != -1 && endIndex != -1 {
level := strings.TrimSpace(messageBody[startIndex+1 : endIndex])
msgBody := "XRAY: " + strings.TrimSpace(messageBody[endIndex+1:])

// Map the level to the appropriate logger function
switch level {
case "Debug":
logger.Debug(msgBody)
case "Info":
logger.Info(msgBody)
case "Warning":
logger.Warning(msgBody)
case "Error":
logger.Error(msgBody)
default:
logger.Debug("XRAY: " + msg)
}
} else if msg != "" {
logger.Debug("XRAY: " + msg)
return len(m), nil
}
}

return len(m), nil
}
64 changes: 6 additions & 58 deletions xray/process.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package xray

import (
"bufio"
"bytes"
"encoding/json"
"errors"
Expand All @@ -10,16 +9,12 @@ import (
"os"
"os/exec"
"runtime"
"strings"
"sync"
"syscall"
"time"

"x-ui/config"
"x-ui/logger"
"x-ui/util/common"

"github.com/Workiva/go-datastructures/queue"
)

func GetBinaryName() string {
Expand Down Expand Up @@ -101,7 +96,7 @@ type process struct {
onlineClients []string

config *Config
lines *queue.Queue
logWriter *LogWriter
exitErr error
startTime time.Time
}
Expand All @@ -110,7 +105,7 @@ func newProcess(config *Config) *process {
return &process{
version: "Unknown",
config: config,
lines: queue.New(100),
logWriter: NewLogWriter(),
startTime: time.Now(),
}
}
Expand All @@ -130,17 +125,10 @@ func (p *process) GetErr() error {
}

func (p *process) GetResult() string {
if p.lines.Empty() && p.exitErr != nil {
if len(p.logWriter.lastLine) == 0 && p.exitErr != nil {
return p.exitErr.Error()
}
items, _ := p.lines.TakeUntil(func(item interface{}) bool {
return true
})
lines := make([]string, 0, len(items))
for _, item := range items {
lines = append(lines, item.(string))
}
return strings.Join(lines, "\n")
return p.logWriter.lastLine
}

func (p *process) GetVersion() string {
Expand Down Expand Up @@ -215,54 +203,14 @@ func (p *process) Start() (err error) {
cmd := exec.Command(GetBinaryPath(), "-c", configPath)
p.cmd = cmd

stdReader, err := cmd.StdoutPipe()
if err != nil {
return err
}
errReader, err := cmd.StderrPipe()
if err != nil {
return err
}

var wg sync.WaitGroup
wg.Add(2)

go func() {
defer wg.Done()
reader := bufio.NewReaderSize(stdReader, 8192)
for {
line, _, err := reader.ReadLine()
if err != nil {
return
}
if p.lines.Len() >= 100 {
p.lines.Get(1)
}
p.lines.Put(string(line))
}
}()

go func() {
defer wg.Done()
reader := bufio.NewReaderSize(errReader, 8192)
for {
line, _, err := reader.ReadLine()
if err != nil {
return
}
if p.lines.Len() >= 100 {
p.lines.Get(1)
}
p.lines.Put(string(line))
}
}()
cmd.Stdout = p.logWriter
cmd.Stderr = p.logWriter

go func() {
err := cmd.Run()
if err != nil {
p.exitErr = err
}
wg.Wait()
}()

p.refreshVersion()
Expand Down

0 comments on commit 070fd52

Please sign in to comment.