1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-04 04:55:23 +00:00
v2fly/common/log/access.go

60 lines
1.4 KiB
Go
Raw Normal View History

2015-10-09 15:43:27 +00:00
package log
2016-01-18 11:31:27 +00:00
import (
2016-04-29 21:40:28 +00:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-01-18 11:31:27 +00:00
"github.com/v2ray/v2ray-core/common/serial"
)
2015-10-11 12:46:12 +00:00
// AccessStatus is the status of an access request from clients.
2015-10-09 15:43:27 +00:00
type AccessStatus string
const (
AccessAccepted = AccessStatus("accepted")
AccessRejected = AccessStatus("rejected")
)
2015-12-05 20:10:14 +00:00
var (
accessLoggerInstance logWriter = &noOpLogWriter{}
)
2015-10-09 15:43:27 +00:00
type accessLog struct {
2016-01-18 11:31:27 +00:00
From serial.String
To serial.String
2015-10-09 15:43:27 +00:00
Status AccessStatus
2016-01-18 11:31:27 +00:00
Reason serial.String
2015-10-09 15:43:27 +00:00
}
2016-04-29 11:38:40 +00:00
func (this *accessLog) Release() {
this.From = nil
this.To = nil
this.Reason = nil
}
2015-12-05 20:10:14 +00:00
func (this *accessLog) String() string {
2016-04-29 21:40:28 +00:00
b := alloc.NewSmallBuffer().Clear()
defer b.Release()
return b.AppendString(this.From.String()).AppendString(" ").AppendString(string(this.Status)).AppendString(" ").AppendString(this.To.String()).AppendString(" ").AppendString(this.Reason.String()).String()
2015-10-09 15:43:27 +00:00
}
2015-10-11 12:46:12 +00:00
// InitAccessLogger initializes the access logger to write into the give file.
2015-12-05 20:10:14 +00:00
func InitAccessLogger(file string) error {
logger, err := newFileLogWriter(file)
if err != nil {
2016-01-18 11:24:33 +00:00
Error("Failed to create access logger on file (", file, "): ", file, err)
2015-12-05 20:10:14 +00:00
return err
2015-10-09 15:43:27 +00:00
}
2015-12-05 20:10:14 +00:00
accessLoggerInstance = logger
return nil
2015-10-09 15:43:27 +00:00
}
2015-10-11 12:46:12 +00:00
// Access writes an access log.
2016-01-18 11:31:27 +00:00
func Access(from, to serial.String, status AccessStatus, reason serial.String) {
2015-12-05 20:10:14 +00:00
accessLoggerInstance.Log(&accessLog{
From: from,
To: to,
Status: status,
Reason: reason,
})
2015-10-09 15:43:27 +00:00
}