fs/log: don't compile systemd log integration for non unix systems

This commit is contained in:
Benjamin Gustin 2020-12-29 00:07:12 +01:00 committed by GitHub
parent 5601652d65
commit 4d54454900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 26 deletions

View File

@ -3,7 +3,6 @@ package log
import (
"context"
"fmt"
"io"
"log"
"os"
@ -12,7 +11,6 @@ import (
"strings"
systemd "github.com/iguanesolutions/go-systemd/v5"
sysdjournald "github.com/iguanesolutions/go-systemd/v5/journald"
"github.com/rclone/rclone/fs"
"github.com/sirupsen/logrus"
)
@ -156,27 +154,3 @@ func InitLogging() {
func Redirected() bool {
return Opt.UseSyslog || Opt.File != ""
}
var logLevelToStringSystemd = []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,
}
// Starts systemd logging
func startSystemdLog() {
log.SetFlags(0)
fs.LogPrint = func(level fs.LogLevel, text string) {
var prefix string
if level < fs.LogLevel(len(logLevelToStringSystemd)) {
prefix = logLevelToStringSystemd[level]
}
text = fmt.Sprintf("%s%-6s: %s", prefix, level, text)
_ = log.Output(4, text)
}
}

16
fs/log/systemd.go Normal file
View File

@ -0,0 +1,16 @@
// Systemd interface for non-Unix variants only
// +build windows nacl plan9
package log
import (
"log"
"runtime"
)
// 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
}

50
fs/log/systemd_unix.go Normal file
View File

@ -0,0 +1,50 @@
// Systemd interface for Unix variants only
// +build !windows,!nacl,!plan9
package log
import (
"fmt"
"log"
"strings"
sysdjournald "github.com/iguanesolutions/go-systemd/v5/journald"
"github.com/rclone/rclone/fs"
)
// Enables systemd logs if configured or if auto detected
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)
fs.LogPrint = func(level fs.LogLevel, text string) {
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,
}
func systemdLogPrefix(l fs.LogLevel) string {
if l >= fs.LogLevel(len(logLevelToSystemdPrefix)) {
return ""
}
return logLevelToSystemdPrefix[l]
}