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.
This commit is contained in:
David Anderson 2016-08-15 21:54:47 -07:00
parent a943caa093
commit ee0987169f
5 changed files with 52 additions and 26 deletions

View File

@ -41,6 +41,15 @@ the Pixiecore boot API. The specification can be found at <TODO>.`,
if err != nil { if err != nil {
fatalf("Error reading flag: %s", err) 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) booter, err := pixiecore.APIBooter(server, timeout)
if err != nil { if err != nil {
fatalf("Failed to create API booter: %s", err) fatalf("Failed to create API booter: %s", err)
@ -48,9 +57,15 @@ the Pixiecore boot API. The specification can be found at <TODO>.`,
s := &pixiecore.Server{ s := &pixiecore.Server{
Booter: booter, Booter: booter,
Ipxe: Ipxe, Ipxe: Ipxe,
Log: logStdout, Log: logWithStdFmt,
Debug: debugLog,
} }
if timestamps {
s.Log = logWithStdLog
}
if debug {
s.Debug = s.Log
}
fmt.Println(s.Serve()) fmt.Println(s.Serve())
}} }}

View File

@ -38,6 +38,14 @@ var bootCmd = &cobra.Command{
if err != nil { if err != nil {
fatalf("Error reading flag: %s", err) 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{ spec := &pixiecore.Spec{
Kernel: pixiecore.ID(kernel), Kernel: pixiecore.ID(kernel),
@ -56,9 +64,15 @@ var bootCmd = &cobra.Command{
s := &pixiecore.Server{ s := &pixiecore.Server{
Booter: booter, Booter: booter,
Ipxe: Ipxe, Ipxe: Ipxe,
Log: logStdout, Log: logWithStdFmt,
Debug: debugLog,
} }
if timestamps {
s.Log = logWithStdLog
}
if debug {
s.Debug = s.Log
}
fmt.Println(s.Serve()) fmt.Println(s.Serve())
}, },
} }

View File

@ -84,23 +84,13 @@ func (ipxeFirmwareFlag) Type() string {
return "filename" return "filename"
} }
var cfgFile string
func init() { func init() {
cobra.OnInitialize(initConfig) 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() { 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.SetEnvPrefix("pixiecore")
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match
} }

View File

@ -16,17 +16,20 @@ package cli
import ( import (
"fmt" "fmt"
"log"
"sync" "sync"
) )
var logSync sync.Mutex 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() logSync.Lock()
defer logSync.Unlock() defer logSync.Unlock()
fmt.Printf("[%s] %s\n", subsys, msg) fmt.Printf("[%s] %s\n", subsys, msg)
} }
func debugLog(subsys, msg string) {
logStdout("DEBUG-"+subsys, msg)
}

View File

@ -42,7 +42,7 @@ func v1compatCLI() bool {
initrdFile := fs.String("initrd", "", "Comma-separated list of initrds to pass to the kernel") initrdFile := fs.String("initrd", "", "Comma-separated list of initrds to pass to the kernel")
kernelCmdline := fs.String("cmdline", "", "Additional arguments for the kernel commandline") 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 { if err := fs.Parse(os.Args[1:]); err != nil {
// This error path includes passing -h or --help. We want the // This error path includes passing -h or --help. We want the
@ -81,14 +81,16 @@ func v1compatCLI() bool {
s := &pixiecore.Server{ s := &pixiecore.Server{
Booter: booter, Booter: booter,
Ipxe: Ipxe, Ipxe: Ipxe,
Log: logStdout, Log: logWithStdLog,
Debug: debugLog,
Address: *listenAddr, Address: *listenAddr,
HTTPPort: *portHTTP, HTTPPort: *portHTTP,
DHCPPort: *portDHCP, DHCPPort: *portDHCP,
TFTPPort: *portTFTP, TFTPPort: *portTFTP,
PXEPort: *portPXE, PXEPort: *portPXE,
} }
if *debug {
s.Debug = logWithStdLog
}
fmt.Println(s.Serve()) fmt.Println(s.Serve())
case *kernelFile != "": case *kernelFile != "":
@ -117,14 +119,16 @@ func v1compatCLI() bool {
s := &pixiecore.Server{ s := &pixiecore.Server{
Booter: booter, Booter: booter,
Ipxe: Ipxe, Ipxe: Ipxe,
Log: logStdout, Log: logWithStdLog,
Debug: debugLog,
Address: *listenAddr, Address: *listenAddr,
HTTPPort: *portHTTP, HTTPPort: *portHTTP,
DHCPPort: *portDHCP, DHCPPort: *portDHCP,
TFTPPort: *portTFTP, TFTPPort: *portTFTP,
PXEPort: *portPXE, PXEPort: *portPXE,
} }
if *debug {
s.Debug = logWithStdLog
}
fmt.Println(s.Serve()) fmt.Println(s.Serve())
default: default: