Skip to content

Commit

Permalink
fix(libcore): just print log once
Browse files Browse the repository at this point in the history
Signed-off-by: HystericalDragon <[email protected]>
  • Loading branch information
xchacha20-poly1305 committed Mar 30, 2024
1 parent 462d400 commit c1241f3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract class BoxInstance(

protected open suspend fun loadConfig() {
NekoJSInterface.Default.destroyAllJsi()
box = Libcore.newSingBoxInstance(config.config, false)
box = Libcore.newSingBoxInstance(config.config)
}

open suspend fun init() {
Expand Down Expand Up @@ -265,4 +265,4 @@ abstract class BoxInstance(
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TestInstance(profile: ProxyEntity, val link: String, val timeout: Int) :
override suspend fun loadConfig() {
// don't call destroyAllJsi here
if (BuildConfig.DEBUG) Logs.d(config.config)
box = Libcore.newSingBoxInstance(config.config, true)
box = Libcore.newSingBoxInstance(config.config)
}

}
9 changes: 3 additions & 6 deletions libcore/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type BoxInstance struct {
servicePauseFields
}

func NewSingBoxInstance(config string, forTest bool) (b *BoxInstance, err error) {
func NewSingBoxInstance(config string) (b *BoxInstance, err error) {
defer catchPanic("NewSingBoxInstance", func(panicErr error) { err = panicErr })

// parse options
Expand All @@ -66,12 +66,9 @@ func NewSingBoxInstance(config string, forTest bool) (b *BoxInstance, err error)
Options: options,
Context: ctx,
PlatformInterface: platformWrapper,
PlatformLogWriter: nil, // Added in log.go
}
// If set platformLogWrapper, box will set something about cache file,
// which will panic with simple configuration.
if !forTest {
boxOption.PlatformLogWriter = platformLogWrapper
}

instance, err := box.New(boxOption)
if err != nil {
cancel()
Expand Down
8 changes: 4 additions & 4 deletions libcore/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package libcore

import (
"crypto/x509"
"log"
_ "unsafe" // for go:linkname

"github.com/sagernet/sing-box/log"
scribe "github.com/xchacha20-poly1305/TLS-scribe"
"github.com/xchacha20-poly1305/cazilla"
)
Expand All @@ -15,7 +15,7 @@ var systemRoots *x509.CertPool
func updateRootCACerts(pem []byte, enabledCazilla bool) {
roots := func(useMozilla bool) *x509.CertPool {
if useMozilla {
log.Println("Using cazilla.")
log.Info("Using cazilla.")
return cazilla.CA
}

Expand All @@ -25,9 +25,9 @@ func updateRootCACerts(pem []byte, enabledCazilla bool) {

if len(pem) > 0 {
if roots.AppendCertsFromPEM(pem) {
log.Println("external ca.pem was loaded")
log.Info("external ca.pem was loaded")
} else {
log.Println("failed to append certificates from pem")
log.Warn("failed to append certificates from pem")
}
}
systemRoots = roots
Expand Down
60 changes: 29 additions & 31 deletions libcore/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@ package libcore

import (
"context"
"fmt"
"io"
"log"
stdlog "log"
"os"
"syscall"
"time"

boxlog "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/log"
E "github.com/sagernet/sing/common/exceptions"
)

func LogDebug(l string) {
boxlog.Debug(l)
log.Debug(l)
}

func LogInfo(l string) {
boxlog.Info(l)
log.Info(l)
}

func LogWarning(l string) {
boxlog.Warn(l)
log.Warn(l)
}

func LogError(l string) {
boxlog.Error(l)
log.Error(l)
}

var platformLogWrapper *logWriter
Expand All @@ -36,23 +35,23 @@ func setupLog(maxSize int64, path string, enableLog, notTruncateOnStart bool) (e
return
}

var f *os.File
f, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
var file *os.File
file, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
if err == nil {
fd := int(f.Fd())
fd := int(file.Fd())
if !notTruncateOnStart {
_ = syscall.Flock(fd, syscall.LOCK_EX)
// Check if need truncate
if size, _ := f.Seek(0, io.SeekEnd); size > maxSize {
if size, _ := file.Seek(0, io.SeekEnd); size > maxSize {
// read oldBytes for maxSize
_, _ = f.Seek(-maxSize, io.SeekCurrent)
oldBytes, err := io.ReadAll(f)
_, _ = file.Seek(-maxSize, io.SeekCurrent)
oldBytes, err := io.ReadAll(file)
if err == nil {
// truncate file
err = f.Truncate(0)
err = file.Truncate(0)
// write oldBytes
if err == nil {
_, _ = f.Write(oldBytes)
_, _ = file.Write(oldBytes)
}
}
}
Expand All @@ -63,21 +62,20 @@ func setupLog(maxSize int64, path string, enableLog, notTruncateOnStart bool) (e
}

if err != nil {
log.Println(E.Cause(err, "open log"))
stdlog.Println(E.Cause(err, "open log"))
}

//
platformLogWrapper = &logWriter{
disable: !enableLog,
writer: f,
writer: file,
}
// setup std log
log.SetFlags(log.LstdFlags | log.LUTC)
log.SetOutput(platformLogWrapper)
stdlog.SetFlags(stdlog.LstdFlags | stdlog.LUTC)
stdlog.SetOutput(platformLogWrapper)

// setup box default log
boxlog.SetStdLogger(boxlog.NewDefaultFactory(context.Background(),
boxlog.Formatter{BaseTime: time.Now(), DisableColors: false},
log.SetStdLogger(log.NewDefaultFactory(context.Background(),
log.Formatter{BaseTime: time.Now(), DisableColors: false},
os.Stderr,
"",
platformLogWrapper,
Expand All @@ -86,7 +84,7 @@ func setupLog(maxSize int64, path string, enableLog, notTruncateOnStart bool) (e
return
}

var _ boxlog.PlatformWriter = (*logWriter)(nil)
var _ log.PlatformWriter = (*logWriter)(nil)

type logWriter struct {
disable bool
Expand All @@ -97,8 +95,8 @@ func (w *logWriter) DisableColors() bool {
return false
}

func (w *logWriter) WriteMessage(level boxlog.Level, message string) {
_, _ = io.WriteString(w.writer, fmt.Sprintf("%s\n", message))
func (w *logWriter) WriteMessage(_ log.Level, _ string) {
//_, _ = io.WriteString(w.writer, fmt.Sprintf("%s\n", message))
}

var _ io.Writer = (*logWriter)(nil)
Expand All @@ -108,24 +106,24 @@ func (w *logWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

f, isFile := w.writer.(*os.File)
file, isFile := w.writer.(*os.File)
if isFile {
fd := int(f.Fd())
fd := int(file.Fd())
_ = syscall.Flock(fd, syscall.LOCK_EX)
defer syscall.Flock(fd, syscall.LOCK_UN)
}
return w.writer.Write(p)
}

func (w *logWriter) truncate() {
if f, ok := w.writer.(*os.File); ok {
_ = f.Truncate(0)
if file, isFile := w.writer.(*os.File); isFile {
_ = file.Truncate(0)
}
}

func (w *logWriter) Close() error {
if f, ok := w.writer.(*os.File); ok {
return f.Close()
if file, isFile := w.writer.(*os.File); isFile {
return file.Close()
}

return nil
Expand Down

0 comments on commit c1241f3

Please sign in to comment.