2017-12-21 23:41:40 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/platform"
|
2018-05-27 12:42:53 +00:00
|
|
|
"v2ray.com/core/common/signal/done"
|
|
|
|
"v2ray.com/core/common/signal/semaphore"
|
2017-12-21 23:41:40 +00:00
|
|
|
)
|
|
|
|
|
2017-12-22 09:44:13 +00:00
|
|
|
// Writer is the interface for writing logs.
|
|
|
|
type Writer interface {
|
2017-12-21 23:51:25 +00:00
|
|
|
Write(string) error
|
|
|
|
io.Closer
|
2017-12-21 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 09:44:13 +00:00
|
|
|
// WriterCreator is a function to create LogWriters.
|
|
|
|
type WriterCreator func() Writer
|
2017-12-21 23:41:40 +00:00
|
|
|
|
|
|
|
type generalLogger struct {
|
2017-12-22 09:44:13 +00:00
|
|
|
creator WriterCreator
|
2017-12-21 23:41:40 +00:00
|
|
|
buffer chan Message
|
2018-05-27 12:42:53 +00:00
|
|
|
access *semaphore.Instance
|
|
|
|
done *done.Instance
|
2017-12-21 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogger returns a generic log handler that can handle all type of messages.
|
2017-12-22 09:44:13 +00:00
|
|
|
func NewLogger(logWriterCreator WriterCreator) Handler {
|
2017-12-21 23:41:40 +00:00
|
|
|
return &generalLogger{
|
|
|
|
creator: logWriterCreator,
|
|
|
|
buffer: make(chan Message, 16),
|
2018-05-27 12:42:53 +00:00
|
|
|
access: semaphore.New(1),
|
|
|
|
done: done.New(),
|
2017-12-21 23:41:40 +00: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
|
|
|
|
}
|
2018-06-04 12:29:43 +00:00
|
|
|
defer logger.Close() // nolint: errcheck
|
2017-12-21 23:41:40 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2018-04-15 18:40:47 +00:00
|
|
|
case <-l.done.Wait():
|
2018-02-14 16:35:09 +00:00
|
|
|
return
|
2017-12-21 23:41:40 +00:00
|
|
|
case msg := <-l.buffer:
|
2018-06-04 12:29:43 +00:00
|
|
|
logger.Write(msg.String() + platform.LineSeparator()) // nolint: errcheck
|
2017-12-21 23:41:40 +00: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 16:35:09 +00:00
|
|
|
func (l *generalLogger) Close() error {
|
|
|
|
return l.done.Close()
|
|
|
|
}
|
|
|
|
|
2017-12-21 23:41:40 +00:00
|
|
|
type consoleLogWriter struct {
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
2017-12-21 23:51:25 +00:00
|
|
|
func (w *consoleLogWriter) Write(s string) error {
|
|
|
|
w.logger.Print(s)
|
|
|
|
return nil
|
2017-12-21 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *consoleLogWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileLogWriter struct {
|
|
|
|
file *os.File
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
2017-12-21 23:51:25 +00:00
|
|
|
func (w *fileLogWriter) Write(s string) error {
|
|
|
|
w.logger.Print(s)
|
|
|
|
return nil
|
2017-12-21 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fileLogWriter) Close() error {
|
|
|
|
return w.file.Close()
|
|
|
|
}
|
|
|
|
|
2017-12-22 09:41:10 +00:00
|
|
|
// CreateStdoutLogWriter returns a LogWriterCreator that creates LogWriter for stdout.
|
2017-12-22 09:44:13 +00:00
|
|
|
func CreateStdoutLogWriter() WriterCreator {
|
|
|
|
return func() Writer {
|
2017-12-21 23:41:40 +00:00
|
|
|
return &consoleLogWriter{
|
|
|
|
logger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 09:41:10 +00:00
|
|
|
// CreateFileLogWriter returns a LogWriterCreator that creates LogWriter for the given file.
|
2017-12-22 09:44:13 +00:00
|
|
|
func CreateFileLogWriter(path string) (WriterCreator, error) {
|
2017-12-21 23:41:40 +00: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 09:44:13 +00:00
|
|
|
return func() Writer {
|
2017-12-21 23:41:40 +00: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()))
|
|
|
|
}
|