2020-12-29 00:07:12 +01:00
|
|
|
// Systemd interface for Unix variants only
|
|
|
|
|
2023-11-29 10:25:30 +01:00
|
|
|
//go:build unix
|
|
|
|
// +build unix
|
2020-12-29 00:07:12 +01:00
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-11-29 10:25:30 +01:00
|
|
|
"strconv"
|
2020-12-29 00:07:12 +01:00
|
|
|
"strings"
|
|
|
|
|
2023-11-29 10:25:30 +01:00
|
|
|
"github.com/coreos/go-systemd/v22/journal"
|
2020-12-29 00:07:12 +01:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
)
|
|
|
|
|
2023-11-29 10:25:30 +01:00
|
|
|
// Enables systemd logs if configured or if auto-detected
|
2020-12-29 00:07:12 +01:00
|
|
|
func startSystemdLog() bool {
|
|
|
|
flagsStr := "," + Opt.Format + ","
|
|
|
|
var flags int
|
|
|
|
if strings.Contains(flagsStr, ",longfile,") {
|
|
|
|
flags |= log.Llongfile
|
|
|
|
}
|
|
|
|
if strings.Contains(flagsStr, ",shortfile,") {
|
|
|
|
flags |= log.Lshortfile
|
|
|
|
}
|
|
|
|
log.SetFlags(flags)
|
2023-11-29 10:25:30 +01:00
|
|
|
// TODO: Use the native journal.Print approach rather than a custom implementation
|
2020-12-29 00:07:12 +01:00
|
|
|
fs.LogPrint = func(level fs.LogLevel, text string) {
|
2023-11-29 10:25:30 +01:00
|
|
|
text = fmt.Sprintf("<%s>%-6s: %s", systemdLogPrefix(level), level, text)
|
2020-12-29 00:07:12 +01:00
|
|
|
_ = log.Output(4, text)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-11-29 10:25:30 +01:00
|
|
|
var logLevelToSystemdPrefix = []journal.Priority{
|
|
|
|
fs.LogLevelEmergency: journal.PriEmerg,
|
|
|
|
fs.LogLevelAlert: journal.PriAlert,
|
|
|
|
fs.LogLevelCritical: journal.PriCrit,
|
|
|
|
fs.LogLevelError: journal.PriErr,
|
|
|
|
fs.LogLevelWarning: journal.PriWarning,
|
|
|
|
fs.LogLevelNotice: journal.PriNotice,
|
|
|
|
fs.LogLevelInfo: journal.PriInfo,
|
|
|
|
fs.LogLevelDebug: journal.PriDebug,
|
2020-12-29 00:07:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func systemdLogPrefix(l fs.LogLevel) string {
|
|
|
|
if l >= fs.LogLevel(len(logLevelToSystemdPrefix)) {
|
|
|
|
return ""
|
|
|
|
}
|
2023-11-29 10:25:30 +01:00
|
|
|
return strconv.Itoa(int(logLevelToSystemdPrefix[l]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func isJournalStream() bool {
|
|
|
|
if usingJournald, _ := journal.StderrIsJournalStream(); usingJournald {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-12-29 00:07:12 +01:00
|
|
|
}
|