Skip to content

Allow logging to be overridden #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions goagain.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,20 @@ var (

// The strategy to use; Single by default.
Strategy strategy = Single

Logger *log.Logger
)

func init() {
Logger = log.New(os.Stderr, "", log.LstdFlags)
}

func logln(v ...interface{}) {
if Logger != nil {
Logger.Println(v...)
}
}

// Re-exec this same image without dropping the net.Listener.
func Exec(l net.Listener) error {
var pid int
Expand All @@ -69,7 +81,7 @@ func Exec(l net.Listener) error {
); nil != err {
return err
}
log.Println("re-executing", argv0)
logln("re-executing", argv0)
return syscall.Exec(argv0, os.Args, os.Environ())
}

Expand Down Expand Up @@ -123,7 +135,7 @@ func ForkExec(l net.Listener) error {
if nil != err {
return err
}
log.Println("spawned child", p.Pid)
logln("spawned child", p.Pid)
if err = os.Setenv("GOAGAIN_PID", fmt.Sprint(p.Pid)); nil != err {
return err
}
Expand Down Expand Up @@ -159,7 +171,7 @@ func Kill() error {
if syscall.SIGQUIT == sig && Double == Strategy {
go syscall.Wait4(pid, nil, 0, nil)
}
log.Println("sending signal", sig, "to process", pid)
logln("sending signal", sig, "to process", pid)
return syscall.Kill(pid, sig)
}

Expand Down Expand Up @@ -205,14 +217,14 @@ func Wait(l net.Listener) (syscall.Signal, error) {
forked := false
for {
sig := <-ch
log.Println(sig.String())
logln(sig.String())
switch sig {

// SIGHUP should reload configuration.
case syscall.SIGHUP:
if nil != OnSIGHUP {
if err := OnSIGHUP(l); nil != err {
log.Println("OnSIGHUP:", err)
logln("OnSIGHUP:", err)
}
}

Expand All @@ -232,7 +244,7 @@ func Wait(l net.Listener) (syscall.Signal, error) {
case syscall.SIGUSR1:
if nil != OnSIGUSR1 {
if err := OnSIGUSR1(l); nil != err {
log.Println("OnSIGUSR1:", err)
logln("OnSIGUSR1:", err)
}
}

Expand Down