Add debug logs

This commit is contained in:
problematicconsumer
2023-10-27 16:39:48 +03:30
parent a390c7fc76
commit 783179511a
4 changed files with 58 additions and 22 deletions

View File

@@ -6,27 +6,28 @@ import (
"github.com/hiddify/libcore/bridge"
"github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/log"
)
type CommandClientHandler struct {
name string
port int64
port int64
logger log.Logger
}
func (cch *CommandClientHandler) Connected() {
fmt.Printf("[%s] CONNECTED\n", cch.name)
cch.logger.Debug("CONNECTED")
}
func (cch *CommandClientHandler) Disconnected(message string) {
fmt.Printf("[%s] DISCONNECTED: %s\n", cch.name, message)
cch.logger.Debug("DISCONNECTED: ", message)
}
func (cch *CommandClientHandler) ClearLog() {
fmt.Printf("[%s] clear log\n", cch.name)
cch.logger.Debug("clear log")
}
func (cch *CommandClientHandler) WriteLog(message string) {
fmt.Printf("[%s] log: %s\n", cch.name, message)
cch.logger.Debug("log: ", message)
}
func (cch *CommandClientHandler) WriteStatus(message *libbox.StatusMessage) {
@@ -40,6 +41,7 @@ func (cch *CommandClientHandler) WriteStatus(message *libbox.StatusMessage) {
"downlink-total": message.DownlinkTotal,
},
)
cch.logger.Debug("Memory: ", libbox.FormatBytes(message.Memory), ", MemoryInuse: ", libbox.FormatBytes(message.MemoryInuse))
if err != nil {
bridge.SendStringToPort(cch.port, fmt.Sprintf("error: %e", err))
} else {
@@ -78,11 +80,11 @@ func (cch *CommandClientHandler) WriteGroups(message libbox.OutboundGroupIterato
}
func (cch *CommandClientHandler) InitializeClashMode(modeList libbox.StringIterator, currentMode string) {
fmt.Printf("[%s] clash mode: %s\n", cch.name, currentMode)
cch.logger.Debug("initial clash mode: ", currentMode)
}
func (cch *CommandClientHandler) UpdateClashMode(newMode string) {
fmt.Printf("[%s] update clash mode: %s\n", cch.name, newMode)
cch.logger.Debug("update clash mode: ", newMode)
}
type OutboundGroup struct {

View File

@@ -7,10 +7,12 @@ import (
var commandServer *libbox.CommandServer
type CommandServerHandler struct{}
type CommandServerHandler struct {
logger log.Logger
}
func (csh *CommandServerHandler) ServiceReload() error {
log.Trace("[Command Server Handler] Reloading service")
csh.logger.Trace("Reloading service")
propagateStatus(Starting)
if commandServer != nil {
commandServer.SetService(nil)
@@ -24,17 +26,18 @@ func (csh *CommandServerHandler) ServiceReload() error {
}
func (csh *CommandServerHandler) GetSystemProxyStatus() *libbox.SystemProxyStatus {
log.Trace("[Command Server Handler] Getting system proxy status")
csh.logger.Trace("Getting system proxy status")
return &libbox.SystemProxyStatus{Available: true, Enabled: false}
}
func (csh *CommandServerHandler) SetSystemProxyEnabled(isEnabled bool) error {
log.Trace("[Command Server Handler] Setting system proxy status")
csh.logger.Trace("Setting system proxy status, enabled? ", isEnabled)
return csh.ServiceReload()
}
func startCommandServer() error {
log.Trace("[Command Server Handler] Starting command server")
commandServer = libbox.NewCommandServer(&CommandServerHandler{}, 300)
func startCommandServer(logFactory log.Factory) error {
logger := logFactory.NewLogger("[Command Server Handler]")
logger.Trace("Starting command server")
commandServer = libbox.NewCommandServer(&CommandServerHandler{logger: logger}, 300)
return commandServer.Start()
}

View File

@@ -1,17 +1,23 @@
package main
import "github.com/sagernet/sing-box/experimental/libbox"
import (
"github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/log"
)
var (
statusClient *libbox.CommandClient
groupClient *libbox.CommandClient
)
func StartCommand(command int32, port int64) error {
func StartCommand(command int32, port int64, logFactory log.Factory) error {
switch command {
case libbox.CommandStatus:
statusClient = libbox.NewCommandClient(
&CommandClientHandler{port: port, name: "status"},
&CommandClientHandler{
port: port,
logger: logFactory.NewLogger("[Status Command Client]"),
},
&libbox.CommandClientOptions{
Command: libbox.CommandStatus,
StatusInterval: 1000000000,
@@ -20,7 +26,10 @@ func StartCommand(command int32, port int64) error {
return statusClient.Connect()
case libbox.CommandGroup:
groupClient = libbox.NewCommandClient(
&CommandClientHandler{port: port, name: "group"},
&CommandClientHandler{
port: port,
logger: logFactory.NewLogger("[Group Command Client]"),
},
&libbox.CommandClientOptions{
Command: libbox.CommandGroup,
StatusInterval: 1000000000,

View File

@@ -6,6 +6,7 @@ package main
import "C"
import (
"encoding/json"
"io"
"os"
"time"
"unsafe"
@@ -19,6 +20,7 @@ import (
var box *libbox.BoxService
var configOptions *shared.ConfigOptions
var activeConfigPath *string
var logFactory *log.Factory
//export setupOnce
func setupOnce(api unsafe.Pointer) {
@@ -26,9 +28,29 @@ func setupOnce(api unsafe.Pointer) {
}
//export setup
func setup(baseDir *C.char, workingDir *C.char, tempDir *C.char, statusPort C.longlong) {
func setup(baseDir *C.char, workingDir *C.char, tempDir *C.char, statusPort C.longlong, debug bool) (CErr *C.char) {
defer shared.DeferPanicToError("setup", func(err error) {
CErr = C.CString(err.Error())
})
Setup(C.GoString(baseDir), C.GoString(workingDir), C.GoString(tempDir))
statusPropagationPort = int64(statusPort)
var defaultWriter io.Writer
if !debug {
defaultWriter = io.Discard
}
factory, err := log.New(
log.Options{
DefaultWriter: defaultWriter,
BaseTime: time.Now(),
Observable: false,
})
if err != nil {
return C.CString(err.Error())
}
logFactory = &factory
return C.CString("")
}
//export parse
@@ -93,7 +115,7 @@ func startService(delayStart bool) error {
shared.SaveCurrentConfig(sWorkingPath, options)
err = startCommandServer()
err = startCommandServer(*logFactory)
if err != nil {
return stopAndAlert(StartCommandServer, err)
}
@@ -180,7 +202,7 @@ func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
//export startCommandClient
func startCommandClient(command C.int, port C.longlong) *C.char {
err := StartCommand(int32(command), int64(port))
err := StartCommand(int32(command), int64(port), *logFactory)
if err != nil {
return C.CString(err.Error())
}