1 // Frontend: Logging functions for programs using the safcm library
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024 Simon Ruderich
12 "ruderich.org/simon/safcm"
13 "ruderich.org/simon/safcm/rpc"
19 // Only one of Error, Log and ConnEvent is set in a single event
22 ConnEvent rpc.ConnEvent
24 Escaped bool // true if untrusted input is already escaped
32 func (l *Loop) Log(host Host, level safcm.LogLevel, escaped bool,
45 // LogEvent logs events using the log package. It can be used to implement
46 // Loop.LogEventFunc. It's used by cmd/safcm to log events.
47 func LogEvent(x Event, level safcm.LogLevel, isTTY bool,
50 // We have multiple event sources so this is somewhat ugly.
51 var prefix, data string
55 data = x.Error.Error()
57 // We logged an error, tell the caller
59 } else if x.Log.Level != 0 {
60 if level < x.Log.Level {
63 // LogError and LogDebug3 should not occur here
67 case safcm.LogVerbose:
74 prefix = fmt.Sprintf("[INVALID=%d]", x.Log.Level)
79 switch x.ConnEvent.Type {
80 case rpc.ConnEventStderr:
82 case rpc.ConnEventDebug:
84 case rpc.ConnEventUpload:
85 if level < safcm.LogInfo {
89 x.ConnEvent.Data = "remote helper upload in progress"
91 prefix = fmt.Sprintf("[INVALID=%d]", x.ConnEvent.Type)
94 data = x.ConnEvent.Data
99 host = ColorString(isTTY, color, host)
101 // Make sure to escape control characters to prevent terminal
104 data = EscapeControlCharacters(isTTY, data)
106 log.Printf("%-9s [%s] %s", prefix, host, data)