From 783179511ad901f6c233a7f1831c8ddf8dcb8220 Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Fri, 27 Oct 2023 16:39:48 +0330 Subject: [PATCH] Add debug logs --- custom/command_client.go | 18 ++++++++++-------- custom/command_server.go | 17 ++++++++++------- custom/commands.go | 17 +++++++++++++---- custom/custom.go | 28 +++++++++++++++++++++++++--- 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/custom/command_client.go b/custom/command_client.go index b4fb98d..0a0cc8b 100644 --- a/custom/command_client.go +++ b/custom/command_client.go @@ -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 { diff --git a/custom/command_server.go b/custom/command_server.go index 786f45b..d90bfe1 100644 --- a/custom/command_server.go +++ b/custom/command_server.go @@ -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() } diff --git a/custom/commands.go b/custom/commands.go index e76565f..0178f7c 100644 --- a/custom/commands.go +++ b/custom/commands.go @@ -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, diff --git a/custom/custom.go b/custom/custom.go index 246abcd..9ec5fc2 100644 --- a/custom/custom.go +++ b/custom/custom.go @@ -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()) }