X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/d008b46cbfc6348255e9b9a1fc009f0e4bd6aa2a..75b9579355e1adcdbb1255a7e8e4582e0a431f3d:/daemon.go diff --git a/daemon.go b/daemon.go index 3a950da..4c0f466 100644 --- a/daemon.go +++ b/daemon.go @@ -29,7 +29,8 @@ const ( // the clients on the Unix socket in the data directory until ctx ends. When // telegram.json exists, it also runs the Telegram bot. It also keeps a linked // WhatsApp device connected. It holds the lock of the token file, so it is the -// only process that refreshes the tokens and opens the WhatsApp session. +// only process that refreshes the tokens and opens the WhatsApp session. Its +// log lines go to out, through the loggers of daemonLogger. func daemon(ctx context.Context, out io.Writer) (err error) { dir, err := dataDir() if err != nil { @@ -64,7 +65,8 @@ func daemon(ctx context.Context, out io.Writer) (err error) { }() whatsappSetDeviceProps() - whatsapp, err := openWhatsApp(ctx, dir, time.Now, log.New(out, "whatsapp: ", log.LstdFlags), whatsappNewClient) + logger := daemonLogger(out, "onechat: ") + whatsapp, err := openWhatsApp(ctx, dir, time.Now, daemonLogger(out, "whatsapp: "), whatsappNewClient) if err != nil { return err } @@ -108,18 +110,17 @@ func daemon(ctx context.Context, out io.Writer) (err error) { return err } - _, err = fmt.Fprintf(out, "onechat: %s on %s, %s at %s effort, in %s\n", agent.config.Name, listener.Addr(), agent.config.Model, agent.config.Effort, agent.config.Workdir) - if err != nil { - return errors.Join(err, listener.Close()) + linked := "WhatsApp is not linked" + if whatsapp.isLinked() { + linked = "WhatsApp is linked" } + logger.Printf("%s is ready: socket %s, model %s at %s effort, commands run in %s, %s, active schedules: %d", agent.config.Name, listener.Addr(), agent.config.Model, agent.config.Effort, agent.config.Workdir, linked, schedules.Len()) + var telegram *TelegramBot if hasBot { - telegram = newTelegramBot(newTelegramAPI(telegramAPIURL, botSettings.Token), botSettings.Owner, filepath.Join(agent.dir, telegramOffsetFileName), log.New(out, "telegram: ", log.LstdFlags)) - _, err = fmt.Fprintln(out, "onechat: the Telegram bot waits for messages of its owner") - if err != nil { - return errors.Join(err, listener.Close()) - } + telegram = newTelegramBot(newTelegramAPI(telegramAPIURL, botSettings.Token), botSettings.Owner, filepath.Join(agent.dir, telegramOffsetFileName), daemonLogger(out, "telegram: ")) + logger.Print("the Telegram bot waits for messages of its owner") } return daemonServe(ctx, listener, telegram, whatsapp, schedules, func(notify func(Event)) (*TurnLoop, error) { @@ -420,3 +421,25 @@ func daemonRunRequest(l *TurnLoop, whatsapp *WhatsApp, request SocketRequest, an return fmt.Errorf("unknown request %q", request.Type) } } + +// daemonLogger returns the logger of the part of the daemon with prefix, such +// as "telegram: ", on out. The journal adds the time to each line and sets +// JOURNAL_STREAM for a service, so then the lines have no time stamp. Otherwise +// each line has the standard time stamp. +func daemonLogger(out io.Writer, prefix string) *log.Logger { + if os.Getenv("JOURNAL_STREAM") != "" { + return log.New(out, prefix, 0) + } + + return log.New(out, prefix, log.LstdFlags) +} + +// durationText returns d for a log line: rounded to the second, or to the +// millisecond when it is shorter than a second. +func durationText(d time.Duration) string { + if d < time.Second { + return d.Round(time.Millisecond).String() + } + + return d.Round(time.Second).String() +}