mirror of https://github.com/v2ray/v2ray-core
78 lines
1.4 KiB
Go
78 lines
1.4 KiB
Go
package internal
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/v2ray/v2ray-core/common/platform"
|
|
)
|
|
|
|
type LogWriter interface {
|
|
Log(LogEntry)
|
|
}
|
|
|
|
type NoOpLogWriter struct {
|
|
}
|
|
|
|
func (this *NoOpLogWriter) Log(entry LogEntry) {
|
|
entry.Release()
|
|
}
|
|
|
|
type StdOutLogWriter struct {
|
|
logger *log.Logger
|
|
}
|
|
|
|
func NewStdOutLogWriter() LogWriter {
|
|
return &StdOutLogWriter{
|
|
logger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
|
|
}
|
|
}
|
|
|
|
func (this *StdOutLogWriter) Log(log LogEntry) {
|
|
this.logger.Print(log.String() + platform.LineSeparator())
|
|
log.Release()
|
|
}
|
|
|
|
type FileLogWriter struct {
|
|
queue chan string
|
|
logger *log.Logger
|
|
file *os.File
|
|
}
|
|
|
|
func (this *FileLogWriter) Log(log LogEntry) {
|
|
select {
|
|
case this.queue <- log.String():
|
|
default:
|
|
// We don't expect this to happen, but don't want to block main thread as well.
|
|
}
|
|
log.Release()
|
|
}
|
|
|
|
func (this *FileLogWriter) run() {
|
|
for {
|
|
entry, open := <-this.queue
|
|
if !open {
|
|
break
|
|
}
|
|
this.logger.Print(entry + platform.LineSeparator())
|
|
}
|
|
}
|
|
|
|
func (this *FileLogWriter) Close() {
|
|
this.file.Close()
|
|
}
|
|
|
|
func NewFileLogWriter(path string) (*FileLogWriter, error) {
|
|
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
logger := &FileLogWriter{
|
|
queue: make(chan string, 16),
|
|
logger: log.New(file, "", log.Ldate|log.Ltime),
|
|
file: file,
|
|
}
|
|
go logger.run()
|
|
return logger, nil
|
|
}
|