mirror of https://github.com/rclone/rclone.git
log: factor flags into logflags package - fixes #3792
This commit is contained in:
parent
11f501bd44
commit
ae340cf7d9
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/rclone/rclone/fs"
|
"github.com/rclone/rclone/fs"
|
||||||
"github.com/rclone/rclone/fs/config/configflags"
|
"github.com/rclone/rclone/fs/config/configflags"
|
||||||
"github.com/rclone/rclone/fs/filter/filterflags"
|
"github.com/rclone/rclone/fs/filter/filterflags"
|
||||||
|
"github.com/rclone/rclone/fs/log/logflags"
|
||||||
"github.com/rclone/rclone/fs/rc/rcflags"
|
"github.com/rclone/rclone/fs/rc/rcflags"
|
||||||
"github.com/rclone/rclone/lib/atexit"
|
"github.com/rclone/rclone/lib/atexit"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -169,6 +170,7 @@ func setupRootCommand(rootCmd *cobra.Command) {
|
||||||
configflags.AddFlags(pflag.CommandLine)
|
configflags.AddFlags(pflag.CommandLine)
|
||||||
filterflags.AddFlags(pflag.CommandLine)
|
filterflags.AddFlags(pflag.CommandLine)
|
||||||
rcflags.AddFlags(pflag.CommandLine)
|
rcflags.AddFlags(pflag.CommandLine)
|
||||||
|
logflags.AddFlags(pflag.CommandLine)
|
||||||
|
|
||||||
Root.Run = runRoot
|
Root.Run = runRoot
|
||||||
Root.Flags().BoolVarP(&version, "version", "V", false, "Print the version number")
|
Root.Flags().BoolVarP(&version, "version", "V", false, "Print the version number")
|
||||||
|
|
|
@ -10,16 +10,24 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rclone/rclone/fs"
|
"github.com/rclone/rclone/fs"
|
||||||
"github.com/rclone/rclone/fs/config/flags"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Flags
|
// Options contains options for the remote control server
|
||||||
var (
|
type Options struct {
|
||||||
logFile = flags.StringP("log-file", "", "", "Log everything to this file")
|
File string // Log everything to this file
|
||||||
logFormat = flags.StringP("log-format", "", "date,time", "Comma separated list of log format options")
|
Format string // Comma separated list of log format options
|
||||||
useSyslog = flags.BoolP("syslog", "", false, "Use Syslog for logging")
|
UseSyslog bool // Use Syslog for logging
|
||||||
syslogFacility = flags.StringP("syslog-facility", "", "DAEMON", "Facility for syslog, eg KERN,USER,...")
|
SyslogFacility string // Facility for syslog, eg KERN,USER,...
|
||||||
)
|
}
|
||||||
|
|
||||||
|
// DefaultOpt is the default values used for Opt
|
||||||
|
var DefaultOpt = Options{
|
||||||
|
Format: "date,time",
|
||||||
|
SyslogFacility: "DAEMON",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Opt is the options for the logger
|
||||||
|
var Opt = DefaultOpt
|
||||||
|
|
||||||
// fnName returns the name of the calling +2 function
|
// fnName returns the name of the calling +2 function
|
||||||
func fnName() string {
|
func fnName() string {
|
||||||
|
@ -79,7 +87,7 @@ func Stack(o interface{}, info string) {
|
||||||
|
|
||||||
// InitLogging start the logging as per the command line flags
|
// InitLogging start the logging as per the command line flags
|
||||||
func InitLogging() {
|
func InitLogging() {
|
||||||
flagsStr := "," + *logFormat + ","
|
flagsStr := "," + Opt.Format + ","
|
||||||
var flags int
|
var flags int
|
||||||
if strings.Contains(flagsStr, ",date,") {
|
if strings.Contains(flagsStr, ",date,") {
|
||||||
flags |= log.Ldate
|
flags |= log.Ldate
|
||||||
|
@ -102,8 +110,8 @@ func InitLogging() {
|
||||||
log.SetFlags(flags)
|
log.SetFlags(flags)
|
||||||
|
|
||||||
// Log file output
|
// Log file output
|
||||||
if *logFile != "" {
|
if Opt.File != "" {
|
||||||
f, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
|
f, err := os.OpenFile(Opt.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to open log file: %v", err)
|
log.Fatalf("Failed to open log file: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -116,8 +124,8 @@ func InitLogging() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Syslog output
|
// Syslog output
|
||||||
if *useSyslog {
|
if Opt.UseSyslog {
|
||||||
if *logFile != "" {
|
if Opt.File != "" {
|
||||||
log.Fatalf("Can't use --syslog and --log-file together")
|
log.Fatalf("Can't use --syslog and --log-file together")
|
||||||
}
|
}
|
||||||
startSysLog()
|
startSysLog()
|
||||||
|
@ -126,5 +134,5 @@ func InitLogging() {
|
||||||
|
|
||||||
// Redirected returns true if the log has been redirected from stdout
|
// Redirected returns true if the log has been redirected from stdout
|
||||||
func Redirected() bool {
|
func Redirected() bool {
|
||||||
return *useSyslog || *logFile != ""
|
return Opt.UseSyslog || Opt.File != ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
// Package logflags implements command line flags to set up the log
|
||||||
|
package logflags
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/rclone/rclone/fs/config/flags"
|
||||||
|
"github.com/rclone/rclone/fs/log"
|
||||||
|
"github.com/rclone/rclone/fs/rc"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddFlags adds the log flags to the flagSet
|
||||||
|
func AddFlags(flagSet *pflag.FlagSet) {
|
||||||
|
rc.AddOption("log", &log.Opt)
|
||||||
|
|
||||||
|
flags.StringVarP(flagSet, &log.Opt.File, "log-file", "", log.Opt.File, "Log everything to this file")
|
||||||
|
flags.StringVarP(flagSet, &log.Opt.Format, "log-format", "", log.Opt.Format, "Comma separated list of log format options")
|
||||||
|
flags.BoolVarP(flagSet, &log.Opt.UseSyslog, "syslog", "", log.Opt.UseSyslog, "Use Syslog for logging")
|
||||||
|
flags.StringVarP(flagSet, &log.Opt.SyslogFacility, "syslog-facility", "", log.Opt.SyslogFacility, "Facility for syslog, eg KERN,USER,...")
|
||||||
|
}
|
|
@ -32,9 +32,9 @@ var (
|
||||||
|
|
||||||
// Starts syslog
|
// Starts syslog
|
||||||
func startSysLog() bool {
|
func startSysLog() bool {
|
||||||
facility, ok := syslogFacilityMap[*syslogFacility]
|
facility, ok := syslogFacilityMap[Opt.SyslogFacility]
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Fatalf("Unknown syslog facility %q - man syslog for list", *syslogFacility)
|
log.Fatalf("Unknown syslog facility %q - man syslog for list", Opt.SyslogFacility)
|
||||||
}
|
}
|
||||||
Me := path.Base(os.Args[0])
|
Me := path.Base(os.Args[0])
|
||||||
w, err := syslog.New(syslog.LOG_NOTICE|facility, Me)
|
w, err := syslog.New(syslog.LOG_NOTICE|facility, Me)
|
||||||
|
|
Loading…
Reference in New Issue