log: factor flags into logflags package - fixes #3792

s3-about
Nick Craig-Wood 2019-12-10 13:54:00 +00:00
parent 11f501bd44
commit ae340cf7d9
4 changed files with 45 additions and 16 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configflags"
"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/lib/atexit"
"github.com/spf13/cobra"
@ -169,6 +170,7 @@ func setupRootCommand(rootCmd *cobra.Command) {
configflags.AddFlags(pflag.CommandLine)
filterflags.AddFlags(pflag.CommandLine)
rcflags.AddFlags(pflag.CommandLine)
logflags.AddFlags(pflag.CommandLine)
Root.Run = runRoot
Root.Flags().BoolVarP(&version, "version", "V", false, "Print the version number")

View File

@ -10,16 +10,24 @@ import (
"strings"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/flags"
)
// Flags
var (
logFile = flags.StringP("log-file", "", "", "Log everything to this file")
logFormat = flags.StringP("log-format", "", "date,time", "Comma separated list of log format options")
useSyslog = flags.BoolP("syslog", "", false, "Use Syslog for logging")
syslogFacility = flags.StringP("syslog-facility", "", "DAEMON", "Facility for syslog, eg KERN,USER,...")
)
// Options contains options for the remote control server
type Options struct {
File string // Log everything to this file
Format string // Comma separated list of log format options
UseSyslog bool // Use Syslog for logging
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
func fnName() string {
@ -79,7 +87,7 @@ func Stack(o interface{}, info string) {
// InitLogging start the logging as per the command line flags
func InitLogging() {
flagsStr := "," + *logFormat + ","
flagsStr := "," + Opt.Format + ","
var flags int
if strings.Contains(flagsStr, ",date,") {
flags |= log.Ldate
@ -102,8 +110,8 @@ func InitLogging() {
log.SetFlags(flags)
// Log file output
if *logFile != "" {
f, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if Opt.File != "" {
f, err := os.OpenFile(Opt.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
@ -116,8 +124,8 @@ func InitLogging() {
}
// Syslog output
if *useSyslog {
if *logFile != "" {
if Opt.UseSyslog {
if Opt.File != "" {
log.Fatalf("Can't use --syslog and --log-file together")
}
startSysLog()
@ -126,5 +134,5 @@ func InitLogging() {
// Redirected returns true if the log has been redirected from stdout
func Redirected() bool {
return *useSyslog || *logFile != ""
return Opt.UseSyslog || Opt.File != ""
}

View 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,...")
}

View File

@ -32,9 +32,9 @@ var (
// Starts syslog
func startSysLog() bool {
facility, ok := syslogFacilityMap[*syslogFacility]
facility, ok := syslogFacilityMap[Opt.SyslogFacility]
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])
w, err := syslog.New(syslog.LOG_NOTICE|facility, Me)