X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/4c199e7d93b6a4529ed5956807911b5f6c5489a7..d8f3785bb69288663f4cc46bf4d609c6770452d5:/daemon_log.go?ds=inline diff --git a/daemon_log.go b/daemon_log.go index 15426bc..771fef4 100644 --- a/daemon_log.go +++ b/daemon_log.go @@ -5,8 +5,10 @@ import ( "fmt" "io" "log/slog" + "os" "strings" "sync" + "time" ) // DaemonLogHandler writes short event lines with ordered attributes. Copies @@ -141,3 +143,25 @@ 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() +} + +// 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{}, + }) +}