Skip to content

Commit

Permalink
feat: apply log level for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
xchacha20-poly1305 committed Oct 4, 2024
1 parent 3aca3f4 commit fab5bb3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/io/nekohasekai/sagernet/SagerNet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class SagerNet : Application(),
filesDir.absolutePath + "/",
externalAssets.absolutePath + "/",
DataStore.logBufSize,
DataStore.logLevel > 0,
DataStore.logLevel,
DataStore.rulesProvider == 0,
)
Libcore.updateRootCACerts(DataStore.enabledCazilla)
Expand Down
4 changes: 2 additions & 2 deletions libcore/libcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Kill() {
}

func InitCore(process, cachePath, internalAssets, externalAssets string,
maxLogSizeKb int32, logEnable bool,
maxLogSizeKb int32, logLevel int32,
useOfficialAssets bool,
) {
defer catchPanic("InitCore", func(panicErr error) { log.Error(panicErr) })
Expand All @@ -30,7 +30,7 @@ func InitCore(process, cachePath, internalAssets, externalAssets string,
if maxLogSizeKb < 50 {
maxLogSizeKb = 50
}
_ = setupLog(int64(maxLogSizeKb)*1024, filepath.Join(externalAssets, "stderr.log"), logEnable, isBgProcess)
_ = setupLog(int64(maxLogSizeKb)*1024, filepath.Join(externalAssets, "stderr.log"), log.Level(logLevel), isBgProcess)

// Set up some component
go func() {
Expand Down
16 changes: 8 additions & 8 deletions libcore/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func LogClear() {

var platformLogWrapper *logWriter

func setupLog(maxSize int64, path string, enableLog, notTruncateOnStart bool) (err error) {
func setupLog(maxSize int64, path string, level log.Level, notTruncateOnStart bool) (err error) {
if platformLogWrapper != nil {
return
}
Expand Down Expand Up @@ -72,10 +72,9 @@ func setupLog(maxSize int64, path string, enableLog, notTruncateOnStart bool) (e
}

platformLogWrapper = &logWriter{
disable: !enableLog,
writer: file,
writer: file,
}
log.SetStdLogger(log.NewDefaultFactory(
factory := log.NewDefaultFactory(
context.Background(),
log.Formatter{
BaseTime: time.Now(),
Expand All @@ -86,7 +85,9 @@ func setupLog(maxSize int64, path string, enableLog, notTruncateOnStart bool) (e
"",
platformLogWrapper,
false,
).Logger())
)
factory.SetLevel(level)
log.SetStdLogger(factory.Logger())
// setup std log
stdlog.SetFlags(stdlog.LstdFlags | stdlog.LUTC)
stdlog.SetOutput(platformLogWrapper)
Expand All @@ -97,8 +98,7 @@ func setupLog(maxSize int64, path string, enableLog, notTruncateOnStart bool) (e
var _ log.PlatformWriter = (*logWriter)(nil)

type logWriter struct {
disable bool
writer io.Writer
writer io.Writer
}

func (w *logWriter) DisableColors() bool {
Expand All @@ -114,7 +114,7 @@ func (w *logWriter) WriteMessage(_ log.Level, message string) {
var _ io.Writer = (*logWriter)(nil)

func (w *logWriter) Write(p []byte) (n int, err error) {
if w.disable || w.writer == nil {
if w.writer == nil {
return len(p), nil
}

Expand Down

0 comments on commit fab5bb3

Please sign in to comment.