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.

40 lines
695 B

4 years ago
package log
import (
"fmt"
"time"
"github.com/xjasonlyu/tun2socks/v2/common/observable"
4 years ago
)
var (
_logCh = make(chan any)
_source = observable.NewObservable(_logCh)
4 years ago
)
type Event struct {
Level Level `json:"level"`
Message string `json:"msg"`
Time time.Time `json:"time"`
}
func newEvent(level Level, format string, args ...any) *Event {
4 years ago
event := &Event{
Level: level,
Time: time.Now(),
Message: fmt.Sprintf(format, args...),
}
_logCh <- event /* send all events to logCh */
4 years ago
return event
}
func Subscribe() observable.Subscription {
sub, _ := _source.Subscribe()
4 years ago
return sub
}
func UnSubscribe(sub observable.Subscription) {
_source.UnSubscribe(sub)
4 years ago
}