From ee0987169f3767db15fb19f929fcd99c4fc9c8b0 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 15 Aug 2016 21:54:47 -0700 Subject: [PATCH] pixiecore/cli: implement logging controls. The v1compat CLI is now complete, with support for --debug. The v2 CLI additionally supports optional timestamping, so that when you're using a modern init system that captures logs for you (e.g. systemd), you don't have stuttering timestamps. --- pixiecore/cli/apicmd.go | 19 +++++++++++++++++-- pixiecore/cli/bootcmd.go | 18 ++++++++++++++++-- pixiecore/cli/cli.go | 14 ++------------ pixiecore/cli/logging.go | 13 ++++++++----- pixiecore/cli/v1compat.go | 14 +++++++++----- 5 files changed, 52 insertions(+), 26 deletions(-) diff --git a/pixiecore/cli/apicmd.go b/pixiecore/cli/apicmd.go index fb1780f..27a876f 100644 --- a/pixiecore/cli/apicmd.go +++ b/pixiecore/cli/apicmd.go @@ -41,6 +41,15 @@ the Pixiecore boot API. The specification can be found at .`, if err != nil { fatalf("Error reading flag: %s", err) } + debug, err := cmd.Flags().GetBool("debug") + if err != nil { + fatalf("Error reading flag: %s", err) + } + timestamps, err := cmd.Flags().GetBool("log-timestamps") + if err != nil { + fatalf("Error reading flag: %s", err) + } + booter, err := pixiecore.APIBooter(server, timeout) if err != nil { fatalf("Failed to create API booter: %s", err) @@ -48,9 +57,15 @@ the Pixiecore boot API. The specification can be found at .`, s := &pixiecore.Server{ Booter: booter, Ipxe: Ipxe, - Log: logStdout, - Debug: debugLog, + Log: logWithStdFmt, } + if timestamps { + s.Log = logWithStdLog + } + if debug { + s.Debug = s.Log + } + fmt.Println(s.Serve()) }} diff --git a/pixiecore/cli/bootcmd.go b/pixiecore/cli/bootcmd.go index 0463045..ba0b802 100644 --- a/pixiecore/cli/bootcmd.go +++ b/pixiecore/cli/bootcmd.go @@ -38,6 +38,14 @@ var bootCmd = &cobra.Command{ if err != nil { fatalf("Error reading flag: %s", err) } + debug, err := cmd.Flags().GetBool("debug") + if err != nil { + fatalf("Error reading flag: %s", err) + } + timestamps, err := cmd.Flags().GetBool("log-timestamps") + if err != nil { + fatalf("Error reading flag: %s", err) + } spec := &pixiecore.Spec{ Kernel: pixiecore.ID(kernel), @@ -56,9 +64,15 @@ var bootCmd = &cobra.Command{ s := &pixiecore.Server{ Booter: booter, Ipxe: Ipxe, - Log: logStdout, - Debug: debugLog, + Log: logWithStdFmt, } + if timestamps { + s.Log = logWithStdLog + } + if debug { + s.Debug = s.Log + } + fmt.Println(s.Serve()) }, } diff --git a/pixiecore/cli/cli.go b/pixiecore/cli/cli.go index 6f8761c..64fc41d 100644 --- a/pixiecore/cli/cli.go +++ b/pixiecore/cli/cli.go @@ -84,23 +84,13 @@ func (ipxeFirmwareFlag) Type() string { return "filename" } -var cfgFile string - func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") + rootCmd.PersistentFlags().BoolP("debug", "d", false, "Log more things that aren't directly related to booting a recognized client") + rootCmd.PersistentFlags().BoolP("log-timestamps", "t", false, "Add a timestamp to each log line") } func initConfig() { - if cfgFile != "" { // enable ability to specify config file via flag - viper.SetConfigFile(cfgFile) - if err := viper.ReadInConfig(); err != nil { - fmt.Printf("Error reading configuration file %q: %s\n", viper.ConfigFileUsed(), err) - os.Exit(1) - } - fmt.Println("Using config file:", viper.ConfigFileUsed()) - } - viper.SetEnvPrefix("pixiecore") viper.AutomaticEnv() // read in environment variables that match } diff --git a/pixiecore/cli/logging.go b/pixiecore/cli/logging.go index 9700b69..20bc2b9 100644 --- a/pixiecore/cli/logging.go +++ b/pixiecore/cli/logging.go @@ -16,17 +16,20 @@ package cli import ( "fmt" + "log" "sync" ) var logSync sync.Mutex -func logStdout(subsys, msg string) { +func logWithStdLog(subsys, msg string) { + logSync.Lock() + defer logSync.Unlock() + log.Printf("[%s] %s", subsys, msg) +} + +func logWithStdFmt(subsys, msg string) { logSync.Lock() defer logSync.Unlock() fmt.Printf("[%s] %s\n", subsys, msg) } - -func debugLog(subsys, msg string) { - logStdout("DEBUG-"+subsys, msg) -} diff --git a/pixiecore/cli/v1compat.go b/pixiecore/cli/v1compat.go index 55b41e6..5dbfe92 100644 --- a/pixiecore/cli/v1compat.go +++ b/pixiecore/cli/v1compat.go @@ -42,7 +42,7 @@ func v1compatCLI() bool { initrdFile := fs.String("initrd", "", "Comma-separated list of initrds to pass to the kernel") kernelCmdline := fs.String("cmdline", "", "Additional arguments for the kernel commandline") - //debug := fs.Bool("debug", false, "Log more things that aren't directly related to booting a recognized client") + debug := fs.Bool("debug", false, "Log more things that aren't directly related to booting a recognized client") if err := fs.Parse(os.Args[1:]); err != nil { // This error path includes passing -h or --help. We want the @@ -81,14 +81,16 @@ func v1compatCLI() bool { s := &pixiecore.Server{ Booter: booter, Ipxe: Ipxe, - Log: logStdout, - Debug: debugLog, + Log: logWithStdLog, Address: *listenAddr, HTTPPort: *portHTTP, DHCPPort: *portDHCP, TFTPPort: *portTFTP, PXEPort: *portPXE, } + if *debug { + s.Debug = logWithStdLog + } fmt.Println(s.Serve()) case *kernelFile != "": @@ -117,14 +119,16 @@ func v1compatCLI() bool { s := &pixiecore.Server{ Booter: booter, Ipxe: Ipxe, - Log: logStdout, - Debug: debugLog, + Log: logWithStdLog, Address: *listenAddr, HTTPPort: *portHTTP, DHCPPort: *portDHCP, TFTPPort: *portTFTP, PXEPort: *portPXE, } + if *debug { + s.Debug = logWithStdLog + } fmt.Println(s.Serve()) default: