mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
759 B
50 lines
759 B
package log
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"unsafe"
|
|
|
|
"v2ray.com/core/common/serial"
|
|
)
|
|
|
|
type Message interface {
|
|
String() string
|
|
}
|
|
|
|
type Handler interface {
|
|
Handle(msg Message)
|
|
}
|
|
|
|
type noOpHandler byte
|
|
|
|
func (noOpHandler) Handle(msg Message) {}
|
|
|
|
type GeneralMessage struct {
|
|
Severity Severity
|
|
Content interface{}
|
|
}
|
|
|
|
func (m *GeneralMessage) String() string {
|
|
return serial.Concat("[", m.Severity, "]: ", m.Content)
|
|
}
|
|
|
|
func Record(msg Message) {
|
|
h := (*Handler)(atomic.LoadPointer(&logHandler))
|
|
(*h).Handle(msg)
|
|
}
|
|
|
|
var (
|
|
logHandler unsafe.Pointer
|
|
)
|
|
|
|
func RegisterHandler(handler Handler) {
|
|
if handler == nil {
|
|
panic("Log handler is nil")
|
|
}
|
|
atomic.StorePointer(&logHandler, unsafe.Pointer(&handler))
|
|
}
|
|
|
|
func init() {
|
|
RegisterHandler(noOpHandler(0))
|
|
}
|