Add per-network logger

This commit is contained in:
Simon Ser 2021-04-13 20:12:49 +02:00
parent be2825595d
commit fb4a01b023

16
user.go
View File

@ -109,6 +109,7 @@ func (ds deliveredStore) ForEachClient(f func(clientName string)) {
type network struct { type network struct {
Network Network
user *user user *user
logger Logger
stopped chan struct{} stopped chan struct{}
conn *upstreamConn conn *upstreamConn
@ -119,6 +120,8 @@ type network struct {
} }
func newNetwork(user *user, record *Network, channels []Channel) *network { func newNetwork(user *user, record *Network, channels []Channel) *network {
logger := &prefixLogger{user.logger, fmt.Sprintf("network %q: ", record.GetName())}
m := channelCasemapMap{newCasemapMap(0)} m := channelCasemapMap{newCasemapMap(0)}
for _, ch := range channels { for _, ch := range channels {
ch := ch ch := ch
@ -128,6 +131,7 @@ func newNetwork(user *user, record *Network, channels []Channel) *network {
return &network{ return &network{
Network: *record, Network: *record,
user: user, user: user,
logger: logger,
stopped: make(chan struct{}), stopped: make(chan struct{}),
channels: m, channels: m,
delivered: newDeliveredStore(), delivered: newDeliveredStore(),
@ -173,14 +177,14 @@ func (net *network) run() {
if dur := time.Now().Sub(lastTry); dur < retryConnectDelay { if dur := time.Now().Sub(lastTry); dur < retryConnectDelay {
delay := retryConnectDelay - dur delay := retryConnectDelay - dur
net.user.logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), net.Addr) net.logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), net.Addr)
time.Sleep(delay) time.Sleep(delay)
} }
lastTry = time.Now() lastTry = time.Now()
uc, err := connectToUpstream(net) uc, err := connectToUpstream(net)
if err != nil { if err != nil {
net.user.logger.Printf("failed to connect to upstream server %q: %v", net.Addr, err) net.logger.Printf("failed to connect to upstream server %q: %v", net.Addr, err)
net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to connect: %v", err)} net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to connect: %v", err)}
continue continue
} }
@ -233,7 +237,7 @@ func (net *network) detach(ch *Channel) {
return return
} }
net.user.logger.Printf("network %q: detaching channel %q", net.GetName(), ch.Name) net.logger.Printf("detaching channel %q", ch.Name)
ch.Detached = true ch.Detached = true
@ -241,7 +245,7 @@ func (net *network) detach(ch *Channel) {
nameCM := net.casemap(ch.Name) nameCM := net.casemap(ch.Name)
lastID, err := net.user.msgStore.LastMsgID(net, nameCM, time.Now()) lastID, err := net.user.msgStore.LastMsgID(net, nameCM, time.Now())
if err != nil { if err != nil {
net.user.logger.Printf("failed to get last message ID for channel %q: %v", ch.Name, err) net.logger.Printf("failed to get last message ID for channel %q: %v", ch.Name, err)
} }
ch.DetachedInternalMsgID = lastID ch.DetachedInternalMsgID = lastID
} }
@ -267,7 +271,7 @@ func (net *network) attach(ch *Channel) {
return return
} }
net.user.logger.Printf("network %q: attaching channel %q", net.GetName(), ch.Name) net.logger.Printf("attaching channel %q", ch.Name)
detachedMsgID := ch.DetachedInternalMsgID detachedMsgID := ch.DetachedInternalMsgID
ch.Detached = false ch.Detached = false
@ -347,7 +351,7 @@ func (net *network) storeClientDeliveryReceipts(clientName string) {
}) })
if err := net.user.srv.db.StoreClientDeliveryReceipts(net.ID, clientName, receipts); err != nil { if err := net.user.srv.db.StoreClientDeliveryReceipts(net.ID, clientName, receipts); err != nil {
net.user.logger.Printf("failed to store delivery receipts for user %q, client %q, network %q: %v", net.user.Username, clientName, net.GetName(), err) net.logger.Printf("failed to store delivery receipts for client %q: %v", clientName, err)
} }
} }