mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
Make easier for daemonization
- Create pidfile if requested - Create logfile and close std* streams if requested
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -24,5 +24,5 @@ _testmain.go
|
||||
# vim swp files
|
||||
*.swp
|
||||
|
||||
# revel tmp
|
||||
app/tmp
|
||||
# our binary
|
||||
/inbucket
|
||||
|
||||
41
inbucket.go
41
inbucket.go
@@ -11,11 +11,14 @@ import (
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"github.com/jhillyerd/inbucket/smtpd"
|
||||
"github.com/jhillyerd/inbucket/web"
|
||||
golog "log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var help = flag.Bool("help", false, "Displays this help")
|
||||
var pidfile = flag.String("pidfile", "none", "Write our PID into the specified file")
|
||||
var logfile = flag.String("logfile", "stderr", "Write out log into the specified file")
|
||||
|
||||
var startTime = time.Now()
|
||||
|
||||
@@ -37,10 +40,46 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Configure logging
|
||||
// Configure logging, close std* fds
|
||||
if *logfile != "stderr" {
|
||||
// stderr is the go logging default
|
||||
if *logfile == "stdout" {
|
||||
// set to stdout
|
||||
golog.SetOutput(os.Stdout)
|
||||
} else {
|
||||
// use specificed log file
|
||||
logf, err := os.OpenFile(*logfile, os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to create %v: %v\n", *logfile, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer logf.Close()
|
||||
golog.SetOutput(logf)
|
||||
|
||||
// close std* streams
|
||||
os.Stdout.Close()
|
||||
os.Stderr.Close()
|
||||
os.Stdin.Close()
|
||||
os.Stdout = logf
|
||||
os.Stderr = logf
|
||||
}
|
||||
}
|
||||
|
||||
level, _ := config.Config.String("logging", "level")
|
||||
log.SetLogLevel(level)
|
||||
|
||||
// Write pidfile if requested
|
||||
// TODO: Probably supposed to remove pidfile during shutdown
|
||||
if *pidfile != "none" {
|
||||
pidf, err := os.Create(*pidfile)
|
||||
if err != nil {
|
||||
log.Error("Failed to create %v: %v", *pidfile, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer pidf.Close()
|
||||
fmt.Fprintf(pidf, "%v\n", os.Getpid())
|
||||
}
|
||||
|
||||
// Startup SMTP server
|
||||
server := smtpd.New()
|
||||
go server.Start()
|
||||
|
||||
Reference in New Issue
Block a user