"context"
"errors"
"fmt"
- "log"
+ "log/slog"
"net/url"
"os"
"path/filepath"
// the account, so a file that another user can read is an error. The clock
// gives the times of the status, logger gets the failures, and newClient makes
// the client of a device with its event handler.
-func openWhatsApp(ctx context.Context, dir string, clock func() time.Time, logger *log.Logger, newClient func(device *store.Device, handle func(event any) bool) WhatsAppClient) (*WhatsApp, error) {
+func openWhatsApp(ctx context.Context, dir string, clock func() time.Time, logger *slog.Logger, newClient func(device *store.Device, handle func(event any) bool) WhatsAppClient) (*WhatsApp, error) {
w := &WhatsApp{
dir: filepath.Join(dir, whatsappDirName),
clock: clock,
type WhatsApp struct {
dir string
clock func() time.Time
- logger *log.Logger
+ logger *slog.Logger
newClient func(device *store.Device, handle func(event any) bool) WhatsAppClient
noticeDelay time.Duration
requests chan WhatsAppRequest
w.mutex.Unlock()
if first {
- w.logger.Printf("first connect failed, retrying: %s", cause)
+ w.logger.Warn("first connect failed, retrying", "err", cause)
}
return true
return nil
}
- w.logger.Print("logged out by the phone, run /whatsapp pair PHONE")
+ w.logger.Error("logged out by the phone, run /whatsapp pair PHONE")
err := w.inbox.deliver(ctx, l, w.link(), true)
if err != nil {
err = w.removeSession()
if err != nil {
- w.logger.Printf("session delete failed: %v", err)
+ w.logger.Error("session delete failed", "err", err)
}
err = l.note("WhatsApp was unlinked from the phone.")
if err != nil {
- w.logger.Printf("chat note failed: %v", err)
+ w.logger.Error("chat note failed", "err", err)
}
w.broadcast("WhatsApp was unlinked from the phone. Run /whatsapp pair PHONE to link it again.")
return nil
}
- w.logger.Print("unlinked")
+ w.logger.Info("unlinked")
err = l.note("WhatsApp is unlinked.")
if err != nil {
- w.logger.Printf("chat note failed: %v", err)
+ w.logger.Error("chat note failed", "err", err)
}
if client == nil || logoutErr != nil {
linked = true
w.linkedBy(client)
- w.logger.Print("linked")
+ w.logger.Info("linked")
err = l.note("WhatsApp is linked.")
if err != nil {
- w.logger.Printf("chat note failed: %v", err)
+ w.logger.Error("chat note failed", "err", err)
}
answer("WhatsApp is linked to +" + phone + ".")
var notice, line string
+ level := slog.LevelInfo
+
+ var attrs []any
+
w.mutex.Lock()
switch event := event.(type) {
case w.disconnectedAt.IsZero():
line = "connected"
default:
- line = "connected again after " + durationText(w.clock().Sub(w.disconnectedAt))
+ line = "connected again"
+ attrs = append(attrs, "after", w.clock().Sub(w.disconnectedAt))
}
w.connected = true
case *events.Disconnected:
connected := w.connected
if w.startOutage() {
+ level = slog.LevelWarn
line = "disconnected, retrying"
if !connected {
line = "first connect failed, retrying"
w.connected = false
w.noticed = true
notice = "WhatsApp is disconnected and does not connect again by itself: " + event.PermanentDisconnectDescription() + ". Restart the daemon to try again."
- line = "disconnected for good, restart to retry: " + whatsappRedact(w.own, event.PermanentDisconnectDescription())
+ level = slog.LevelError
+ line = "disconnected for good, restart to retry"
+ attrs = append(attrs, "err", whatsappRedact(w.own, event.PermanentDisconnectDescription()))
}
}
w.mutex.Unlock()
if line != "" {
- w.logger.Print(line)
+ w.logger.Log(context.Background(), level, line, attrs...)
}
if notice != "" {