2015-09-11 11:27:36 -04:00
|
|
|
package log
|
|
|
|
|
2018-09-30 17:08:41 -04:00
|
|
|
//go:generate errorgen
|
2017-04-08 19:43:25 -04:00
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
import (
|
2017-02-01 15:35:40 -05:00
|
|
|
"context"
|
2017-12-19 15:28:12 -05:00
|
|
|
"sync"
|
2017-02-01 15:35:40 -05:00
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
2017-12-10 17:33:23 -05:00
|
|
|
"v2ray.com/core/common/log"
|
2015-09-11 11:27:36 -04:00
|
|
|
)
|
|
|
|
|
2018-04-03 05:11:54 -04:00
|
|
|
// Instance is a log.Handler that handles logs.
|
2017-12-19 15:28:12 -05:00
|
|
|
type Instance struct {
|
|
|
|
sync.RWMutex
|
|
|
|
config *Config
|
2017-12-21 18:41:40 -05:00
|
|
|
accessLogger log.Handler
|
|
|
|
errorLogger log.Handler
|
|
|
|
active bool
|
2017-12-19 15:28:12 -05:00
|
|
|
}
|
2015-10-13 12:27:29 -04:00
|
|
|
|
2017-12-19 18:09:52 -05:00
|
|
|
// New creates a new log.Instance based on the given config.
|
2017-12-19 15:28:12 -05:00
|
|
|
func New(ctx context.Context, config *Config) (*Instance, error) {
|
2017-12-21 18:41:40 -05:00
|
|
|
g := &Instance{
|
2017-12-19 15:28:12 -05:00
|
|
|
config: config,
|
2018-02-14 16:00:08 -05:00
|
|
|
active: false,
|
2017-12-21 18:41:40 -05:00
|
|
|
}
|
2018-02-14 16:00:08 -05:00
|
|
|
log.RegisterHandler(g)
|
2017-12-21 18:41:40 -05:00
|
|
|
|
|
|
|
return g, nil
|
2017-12-19 15:28:12 -05:00
|
|
|
}
|
2015-10-13 12:27:29 -04:00
|
|
|
|
2017-12-19 15:28:12 -05:00
|
|
|
func (g *Instance) initAccessLogger() error {
|
|
|
|
switch g.config.AccessLogType {
|
|
|
|
case LogType_File:
|
2017-12-21 18:41:40 -05:00
|
|
|
creator, err := log.CreateFileLogWriter(g.config.AccessLogPath)
|
2017-12-19 15:28:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-21 18:41:40 -05:00
|
|
|
g.accessLogger = log.NewLogger(creator)
|
2017-12-19 15:28:12 -05:00
|
|
|
case LogType_Console:
|
2017-12-21 18:41:40 -05:00
|
|
|
g.accessLogger = log.NewLogger(log.CreateStdoutLogWriter())
|
2017-12-19 15:28:12 -05:00
|
|
|
default:
|
2015-12-05 15:10:14 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-19 15:28:12 -05:00
|
|
|
func (g *Instance) initErrorLogger() error {
|
|
|
|
switch g.config.ErrorLogType {
|
|
|
|
case LogType_File:
|
2017-12-21 18:41:40 -05:00
|
|
|
creator, err := log.CreateFileLogWriter(g.config.ErrorLogPath)
|
2017-12-19 15:28:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-21 18:41:40 -05:00
|
|
|
g.errorLogger = log.NewLogger(creator)
|
2017-12-19 15:28:12 -05:00
|
|
|
case LogType_Console:
|
2017-12-21 18:41:40 -05:00
|
|
|
g.errorLogger = log.NewLogger(log.CreateStdoutLogWriter())
|
2017-04-06 09:13:09 -04:00
|
|
|
default:
|
|
|
|
}
|
2017-12-19 15:28:12 -05:00
|
|
|
return nil
|
2017-04-06 09:13:09 -04:00
|
|
|
}
|
|
|
|
|
2018-02-14 16:00:08 -05:00
|
|
|
// Type implements common.HasType.
|
2018-02-14 11:35:09 -05:00
|
|
|
func (*Instance) Type() interface{} {
|
|
|
|
return (*Instance)(nil)
|
|
|
|
}
|
|
|
|
|
2018-02-14 16:00:08 -05:00
|
|
|
func (g *Instance) startInternal() error {
|
2017-12-21 18:41:40 -05:00
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
2018-02-14 11:35:09 -05:00
|
|
|
|
|
|
|
if g.active {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-21 18:41:40 -05:00
|
|
|
g.active = true
|
2017-02-01 15:35:40 -05:00
|
|
|
|
2018-02-14 11:35:09 -05:00
|
|
|
if err := g.initAccessLogger(); err != nil {
|
|
|
|
return newError("failed to initialize access logger").Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
if err := g.initErrorLogger(); err != nil {
|
|
|
|
return newError("failed to initialize error logger").Base(err).AtWarning()
|
|
|
|
}
|
2018-02-14 16:00:08 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-03 05:11:54 -04:00
|
|
|
// Start implements common.Runnable.Start().
|
2018-02-14 16:00:08 -05:00
|
|
|
func (g *Instance) Start() error {
|
|
|
|
if err := g.startInternal(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newError("Logger started").AtDebug().WriteToLog()
|
2017-12-21 18:41:40 -05:00
|
|
|
|
2018-02-14 11:35:09 -05:00
|
|
|
return nil
|
2017-12-21 18:41:40 -05:00
|
|
|
}
|
|
|
|
|
2017-12-19 18:09:52 -05:00
|
|
|
// Handle implements log.Handler.
|
2017-12-19 15:28:12 -05:00
|
|
|
func (g *Instance) Handle(msg log.Message) {
|
2018-02-14 11:35:09 -05:00
|
|
|
g.RLock()
|
|
|
|
defer g.RUnlock()
|
|
|
|
|
|
|
|
if !g.active {
|
2017-12-21 18:41:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-19 15:28:12 -05:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *log.AccessMessage:
|
|
|
|
if g.accessLogger != nil {
|
2017-12-21 18:41:40 -05:00
|
|
|
g.accessLogger.Handle(msg)
|
2017-02-01 15:35:40 -05:00
|
|
|
}
|
2017-12-19 15:28:12 -05:00
|
|
|
case *log.GeneralMessage:
|
2017-12-21 18:41:40 -05:00
|
|
|
if g.errorLogger != nil && msg.Severity <= g.config.ErrorLogLevel {
|
|
|
|
g.errorLogger.Handle(msg)
|
2017-02-01 15:35:40 -05:00
|
|
|
}
|
2017-12-19 15:28:12 -05:00
|
|
|
default:
|
|
|
|
// Swallow
|
2017-02-01 15:35:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 05:11:54 -04:00
|
|
|
// Close implements common.Closable.Close().
|
2018-02-08 09:39:46 -05:00
|
|
|
func (g *Instance) Close() error {
|
2018-02-14 11:35:09 -05:00
|
|
|
newError("Logger closing").AtDebug().WriteToLog()
|
|
|
|
|
2017-12-19 15:28:12 -05:00
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
|
2018-02-14 11:35:09 -05:00
|
|
|
if !g.active {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-21 18:42:27 -05:00
|
|
|
g.active = false
|
2018-02-08 09:39:46 -05:00
|
|
|
|
2018-05-25 18:47:51 -04:00
|
|
|
common.Close(g.accessLogger) // nolint: errcheck
|
2018-02-14 16:00:08 -05:00
|
|
|
g.accessLogger = nil
|
|
|
|
|
2018-05-25 18:47:51 -04:00
|
|
|
common.Close(g.errorLogger) // nolint: errcheck
|
2018-02-14 16:00:08 -05:00
|
|
|
g.errorLogger = nil
|
2018-02-14 11:35:09 -05:00
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
return nil
|
2016-07-10 17:11:42 -04:00
|
|
|
}
|
2017-02-01 15:35:40 -05:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*Config))
|
|
|
|
}))
|
|
|
|
}
|