Introduce per-user logger

Adds the username to log lines.
This commit is contained in:
Simon Ser 2021-04-13 13:04:23 +02:00
parent 70e5ed05b6
commit adb4b300dc

28
user.go
View File

@ -173,14 +173,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.srv.Logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), net.Addr) net.user.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.srv.Logger.Printf("failed to connect to upstream server %q: %v", net.Addr, err) net.user.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 +233,7 @@ func (net *network) detach(ch *Channel) {
return return
} }
ch.Detached = true ch.Detached = true
net.user.srv.Logger.Printf("network %q: detaching channel %q", net.GetName(), ch.Name) net.user.logger.Printf("network %q: detaching channel %q", net.GetName(), ch.Name)
if net.conn != nil { if net.conn != nil {
uch := net.conn.channels.Value(ch.Name) uch := net.conn.channels.Value(ch.Name)
@ -256,7 +256,7 @@ func (net *network) attach(ch *Channel) {
return return
} }
ch.Detached = false ch.Detached = false
net.user.srv.Logger.Printf("network %q: attaching channel %q", net.GetName(), ch.Name) net.user.logger.Printf("network %q: attaching channel %q", net.GetName(), ch.Name)
var uch *upstreamChannel var uch *upstreamChannel
if net.conn != nil { if net.conn != nil {
@ -330,13 +330,14 @@ 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.srv.Logger.Printf("failed to store delivery receipts for user %q, client %q, network %q: %v", net.user.Username, clientName, net.GetName(), err) net.user.logger.Printf("failed to store delivery receipts for user %q, client %q, network %q: %v", net.user.Username, clientName, net.GetName(), err)
} }
} }
type user struct { type user struct {
User User
srv *Server srv *Server
logger Logger
events chan event events chan event
done chan struct{} done chan struct{}
@ -356,6 +357,8 @@ type pendingLIST struct {
} }
func newUser(srv *Server, record *User) *user { func newUser(srv *Server, record *User) *user {
logger := &prefixLogger{srv.Logger, fmt.Sprintf("user %q: ", record.Username)}
var msgStore messageStore var msgStore messageStore
if srv.LogPath != "" { if srv.LogPath != "" {
msgStore = newFSMessageStore(srv.LogPath, record.Username) msgStore = newFSMessageStore(srv.LogPath, record.Username)
@ -366,6 +369,7 @@ func newUser(srv *Server, record *User) *user {
return &user{ return &user{
User: *record, User: *record,
srv: srv, srv: srv,
logger: logger,
events: make(chan event, 64), events: make(chan event, 64),
done: make(chan struct{}), done: make(chan struct{}),
msgStore: msgStore, msgStore: msgStore,
@ -418,7 +422,7 @@ func (u *user) run() {
defer func() { defer func() {
if u.msgStore != nil { if u.msgStore != nil {
if err := u.msgStore.Close(); err != nil { if err := u.msgStore.Close(); err != nil {
u.srv.Logger.Printf("failed to close message store for user %q: %v", u.Username, err) u.logger.Printf("failed to close message store for user %q: %v", u.Username, err)
} }
} }
close(u.done) close(u.done)
@ -426,7 +430,7 @@ func (u *user) run() {
networks, err := u.srv.db.ListNetworks(u.ID) networks, err := u.srv.db.ListNetworks(u.ID)
if err != nil { if err != nil {
u.srv.Logger.Printf("failed to list networks for user %q: %v", u.Username, err) u.logger.Printf("failed to list networks for user %q: %v", u.Username, err)
return return
} }
@ -434,7 +438,7 @@ func (u *user) run() {
record := record record := record
channels, err := u.srv.db.ListChannels(record.ID) channels, err := u.srv.db.ListChannels(record.ID)
if err != nil { if err != nil {
u.srv.Logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err) u.logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
continue continue
} }
@ -444,7 +448,7 @@ func (u *user) run() {
if u.hasPersistentMsgStore() { if u.hasPersistentMsgStore() {
receipts, err := u.srv.db.ListDeliveryReceipts(record.ID) receipts, err := u.srv.db.ListDeliveryReceipts(record.ID)
if err != nil { if err != nil {
u.srv.Logger.Printf("failed to load delivery receipts for user %q, network %q: %v", u.Username, network.GetName(), err) u.logger.Printf("failed to load delivery receipts for user %q, network %q: %v", u.Username, network.GetName(), err)
return return
} }
@ -514,7 +518,7 @@ func (u *user) run() {
} }
uc.network.detach(c) uc.network.detach(c)
if err := uc.srv.db.StoreChannel(uc.network.ID, c); err != nil { if err := uc.srv.db.StoreChannel(uc.network.ID, c); err != nil {
u.srv.Logger.Printf("failed to store updated detached channel %q: %v", c.Name, err) u.logger.Printf("failed to store updated detached channel %q: %v", c.Name, err)
} }
case eventDownstreamConnected: case eventDownstreamConnected:
dc := e.dc dc := e.dc
@ -579,7 +583,7 @@ func (u *user) run() {
} }
return return
default: default:
u.srv.Logger.Printf("received unknown event type: %T", e) u.logger.Printf("received unknown event type: %T", e)
} }
} }
} }