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.
v2ray-core/common/log/log.go

67 lines
1.2 KiB

7 years ago
package log // import "v2ray.com/core/common/log"
import (
"sync"
"v2ray.com/core/common/serial"
)
7 years ago
// Message is the interface for all log messages.
type Message interface {
String() string
}
7 years ago
// Handler is the interface for log handler.
type Handler interface {
Handle(msg Message)
}
7 years ago
// GeneralMessage is a general log message that can contain all kind of content.
type GeneralMessage struct {
Severity Severity
Content interface{}
}
7 years ago
// String implements Message.
func (m *GeneralMessage) String() string {
7 years ago
return serial.Concat("[", m.Severity, "] ", m.Content)
}
7 years ago
// Record writes a message into log stream.
func Record(msg Message) {
logHandler.Handle(msg)
}
var (
logHandler syncHandler
)
7 years ago
// RegisterHandler register a new handler as current log handler. Previous registered handler will be discarded.
func RegisterHandler(handler Handler) {
if handler == nil {
panic("Log handler is nil")
}
logHandler.Set(handler)
}
type syncHandler struct {
sync.RWMutex
Handler
}
func (h *syncHandler) Handle(msg Message) {
h.RLock()
defer h.RUnlock()
if h.Handler != nil {
h.Handler.Handle(msg)
}
}
func (h *syncHandler) Set(handler Handler) {
h.Lock()
defer h.Unlock()
h.Handler = handler
}