systemd: Fix detection and switch to the coreos package everywhere

rather than having 2 separate libraries

Signed-off-by: Anagh Kumar Baranwal <6824881+darthShadow@users.noreply.github.com>
This commit is contained in:
Anagh Kumar Baranwal
2023-12-02 14:17:15 +00:00
committed by Nick Craig-Wood
parent f0c774156e
commit 298c13e719
10 changed files with 72 additions and 55 deletions
+2 -3
View File
@@ -10,7 +10,6 @@ import (
"runtime"
"strings"
systemd "github.com/iguanesolutions/go-systemd/v5"
"github.com/rclone/rclone/fs"
"github.com/sirupsen/logrus"
)
@@ -49,7 +48,7 @@ func fnName() string {
// Trace debugs the entry and exit of the calling function
//
// It is designed to be used in a defer statement so it returns a
// It is designed to be used in a defer statement, so it returns a
// function that logs the exit parameters.
//
// Any pointers in the exit function will be dereferenced
@@ -141,7 +140,7 @@ func InitLogging() {
// Activate systemd logger support if systemd invocation ID is
// detected and output is going to stderr (not logging to a file or syslog)
if !Redirected() {
if _, usingSystemd := systemd.GetInvocationID(); usingSystemd {
if isJournalStream() {
Opt.LogSystemdSupport = true
}
}
+7 -3
View File
@@ -1,7 +1,7 @@
// Systemd interface for non-Unix variants only
//go:build windows || nacl || plan9
// +build windows nacl plan9
//go:build !unix
// +build !unix
package log
@@ -10,8 +10,12 @@ import (
"runtime"
)
// Enables systemd logs if configured or if auto detected
// Enables systemd logs if configured or if auto-detected
func startSystemdLog() bool {
log.Fatalf("--log-systemd not supported on %s platform", runtime.GOOS)
return false
}
func isJournalStream() bool {
return false
}
+25 -15
View File
@@ -1,20 +1,21 @@
// Systemd interface for Unix variants only
//go:build !windows && !nacl && !plan9
// +build !windows,!nacl,!plan9
//go:build unix
// +build unix
package log
import (
"fmt"
"log"
"strconv"
"strings"
sysdjournald "github.com/iguanesolutions/go-systemd/v5/journald"
"github.com/coreos/go-systemd/v22/journal"
"github.com/rclone/rclone/fs"
)
// Enables systemd logs if configured or if auto detected
// Enables systemd logs if configured or if auto-detected
func startSystemdLog() bool {
flagsStr := "," + Opt.Format + ","
var flags int
@@ -25,27 +26,36 @@ func startSystemdLog() bool {
flags |= log.Lshortfile
}
log.SetFlags(flags)
// TODO: Use the native journal.Print approach rather than a custom implementation
fs.LogPrint = func(level fs.LogLevel, text string) {
text = fmt.Sprintf("%s%-6s: %s", systemdLogPrefix(level), level, text)
text = fmt.Sprintf("<%s>%-6s: %s", systemdLogPrefix(level), level, text)
_ = log.Output(4, text)
}
return true
}
var logLevelToSystemdPrefix = []string{
fs.LogLevelEmergency: sysdjournald.EmergPrefix,
fs.LogLevelAlert: sysdjournald.AlertPrefix,
fs.LogLevelCritical: sysdjournald.CritPrefix,
fs.LogLevelError: sysdjournald.ErrPrefix,
fs.LogLevelWarning: sysdjournald.WarningPrefix,
fs.LogLevelNotice: sysdjournald.NoticePrefix,
fs.LogLevelInfo: sysdjournald.InfoPrefix,
fs.LogLevelDebug: sysdjournald.DebugPrefix,
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,
}
func systemdLogPrefix(l fs.LogLevel) string {
if l >= fs.LogLevel(len(logLevelToSystemdPrefix)) {
return ""
}
return logLevelToSystemdPrefix[l]
return strconv.Itoa(int(logLevelToSystemdPrefix[l]))
}
func isJournalStream() bool {
if usingJournald, _ := journal.StderrIsJournalStream(); usingJournald {
return true
}
return false
}