Commit 411dd7df authored by Matthew Holt's avatar Matthew Holt

New -log flag to direct log output to desired place

Log file can also be stdout or stderr. Log output is disabled by default now, which makes it more feasible to add more log statements to trace program flow in debugging situations.
parent 96f04cdc
...@@ -23,7 +23,6 @@ import ( ...@@ -23,7 +23,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net" "net"
"os" "os"
"path" "path"
...@@ -183,25 +182,25 @@ func startServers(groupings Group) error { ...@@ -183,25 +182,25 @@ func startServers(groupings Group) error {
for _, group := range groupings { for _, group := range groupings {
s, err := server.New(group.BindAddr.String(), group.Configs) s, err := server.New(group.BindAddr.String(), group.Configs)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
s.HTTP2 = HTTP2 // TODO: This setting is temporary s.HTTP2 = HTTP2 // TODO: This setting is temporary
var ln server.ListenerFile var ln server.ListenerFile
if IsRestart() { if IsRestart() {
// Look up this server's listener in the map of inherited file descriptors; // Look up this server's listener in the map of inherited file descriptors;
// if we don't have one, we must make a new one. // if we don't have one, we must make a new one (later).
if fdIndex, ok := loadedGob.ListenerFds[s.Addr]; ok { if fdIndex, ok := loadedGob.ListenerFds[s.Addr]; ok {
file := os.NewFile(fdIndex, "") file := os.NewFile(fdIndex, "")
fln, err := net.FileListener(file) fln, err := net.FileListener(file)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
ln, ok = fln.(server.ListenerFile) ln, ok = fln.(server.ListenerFile)
if !ok { if !ok {
log.Fatal("listener was not a ListenerFile") return errors.New("listener for " + s.Addr + " was not a ListenerFile")
} }
delete(loadedGob.ListenerFds, s.Addr) // mark it as used delete(loadedGob.ListenerFds, s.Addr) // mark it as used
......
...@@ -20,6 +20,7 @@ var ( ...@@ -20,6 +20,7 @@ var (
cpu string cpu string
version bool version bool
revoke string revoke string
logfile string
) )
const ( const (
...@@ -43,14 +44,31 @@ func init() { ...@@ -43,14 +44,31 @@ func init() {
flag.BoolVar(&letsencrypt.Agreed, "agree", false, "Agree to Let's Encrypt Subscriber Agreement") flag.BoolVar(&letsencrypt.Agreed, "agree", false, "Agree to Let's Encrypt Subscriber Agreement")
flag.StringVar(&letsencrypt.DefaultEmail, "email", "", "Default Let's Encrypt account email address") flag.StringVar(&letsencrypt.DefaultEmail, "email", "", "Default Let's Encrypt account email address")
flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate") flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate")
flag.StringVar(&logfile, "log", "", "Process log file")
} }
func main() { func main() {
flag.Parse() flag.Parse() // called here in main() to allow other packages to set flags in their inits
caddy.AppName = appName caddy.AppName = appName
caddy.AppVersion = appVersion caddy.AppVersion = appVersion
// set up process log before anything bad happens
switch logfile {
case "stdout":
log.SetOutput(os.Stdout)
case "stderr":
log.SetOutput(os.Stderr)
case "":
log.SetOutput(ioutil.Discard)
default:
file, err := os.Create(logfile)
if err != nil {
log.Fatalf("Error opening log file: %v", err)
}
log.SetOutput(file)
}
if version { if version {
fmt.Printf("%s %s\n", caddy.AppName, caddy.AppVersion) fmt.Printf("%s %s\n", caddy.AppName, caddy.AppVersion)
os.Exit(0) os.Exit(0)
...@@ -67,13 +85,13 @@ func main() { ...@@ -67,13 +85,13 @@ func main() {
// Set CPU cap // Set CPU cap
err := setCPU(cpu) err := setCPU(cpu)
if err != nil { if err != nil {
log.Fatal(err) mustLogFatal(err)
} }
// Get Caddyfile input // Get Caddyfile input
caddyfile, err := caddy.LoadCaddyfile(loadCaddyfile) caddyfile, err := caddy.LoadCaddyfile(loadCaddyfile)
if err != nil { if err != nil {
log.Fatal(err) mustLogFatal(err)
} }
// Start your engines // Start your engines
...@@ -82,7 +100,7 @@ func main() { ...@@ -82,7 +100,7 @@ func main() {
if caddy.IsRestart() { if caddy.IsRestart() {
log.Println("error starting servers:", err) log.Println("error starting servers:", err)
} else { } else {
log.Fatal(err) mustLogFatal(err)
} }
} }
...@@ -90,6 +108,14 @@ func main() { ...@@ -90,6 +108,14 @@ func main() {
caddy.Wait() caddy.Wait()
} }
// mustLogFatal just wraps log.Fatal() in a way that ensures the
// output is always printed to stderr so the user can see it,
// even if the process log was not enabled.
func mustLogFatal(args ...interface{}) {
log.SetOutput(os.Stderr)
log.Fatal(args...)
}
func loadCaddyfile() (caddy.Input, error) { func loadCaddyfile() (caddy.Input, error) {
// -conf flag // -conf flag
if conf != "" { if conf != "" {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment