From 0c4b21353f6f97048e22227b1dc50a43f1b7fff8 Mon Sep 17 00:00:00 2001 From: beryll1um Date: Tue, 19 Apr 2022 07:47:49 +0200 Subject: [PATCH] Move "-logto" and "-loglevel" parameters to configuration file --- cmd/yggdrasil/main.go | 65 ++++++++++++++++++---------------------- src/config/config.go | 2 ++ src/defaults/defaults.go | 2 ++ 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 58b8230d..51a9fb9b 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -43,7 +43,7 @@ type node struct { admin *admin.AdminSocket } -func readConfig(log *log.Logger, useconf bool, useconffile string, normaliseconf bool) *config.NodeConfig { +func readConfig(useconf bool, useconffile string, normaliseconf bool) *config.NodeConfig { // Use a configuration file. If -useconf, the configuration will be read // from stdin. If -useconffile, the configuration will be read from the // filesystem. @@ -83,10 +83,10 @@ func readConfig(log *log.Logger, useconf bool, useconffile string, normaliseconf } // Check if we have old field names if _, ok := dat["TunnelRouting"]; ok { - log.Warnln("WARNING: Tunnel routing is no longer supported") + fmt.Println("WARNING: Tunnel routing is no longer supported") } if old, ok := dat["SigningPrivateKey"]; ok { - log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option has been renamed to \"PrivateKey\"") + fmt.Println("WARNING: The \"SigningPrivateKey\" configuration option has been renamed to \"PrivateKey\"") if _, ok := dat["PrivateKey"]; !ok { if privstr, err := hex.DecodeString(old.(string)); err == nil { priv := ed25519.PrivateKey(privstr) @@ -94,7 +94,7 @@ func readConfig(log *log.Logger, useconf bool, useconffile string, normaliseconf dat["PrivateKey"] = hex.EncodeToString(priv[:]) dat["PublicKey"] = hex.EncodeToString(pub[:]) } else { - log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option contains an invalid value and will be ignored") + fmt.Println("WARNING: The \"SigningPrivateKey\" configuration option contains an invalid value and will be ignored") } } } @@ -192,8 +192,6 @@ type yggArgs struct { getaddr bool getsnet bool useconffile string - logto string - loglevel string } func getArgs() yggArgs { @@ -204,10 +202,8 @@ func getArgs() yggArgs { confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON") autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)") ver := flag.Bool("version", false, "prints the version of this build") - logto := flag.String("logto", "stdout", "file path to log to, \"syslog\" or \"stdout\"") getaddr := flag.Bool("address", false, "returns the IPv6 address as derived from the supplied configuration") getsnet := flag.Bool("subnet", false, "returns the IPv6 subnet as derived from the supplied configuration") - loglevel := flag.String("loglevel", "info", "loglevel to enable") flag.Parse() return yggArgs{ genconf: *genconf, @@ -217,41 +213,14 @@ func getArgs() yggArgs { confjson: *confjson, autoconf: *autoconf, ver: *ver, - logto: *logto, getaddr: *getaddr, getsnet: *getsnet, - loglevel: *loglevel, } } // The main function is responsible for configuring and starting Yggdrasil. func run(args yggArgs, ctx context.Context, done chan struct{}) { defer close(done) - // Create a new logger that logs output to stdout. - var logger *log.Logger - switch args.logto { - case "stdout": - logger = log.New(os.Stdout, "", log.Flags()) - case "syslog": - if syslogger, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "DAEMON", version.BuildName()); err == nil { - logger = log.New(syslogger, "", log.Flags()) - } - default: - if logfd, err := os.OpenFile(args.logto, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil { - logger = log.New(logfd, "", log.Flags()) - } - } - if logger == nil { - logger = log.New(os.Stdout, "", log.Flags()) - logger.Warnln("Logging defaulting to stdout") - } - - if args.normaliseconf { - setLogLevel("error", logger) - } else { - setLogLevel(args.loglevel, logger) - } - var cfg *config.NodeConfig var err error switch { @@ -265,7 +234,7 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) { cfg = defaults.GenerateConfig() case args.useconffile != "" || args.useconf: // Read the configuration from either stdin or from the filesystem - cfg = readConfig(logger, args.useconf, args.useconffile, args.normaliseconf) + cfg = readConfig(args.useconf, args.useconffile, args.normaliseconf) // If the -normaliseconf option was specified then remarshal the above // configuration and print it back to stdout. This lets the user update // their configuration file with newly mapped names (like above) or to @@ -297,6 +266,30 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) { if cfg == nil { return } + // Create a new logger that logs output to stdout. + var logger *log.Logger + switch cfg.LogTo { + case "stdout": + logger = log.New(os.Stdout, "", log.Flags()) + case "syslog": + if syslogger, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "DAEMON", version.BuildName()); err == nil { + logger = log.New(syslogger, "", log.Flags()) + } + default: + if logfd, err := os.OpenFile(cfg.LogTo, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil { + logger = log.New(logfd, "", log.Flags()) + } + } + if logger == nil { + logger = log.New(os.Stdout, "", log.Flags()) + logger.Warnln("Logging defaulting to stdout") + } + + if args.normaliseconf { + setLogLevel("error", logger) + } else { + setLogLevel(cfg.LogLevel, logger) + } // Have we been asked for the node address yet? If so, print it and then stop. getNodeKey := func() ed25519.PublicKey { if pubkey, err := hex.DecodeString(cfg.PrivateKey); err == nil { diff --git a/src/config/config.go b/src/config/config.go index 041147b8..946a7211 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -27,6 +27,8 @@ import ( // supply one of these structs to the Yggdrasil core when starting a node. type NodeConfig struct { sync.RWMutex `json:"-"` + LogTo string `comment:"Your file path to log to, \"syslog\" or \"stdout\". It will override run argument provided configuration."` + LogLevel string `comment:"Your log level, \"error\", \"warn\", \"info\", \"debug\", \"trace\"."` Peers []string `comment:"List of connection strings for outbound peer connections in URI format,\ne.g. tls://a.b.c.d:e or socks://a.b.c.d:e/f.g.h.i:j. These connections\nwill obey the operating system routing table, therefore you should\nuse this section when you may connect via different interfaces."` InterfacePeers map[string][]string `comment:"List of connection strings for outbound peer connections in URI format,\narranged by source interface, e.g. { \"eth0\": [ tls://a.b.c.d:e ] }.\nNote that SOCKS peerings will NOT be affected by this option and should\ngo in the \"Peers\" section instead."` Listen []string `comment:"Listen addresses for incoming connections. You will need to add\nlisteners in order to accept incoming peerings from non-local nodes.\nMulticast peer discovery will work regardless of any listeners set\nhere. Each listener should be specified in URI format as above, e.g.\ntls://0.0.0.0:0 or tls://[::]:0 to listen on all interfaces."` diff --git a/src/defaults/defaults.go b/src/defaults/defaults.go index 7912fc76..4e1e5625 100644 --- a/src/defaults/defaults.go +++ b/src/defaults/defaults.go @@ -30,6 +30,8 @@ func GenerateConfig() *config.NodeConfig { // Create a node configuration and populate it. cfg := new(config.NodeConfig) cfg.NewKeys() + cfg.LogTo = "stdout" + cfg.LogLevel = "info" cfg.Listen = []string{} cfg.AdminListen = GetDefaults().DefaultAdminListen cfg.Peers = []string{}