diff --git a/app/log/log.go b/app/log/log.go index 93f3fa968..e3e535536 100644 --- a/app/log/log.go +++ b/app/log/log.go @@ -4,6 +4,7 @@ package log import ( "context" + "runtime" "sync" "v2ray.com/core/common" @@ -30,6 +31,18 @@ func New(ctx context.Context, config *Config) (*Instance, error) { return g, nil } +func isMobile() bool { + return runtime.GOOS == "android" || (runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")) +} + +func createStdLogWriter() log.WriterCreator { + if isMobile() { + return log.CreateDefaultLogWriter() + } + + return log.CreateStdoutLogWriter() +} + func (g *Instance) initAccessLogger() error { switch g.config.AccessLogType { case LogType_File: @@ -39,7 +52,7 @@ func (g *Instance) initAccessLogger() error { } g.accessLogger = log.NewLogger(creator) case LogType_Console: - g.accessLogger = log.NewLogger(log.CreateStdoutLogWriter()) + g.accessLogger = log.NewLogger(createStdLogWriter()) default: } return nil @@ -54,7 +67,7 @@ func (g *Instance) initErrorLogger() error { } g.errorLogger = log.NewLogger(creator) case LogType_Console: - g.errorLogger = log.NewLogger(log.CreateStdoutLogWriter()) + g.errorLogger = log.NewLogger(createStdLogWriter()) default: } return nil diff --git a/common/log/logger.go b/common/log/logger.go index 9a2591cb9..a88b7b668 100644 --- a/common/log/logger.go +++ b/common/log/logger.go @@ -119,6 +119,22 @@ func CreateStdoutLogWriter() WriterCreator { } } +type defaultLogWriter struct{} + +func (defaultLogWriter) Write(b []byte) (int, error) { + log.Print(string(b)) + return len(b), nil +} + +// CreateDefaultLogWriter returns a LogWriterCreator that creates LogWriter to write to default Golang logger. +func CreateDefaultLogWriter() WriterCreator { + return func() Writer { + return &consoleLogWriter{ + logger: log.New(defaultLogWriter{}, "", log.Ldate|log.Ltime), + } + } +} + // CreateFileLogWriter returns a LogWriterCreator that creates LogWriter for the given file. func CreateFileLogWriter(path string) (WriterCreator, error) { file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)