v2ray-core/app/log/internal/log_writer.go

91 lines
1.6 KiB
Go
Raw Normal View History

2016-05-12 06:24:41 +00:00
package internal
2015-12-05 20:10:14 +00:00
import (
2017-02-13 22:03:42 +00:00
"context"
2015-12-05 20:10:14 +00:00
"log"
"os"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/platform"
2015-12-05 20:10:14 +00:00
)
2016-05-12 06:24:41 +00:00
type LogWriter interface {
2016-04-29 11:38:40 +00:00
Log(LogEntry)
2016-07-10 21:11:42 +00:00
Close()
2015-12-05 20:10:14 +00:00
}
2016-05-12 06:24:41 +00:00
type NoOpLogWriter struct {
2015-12-05 20:10:14 +00:00
}
2017-01-04 11:52:24 +00:00
func (v *NoOpLogWriter) Log(entry LogEntry) {}
2015-12-05 20:10:14 +00:00
2016-11-27 20:39:09 +00:00
func (v *NoOpLogWriter) Close() {
2016-07-10 21:11:42 +00:00
}
2016-05-12 06:24:41 +00:00
type StdOutLogWriter struct {
2015-12-05 20:10:14 +00:00
logger *log.Logger
}
2016-05-12 06:24:41 +00:00
func NewStdOutLogWriter() LogWriter {
return &StdOutLogWriter{
logger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
2015-12-05 20:10:14 +00:00
}
}
2016-11-27 20:39:09 +00:00
func (v *StdOutLogWriter) Log(log LogEntry) {
v.logger.Print(log.String() + platform.LineSeparator())
2015-12-05 20:10:14 +00:00
}
2017-02-13 22:03:42 +00:00
func (v *StdOutLogWriter) Close() {}
2016-07-10 21:11:42 +00:00
2016-05-12 06:24:41 +00:00
type FileLogWriter struct {
2016-05-13 00:18:07 +00:00
queue chan string
2015-12-05 20:10:14 +00:00
logger *log.Logger
file *os.File
2017-02-13 22:03:42 +00:00
ctx context.Context
cancel context.CancelFunc
2015-12-05 20:10:14 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *FileLogWriter) Log(log LogEntry) {
2015-12-05 20:10:14 +00:00
select {
2017-02-13 22:03:42 +00:00
case <-v.ctx.Done():
return
2016-11-27 20:39:09 +00:00
case v.queue <- log.String():
2015-12-05 20:10:14 +00:00
default:
// We don't expect this to happen, but don't want to block main thread as well.
}
}
2017-02-13 22:03:42 +00:00
func (v *FileLogWriter) run(ctx context.Context) {
L:
2016-05-01 15:19:09 +00:00
for {
2017-02-13 22:03:42 +00:00
select {
case <-ctx.Done():
break L
case entry := <-v.queue:
v.logger.Print(entry + platform.LineSeparator())
2016-05-01 15:19:41 +00:00
}
2015-12-05 20:10:14 +00:00
}
2017-02-13 22:03:42 +00:00
v.file.Close()
2015-12-05 20:10:14 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *FileLogWriter) Close() {
2017-02-13 22:03:42 +00:00
v.cancel()
2015-12-05 20:10:14 +00:00
}
2016-05-12 06:24:41 +00:00
func NewFileLogWriter(path string) (*FileLogWriter, error) {
2015-12-05 20:10:14 +00:00
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return nil, err
}
2017-02-13 22:03:42 +00:00
ctx, cancel := context.WithCancel(context.Background())
2016-05-12 06:24:41 +00:00
logger := &FileLogWriter{
2016-05-13 00:18:07 +00:00
queue: make(chan string, 16),
2015-12-05 20:10:14 +00:00
logger: log.New(file, "", log.Ldate|log.Ltime),
file: file,
2017-02-13 22:03:42 +00:00
ctx: ctx,
cancel: cancel,
2015-12-05 20:10:14 +00:00
}
2017-02-13 22:03:42 +00:00
go logger.run(ctx)
2015-12-05 20:10:14 +00:00
return logger, nil
}