mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-03-01 01:20:49 +03:00
[logs] combine with xray logs #1300
This commit is contained in:
parent
4cb67fd1c3
commit
070fd52d86
53
xray/log_writer.go
Normal file
53
xray/log_writer.go
Normal file
@ -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
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package xray
|
package xray
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@ -10,16 +9,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"x-ui/config"
|
"x-ui/config"
|
||||||
"x-ui/logger"
|
"x-ui/logger"
|
||||||
"x-ui/util/common"
|
"x-ui/util/common"
|
||||||
|
|
||||||
"github.com/Workiva/go-datastructures/queue"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetBinaryName() string {
|
func GetBinaryName() string {
|
||||||
@ -101,7 +96,7 @@ type process struct {
|
|||||||
onlineClients []string
|
onlineClients []string
|
||||||
|
|
||||||
config *Config
|
config *Config
|
||||||
lines *queue.Queue
|
logWriter *LogWriter
|
||||||
exitErr error
|
exitErr error
|
||||||
startTime time.Time
|
startTime time.Time
|
||||||
}
|
}
|
||||||
@ -110,7 +105,7 @@ func newProcess(config *Config) *process {
|
|||||||
return &process{
|
return &process{
|
||||||
version: "Unknown",
|
version: "Unknown",
|
||||||
config: config,
|
config: config,
|
||||||
lines: queue.New(100),
|
logWriter: NewLogWriter(),
|
||||||
startTime: time.Now(),
|
startTime: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,17 +125,10 @@ func (p *process) GetErr() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *process) GetResult() string {
|
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()
|
return p.exitErr.Error()
|
||||||
}
|
}
|
||||||
items, _ := p.lines.TakeUntil(func(item interface{}) bool {
|
return p.logWriter.lastLine
|
||||||
return true
|
|
||||||
})
|
|
||||||
lines := make([]string, 0, len(items))
|
|
||||||
for _, item := range items {
|
|
||||||
lines = append(lines, item.(string))
|
|
||||||
}
|
|
||||||
return strings.Join(lines, "\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *process) GetVersion() string {
|
func (p *process) GetVersion() string {
|
||||||
@ -215,54 +203,14 @@ func (p *process) Start() (err error) {
|
|||||||
cmd := exec.Command(GetBinaryPath(), "-c", configPath)
|
cmd := exec.Command(GetBinaryPath(), "-c", configPath)
|
||||||
p.cmd = cmd
|
p.cmd = cmd
|
||||||
|
|
||||||
stdReader, err := cmd.StdoutPipe()
|
cmd.Stdout = p.logWriter
|
||||||
if err != nil {
|
cmd.Stderr = p.logWriter
|
||||||
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))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.exitErr = err
|
p.exitErr = err
|
||||||
}
|
}
|
||||||
wg.Wait()
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
p.refreshVersion()
|
p.refreshVersion()
|
||||||
|
Loading…
Reference in New Issue
Block a user