2018-01-12 17:30:54 +01:00
|
|
|
// Package log provides logging for rclone
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2020-11-05 12:33:32 +01:00
|
|
|
"context"
|
2018-04-06 20:53:06 +02:00
|
|
|
"io"
|
2018-01-12 17:30:54 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2020-06-26 14:05:56 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-01-12 17:30:54 +01:00
|
|
|
)
|
|
|
|
|
2024-07-04 10:41:32 +02:00
|
|
|
// OptionsInfo descripts the Options in use
|
|
|
|
var OptionsInfo = fs.Options{{
|
|
|
|
Name: "log_file",
|
|
|
|
Default: "",
|
|
|
|
Help: "Log everything to this file",
|
|
|
|
Groups: "Logging",
|
|
|
|
}, {
|
|
|
|
Name: "log_format",
|
|
|
|
Default: "date,time",
|
|
|
|
Help: "Comma separated list of log format options",
|
|
|
|
Groups: "Logging",
|
|
|
|
}, {
|
|
|
|
Name: "syslog",
|
|
|
|
Default: false,
|
|
|
|
Help: "Use Syslog for logging",
|
|
|
|
Groups: "Logging",
|
|
|
|
}, {
|
|
|
|
Name: "syslog_facility",
|
|
|
|
Default: "DAEMON",
|
|
|
|
Help: "Facility for syslog, e.g. KERN,USER",
|
|
|
|
Groups: "Logging",
|
|
|
|
}, {
|
|
|
|
Name: "log_systemd",
|
|
|
|
Default: false,
|
|
|
|
Help: "Activate systemd integration for the logger",
|
|
|
|
Groups: "Logging",
|
|
|
|
}}
|
|
|
|
|
2020-12-17 12:55:27 +01:00
|
|
|
// Options contains options for controlling the logging
|
2019-12-10 14:54:00 +01:00
|
|
|
type Options struct {
|
2024-07-04 10:41:32 +02:00
|
|
|
File string `config:"log_file"` // Log everything to this file
|
|
|
|
Format string `config:"log_format"` // Comma separated list of log format options
|
|
|
|
UseSyslog bool `config:"syslog"` // Use Syslog for logging
|
|
|
|
SyslogFacility string `config:"syslog_facility"` // Facility for syslog, e.g. KERN,USER,...
|
|
|
|
LogSystemdSupport bool `config:"log_systemd"` // set if using systemd logging
|
2019-12-10 14:54:00 +01:00
|
|
|
}
|
|
|
|
|
2024-07-04 10:41:32 +02:00
|
|
|
func init() {
|
|
|
|
fs.RegisterGlobalOptions(fs.OptionsInfo{Name: "log", Opt: &Opt, Options: OptionsInfo})
|
2019-12-10 14:54:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Opt is the options for the logger
|
2024-07-04 10:41:32 +02:00
|
|
|
var Opt Options
|
2018-01-12 17:30:54 +01:00
|
|
|
|
|
|
|
// fnName returns the name of the calling +2 function
|
|
|
|
func fnName() string {
|
|
|
|
pc, _, _, ok := runtime.Caller(2)
|
|
|
|
name := "*Unknown*"
|
|
|
|
if ok {
|
|
|
|
name = runtime.FuncForPC(pc).Name()
|
|
|
|
dot := strings.LastIndex(name, ".")
|
|
|
|
if dot >= 0 {
|
|
|
|
name = name[dot+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trace debugs the entry and exit of the calling function
|
|
|
|
//
|
2023-11-29 10:25:30 +01:00
|
|
|
// It is designed to be used in a defer statement, so it returns a
|
2018-01-12 17:30:54 +01:00
|
|
|
// function that logs the exit parameters.
|
|
|
|
//
|
|
|
|
// Any pointers in the exit function will be dereferenced
|
|
|
|
func Trace(o interface{}, format string, a ...interface{}) func(string, ...interface{}) {
|
2020-11-05 12:33:32 +01:00
|
|
|
if fs.GetConfig(context.Background()).LogLevel < fs.LogLevelDebug {
|
2018-01-12 17:30:54 +01:00
|
|
|
return func(format string, a ...interface{}) {}
|
|
|
|
}
|
|
|
|
name := fnName()
|
|
|
|
fs.LogPrintf(fs.LogLevelDebug, o, name+": "+format, a...)
|
|
|
|
return func(format string, a ...interface{}) {
|
|
|
|
for i := range a {
|
|
|
|
// read the values of the pointed to items
|
|
|
|
typ := reflect.TypeOf(a[i])
|
|
|
|
if typ.Kind() == reflect.Ptr {
|
|
|
|
value := reflect.ValueOf(a[i])
|
|
|
|
if value.IsNil() {
|
|
|
|
a[i] = nil
|
|
|
|
} else {
|
|
|
|
pointedToValue := reflect.Indirect(value)
|
|
|
|
a[i] = pointedToValue.Interface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs.LogPrintf(fs.LogLevelDebug, o, ">"+name+": "+format, a...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 12:51:04 +02:00
|
|
|
// Stack logs a stack trace of callers with the o and info passed in
|
|
|
|
func Stack(o interface{}, info string) {
|
2020-11-05 12:33:32 +01:00
|
|
|
if fs.GetConfig(context.Background()).LogLevel < fs.LogLevelDebug {
|
2019-09-20 12:51:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
arr := [16 * 1024]byte{}
|
|
|
|
buf := arr[:]
|
|
|
|
n := runtime.Stack(buf, false)
|
|
|
|
buf = buf[:n]
|
|
|
|
fs.LogPrintf(fs.LogLevelDebug, o, "%s\nStack trace:\n%s", info, buf)
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// InitLogging start the logging as per the command line flags
|
|
|
|
func InitLogging() {
|
2019-12-10 14:54:00 +01:00
|
|
|
flagsStr := "," + Opt.Format + ","
|
2018-09-02 19:11:09 +02:00
|
|
|
var flags int
|
2020-12-17 12:55:27 +01:00
|
|
|
if strings.Contains(flagsStr, ",date,") {
|
|
|
|
flags |= log.Ldate
|
|
|
|
}
|
|
|
|
if strings.Contains(flagsStr, ",time,") {
|
|
|
|
flags |= log.Ltime
|
|
|
|
}
|
|
|
|
if strings.Contains(flagsStr, ",microseconds,") {
|
|
|
|
flags |= log.Lmicroseconds
|
|
|
|
}
|
|
|
|
if strings.Contains(flagsStr, ",UTC,") {
|
|
|
|
flags |= log.LUTC
|
2018-09-02 19:11:09 +02:00
|
|
|
}
|
|
|
|
if strings.Contains(flagsStr, ",longfile,") {
|
|
|
|
flags |= log.Llongfile
|
|
|
|
}
|
|
|
|
if strings.Contains(flagsStr, ",shortfile,") {
|
|
|
|
flags |= log.Lshortfile
|
|
|
|
}
|
|
|
|
log.SetFlags(flags)
|
|
|
|
|
2021-08-18 13:48:44 +02:00
|
|
|
fs.LogPrintPid = strings.Contains(flagsStr, ",pid,")
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// Log file output
|
2019-12-10 14:54:00 +01:00
|
|
|
if Opt.File != "" {
|
|
|
|
f, err := os.OpenFile(Opt.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
|
2018-01-12 17:30:54 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to open log file: %v", err)
|
|
|
|
}
|
2018-04-06 20:53:06 +02:00
|
|
|
_, err = f.Seek(0, io.SeekEnd)
|
2018-01-12 17:30:54 +01:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to seek log file to end: %v", err)
|
|
|
|
}
|
|
|
|
log.SetOutput(f)
|
2020-06-26 14:05:56 +02:00
|
|
|
logrus.SetOutput(f)
|
2018-01-12 17:30:54 +01:00
|
|
|
redirectStderr(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Syslog output
|
2019-12-10 14:54:00 +01:00
|
|
|
if Opt.UseSyslog {
|
|
|
|
if Opt.File != "" {
|
2018-01-12 17:30:54 +01:00
|
|
|
log.Fatalf("Can't use --syslog and --log-file together")
|
|
|
|
}
|
|
|
|
startSysLog()
|
|
|
|
}
|
2020-12-17 12:55:27 +01:00
|
|
|
|
|
|
|
// 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() {
|
2023-11-29 10:25:30 +01:00
|
|
|
if isJournalStream() {
|
2020-12-17 12:55:27 +01:00
|
|
|
Opt.LogSystemdSupport = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Systemd logging output
|
|
|
|
if Opt.LogSystemdSupport {
|
|
|
|
startSystemdLog()
|
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2018-06-30 17:05:31 +02:00
|
|
|
|
|
|
|
// Redirected returns true if the log has been redirected from stdout
|
|
|
|
func Redirected() bool {
|
2019-12-10 14:54:00 +01:00
|
|
|
return Opt.UseSyslog || Opt.File != ""
|
2018-06-30 17:05:31 +02:00
|
|
|
}
|