Add ability to set log file in command line options

This commit is contained in:
Gary Talent 2017-01-27 19:48:03 -06:00
parent 10a959e9f3
commit 42e65ebf52

View File

@ -10,6 +10,7 @@ package main
import ( import (
"flag" "flag"
"log" "log"
"os"
) )
const ( const (
@ -18,14 +19,16 @@ const (
) )
type cmdOptions struct { type cmdOptions struct {
config string config string
cmd string logFile string
cmd string
} }
func parseCmdOptions() cmdOptions { func parseCmdOptions() cmdOptions {
var o cmdOptions var o cmdOptions
flag.StringVar(&o.cmd, "cmd", CMD_SERVE, "Mode to run command in ("+CMD_SERVE+","+CMD_SPINDOWNALL+")") flag.StringVar(&o.cmd, "cmd", CMD_SERVE, "Mode to run command in ("+CMD_SERVE+","+CMD_SPINDOWNALL+")")
flag.StringVar(&o.config, "config", "dospin.json", "Path to the dospin config file") flag.StringVar(&o.config, "config", "dospin.json", "Path to the dospin config file")
flag.StringVar(&o.logFile, "logFile", "stdout", "Path to the dospin log file")
flag.Parse() flag.Parse()
return o return o
} }
@ -45,6 +48,15 @@ func spindownAll(opts cmdOptions) {
} }
func runServer(opts cmdOptions) { func runServer(opts cmdOptions) {
if opts.logFile != "stdout" {
logFile, err := os.OpenFile(opts.logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err == nil {
log.SetOutput(logFile)
} else {
log.Print("Could not open log file: ", err)
}
}
log.Println("Loading config:", opts.config) log.Println("Loading config:", opts.config)
settings, err := loadSettings(opts.config) settings, err := loadSettings(opts.config)
if err != nil { if err != nil {