]> git.giacomolli.uk - onechat/blobdiff - daemon_log.go
Show an input prompt while the agent is idle
[onechat] / daemon_log.go
index 15426bcafda9319f05a8284d732413df7fb59828..ef1360cc8ba88ac2eae59190e1b5db7b32fbc561 100644 (file)
@@ -5,10 +5,24 @@ import (
        "fmt"
        "io"
        "log/slog"
+       "os"
        "strings"
        "sync"
+       "time"
 )
 
+// daemonLogger returns a logger for one daemon part. JOURNAL_STREAM selects
+// syslog priorities without a time stamp. Other output carries the standard
+// time stamp and level.
+func daemonLogger(out io.Writer, prefix string) *slog.Logger {
+       return slog.New(&DaemonLogHandler{
+               out:     out,
+               prefix:  prefix,
+               journal: os.Getenv("JOURNAL_STREAM") != "",
+               mutex:   &sync.Mutex{},
+       })
+}
+
 // DaemonLogHandler writes short event lines with ordered attributes. Copies
 // share the writer lock. The err attribute follows all other attributes as a
 // cause. Journal lines carry a syslog priority instead of a time and level.
@@ -141,3 +155,13 @@ func daemonLogAttr(line *strings.Builder, group string, attr slog.Attr, cause *s
                fmt.Fprint(line, attr.Value.Any())
        }
 }
+
+// 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()
+}