2017-02-10 21:28:06 +08:00
|
|
|
// Syslog interface for Unix variants only
|
|
|
|
|
|
|
|
// +build !windows,!nacl,!plan9
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
package log
|
2017-02-10 21:28:06 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"log/syslog"
|
|
|
|
"os"
|
|
|
|
"path"
|
2018-01-13 00:30:54 +08:00
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2017-02-10 21:28:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
syslogFacilityMap = map[string]syslog.Priority{
|
|
|
|
"KERN": syslog.LOG_KERN,
|
|
|
|
"USER": syslog.LOG_USER,
|
|
|
|
"MAIL": syslog.LOG_MAIL,
|
|
|
|
"DAEMON": syslog.LOG_DAEMON,
|
|
|
|
"AUTH": syslog.LOG_AUTH,
|
|
|
|
"SYSLOG": syslog.LOG_SYSLOG,
|
|
|
|
"LPR": syslog.LOG_LPR,
|
|
|
|
"NEWS": syslog.LOG_NEWS,
|
|
|
|
"UUCP": syslog.LOG_UUCP,
|
|
|
|
"CRON": syslog.LOG_CRON,
|
|
|
|
"AUTHPRIV": syslog.LOG_AUTHPRIV,
|
|
|
|
"FTP": syslog.LOG_FTP,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Starts syslog
|
|
|
|
func startSysLog() bool {
|
|
|
|
facility, ok := syslogFacilityMap[*syslogFacility]
|
|
|
|
if !ok {
|
|
|
|
log.Fatalf("Unknown syslog facility %q - man syslog for list", *syslogFacility)
|
|
|
|
}
|
|
|
|
Me := path.Base(os.Args[0])
|
|
|
|
w, err := syslog.New(syslog.LOG_NOTICE|facility, Me)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to start syslog: %v", err)
|
|
|
|
}
|
|
|
|
log.SetFlags(0)
|
|
|
|
log.SetOutput(w)
|
2018-01-13 00:30:54 +08:00
|
|
|
fs.LogPrint = func(level fs.LogLevel, text string) {
|
2017-02-10 21:28:06 +08:00
|
|
|
switch level {
|
2018-01-13 00:30:54 +08:00
|
|
|
case fs.LogLevelEmergency:
|
2017-02-10 21:28:06 +08:00
|
|
|
_ = w.Emerg(text)
|
2018-01-13 00:30:54 +08:00
|
|
|
case fs.LogLevelAlert:
|
2017-02-10 21:28:06 +08:00
|
|
|
_ = w.Alert(text)
|
2018-01-13 00:30:54 +08:00
|
|
|
case fs.LogLevelCritical:
|
2017-02-10 21:28:06 +08:00
|
|
|
_ = w.Crit(text)
|
2018-01-13 00:30:54 +08:00
|
|
|
case fs.LogLevelError:
|
2017-02-10 21:28:06 +08:00
|
|
|
_ = w.Err(text)
|
2018-01-13 00:30:54 +08:00
|
|
|
case fs.LogLevelWarning:
|
2017-02-10 21:28:06 +08:00
|
|
|
_ = w.Warning(text)
|
2018-01-13 00:30:54 +08:00
|
|
|
case fs.LogLevelNotice:
|
2017-02-10 21:28:06 +08:00
|
|
|
_ = w.Notice(text)
|
2018-01-13 00:30:54 +08:00
|
|
|
case fs.LogLevelInfo:
|
2017-02-10 21:28:06 +08:00
|
|
|
_ = w.Info(text)
|
2018-01-13 00:30:54 +08:00
|
|
|
case fs.LogLevelDebug:
|
2017-02-10 21:28:06 +08:00
|
|
|
_ = w.Debug(text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|