1 // Frontend: Logging functions for programs using the safcm library
3 // Copyright (C) 2021 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
24 "ruderich.org/simon/safcm"
25 "ruderich.org/simon/safcm/rpc"
31 // Only one of Error, Log and ConnEvent is set in a single event
34 ConnEvent rpc.ConnEvent
36 Escaped bool // true if untrusted input is already escaped
44 func (l *Loop) Log(host Host, level safcm.LogLevel, escaped bool,
57 // LogEvent logs events using the log package. It can be used to implement
58 // Loop.LogEventFunc. It's used by cmd/safcm to log events.
59 func LogEvent(x Event, level safcm.LogLevel, isTTY bool,
62 // We have multiple event sources so this is somewhat ugly.
63 var prefix, data string
67 data = x.Error.Error()
69 // We logged an error, tell the caller
71 } else if x.Log.Level != 0 {
72 if level < x.Log.Level {
75 // LogError and LogDebug3 should not occur here
79 case safcm.LogVerbose:
86 prefix = fmt.Sprintf("[INVALID=%d]", x.Log.Level)
91 switch x.ConnEvent.Type {
92 case rpc.ConnEventStderr:
94 case rpc.ConnEventDebug:
96 case rpc.ConnEventUpload:
97 if level < safcm.LogInfo {
101 x.ConnEvent.Data = "remote helper upload in progress"
103 prefix = fmt.Sprintf("[INVALID=%d]", x.ConnEvent.Type)
106 data = x.ConnEvent.Data
109 host := x.Host.Name()
111 host = ColorString(isTTY, color, host)
113 // Make sure to escape control characters to prevent terminal
116 data = EscapeControlCharacters(isTTY, data)
118 log.Printf("%-9s [%s] %s", prefix, host, data)