1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/log/logger.go

143 lines
2.6 KiB
Go
Raw Normal View History

2017-12-21 18:41:40 -05:00
package log
import (
"io"
"log"
"os"
"time"
"v2ray.com/core/common/platform"
"v2ray.com/core/common/signal"
)
2017-12-22 04:44:13 -05:00
// Writer is the interface for writing logs.
type Writer interface {
2017-12-21 18:51:25 -05:00
Write(string) error
io.Closer
2017-12-21 18:41:40 -05:00
}
2017-12-22 04:44:13 -05:00
// WriterCreator is a function to create LogWriters.
type WriterCreator func() Writer
2017-12-21 18:41:40 -05:00
type generalLogger struct {
2017-12-22 04:44:13 -05:00
creator WriterCreator
2017-12-21 18:41:40 -05:00
buffer chan Message
access *signal.Semaphore
2018-02-14 11:35:09 -05:00
done *signal.Done
2017-12-21 18:41:40 -05:00
}
// NewLogger returns a generic log handler that can handle all type of messages.
2017-12-22 04:44:13 -05:00
func NewLogger(logWriterCreator WriterCreator) Handler {
2017-12-21 18:41:40 -05:00
return &generalLogger{
creator: logWriterCreator,
buffer: make(chan Message, 16),
access: signal.NewSemaphore(1),
2018-02-14 11:35:09 -05:00
done: signal.NewDone(),
2017-12-21 18:41:40 -05:00
}
}
func (l *generalLogger) run() {
defer l.access.Signal()
dataWritten := false
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
logger := l.creator()
if logger == nil {
return
}
defer logger.Close()
for {
select {
2018-02-14 11:35:09 -05:00
case <-l.done.C():
return
2017-12-21 18:41:40 -05:00
case msg := <-l.buffer:
2017-12-21 18:51:25 -05:00
logger.Write(msg.String() + platform.LineSeparator())
2017-12-21 18:41:40 -05:00
dataWritten = true
case <-ticker.C:
if !dataWritten {
return
}
dataWritten = false
}
}
}
func (l *generalLogger) Handle(msg Message) {
select {
case l.buffer <- msg:
default:
}
select {
case <-l.access.Wait():
go l.run()
default:
}
}
2018-02-14 11:35:09 -05:00
func (l *generalLogger) Close() error {
return l.done.Close()
}
2017-12-21 18:41:40 -05:00
type consoleLogWriter struct {
logger *log.Logger
}
2017-12-21 18:51:25 -05:00
func (w *consoleLogWriter) Write(s string) error {
w.logger.Print(s)
return nil
2017-12-21 18:41:40 -05:00
}
func (w *consoleLogWriter) Close() error {
return nil
}
type fileLogWriter struct {
file *os.File
logger *log.Logger
}
2017-12-21 18:51:25 -05:00
func (w *fileLogWriter) Write(s string) error {
w.logger.Print(s)
return nil
2017-12-21 18:41:40 -05:00
}
func (w *fileLogWriter) Close() error {
return w.file.Close()
}
2017-12-22 04:41:10 -05:00
// CreateStdoutLogWriter returns a LogWriterCreator that creates LogWriter for stdout.
2017-12-22 04:44:13 -05:00
func CreateStdoutLogWriter() WriterCreator {
return func() Writer {
2017-12-21 18:41:40 -05:00
return &consoleLogWriter{
logger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
}
}
}
2017-12-22 04:41:10 -05:00
// CreateFileLogWriter returns a LogWriterCreator that creates LogWriter for the given file.
2017-12-22 04:44:13 -05:00
func CreateFileLogWriter(path string) (WriterCreator, error) {
2017-12-21 18:41:40 -05:00
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return nil, err
}
file.Close()
2017-12-22 04:44:13 -05:00
return func() Writer {
2017-12-21 18:41:40 -05:00
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return nil
}
return &fileLogWriter{
file: file,
logger: log.New(file, "", log.Ldate|log.Ltime),
}
}, nil
}
func init() {
RegisterHandler(NewLogger(CreateStdoutLogWriter()))
}