1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 09:17:55 -04:00
v2fly/common/log/internal/log_writer.go

95 lines
1.7 KiB
Go
Raw Normal View History

2016-05-12 02:24:41 -04:00
package internal
2015-12-05 15:10:14 -05:00
import (
"log"
"os"
2016-07-10 17:11:42 -04:00
"time"
2015-12-05 15:10:14 -05:00
"github.com/v2ray/v2ray-core/common/platform"
2016-07-10 17:11:42 -04:00
"github.com/v2ray/v2ray-core/common/signal"
2015-12-05 15:10:14 -05:00
)
2016-05-12 02:24:41 -04:00
type LogWriter interface {
2016-04-29 07:38:40 -04:00
Log(LogEntry)
2016-07-10 17:11:42 -04:00
Close()
2015-12-05 15:10:14 -05:00
}
2016-05-12 02:24:41 -04:00
type NoOpLogWriter struct {
2015-12-05 15:10:14 -05:00
}
2016-05-12 02:24:41 -04:00
func (this *NoOpLogWriter) Log(entry LogEntry) {
2016-04-29 17:40:28 -04:00
entry.Release()
2015-12-05 15:10:14 -05:00
}
2016-07-10 17:11:42 -04:00
func (this *NoOpLogWriter) Close() {
}
2016-05-12 02:24:41 -04:00
type StdOutLogWriter struct {
2015-12-05 15:10:14 -05:00
logger *log.Logger
2016-07-10 17:11:42 -04:00
cancel *signal.CancelSignal
2015-12-05 15:10:14 -05:00
}
2016-05-12 02:24:41 -04:00
func NewStdOutLogWriter() LogWriter {
return &StdOutLogWriter{
logger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
2016-07-10 17:11:42 -04:00
cancel: signal.NewCloseSignal(),
2015-12-05 15:10:14 -05:00
}
}
2016-05-12 02:24:41 -04:00
func (this *StdOutLogWriter) Log(log LogEntry) {
2015-12-05 15:10:14 -05:00
this.logger.Print(log.String() + platform.LineSeparator())
2016-04-29 07:38:40 -04:00
log.Release()
2015-12-05 15:10:14 -05:00
}
2016-07-10 17:11:42 -04:00
func (this *StdOutLogWriter) Close() {
time.Sleep(500 * time.Millisecond)
}
2016-05-12 02:24:41 -04:00
type FileLogWriter struct {
2016-05-12 20:18:07 -04:00
queue chan string
2015-12-05 15:10:14 -05:00
logger *log.Logger
file *os.File
2016-07-10 17:11:42 -04:00
cancel *signal.CancelSignal
2015-12-05 15:10:14 -05:00
}
2016-05-12 02:24:41 -04:00
func (this *FileLogWriter) Log(log LogEntry) {
2015-12-05 15:10:14 -05:00
select {
2016-05-12 20:18:07 -04:00
case this.queue <- log.String():
2015-12-05 15:10:14 -05:00
default:
// We don't expect this to happen, but don't want to block main thread as well.
}
2016-05-12 20:18:07 -04:00
log.Release()
2015-12-05 15:10:14 -05:00
}
2016-05-12 02:24:41 -04:00
func (this *FileLogWriter) run() {
2016-05-01 11:19:09 -04:00
for {
2016-05-01 11:19:41 -04:00
entry, open := <-this.queue
if !open {
break
}
2016-05-12 20:18:07 -04:00
this.logger.Print(entry + platform.LineSeparator())
2015-12-05 15:10:14 -05:00
}
2016-07-10 17:11:42 -04:00
this.cancel.Done()
2015-12-05 15:10:14 -05:00
}
2016-05-12 02:24:41 -04:00
func (this *FileLogWriter) Close() {
2016-07-10 17:11:42 -04:00
close(this.queue)
<-this.cancel.WaitForDone()
2015-12-05 15:10:14 -05:00
this.file.Close()
}
2016-05-12 02:24:41 -04:00
func NewFileLogWriter(path string) (*FileLogWriter, error) {
2015-12-05 15:10:14 -05:00
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return nil, err
}
2016-05-12 02:24:41 -04:00
logger := &FileLogWriter{
2016-05-12 20:18:07 -04:00
queue: make(chan string, 16),
2015-12-05 15:10:14 -05:00
logger: log.New(file, "", log.Ldate|log.Ltime),
file: file,
2016-07-10 17:11:42 -04:00
cancel: signal.NewCloseSignal(),
2015-12-05 15:10:14 -05:00
}
go logger.run()
return logger, nil
}