mirror of
https://github.com/danderson/netboot.git
synced 2025-10-16 10:01:20 +02:00
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:
parent
a943caa093
commit
ee0987169f
@ -41,6 +41,15 @@ the Pixiecore boot API. The specification can be found at <TODO>.`,
|
||||
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 <TODO>.`,
|
||||
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())
|
||||
}}
|
||||
|
||||
|
@ -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())
|
||||
},
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user