2020-04-11 10:42:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"io"
|
2020-04-12 08:49:13 +00:00
|
|
|
"io/ioutil"
|
2020-04-11 10:42:34 +00:00
|
|
|
)
|
|
|
|
|
2020-04-22 18:42:54 +00:00
|
|
|
func setupLogging(loggingType int, systemLogPath, accessLogPath string) (*log.Logger, *log.Logger) {
|
|
|
|
/* Setup global logger */
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
log.SetFlags(0)
|
2020-04-11 10:42:34 +00:00
|
|
|
|
2020-04-22 18:42:54 +00:00
|
|
|
/* Calculate now, because, *shrug* */
|
|
|
|
useSame := (systemLogPath == accessLogPath)
|
2020-04-12 08:49:13 +00:00
|
|
|
|
|
|
|
/* Check requested logging type */
|
2020-04-22 18:42:54 +00:00
|
|
|
var systemLogger, accessLogger *log.Logger
|
|
|
|
switch loggingType {
|
2020-04-12 08:49:13 +00:00
|
|
|
case 0:
|
|
|
|
/* Default */
|
|
|
|
|
|
|
|
/* Setup system logger to output to file, or stderr if none supplied */
|
|
|
|
var systemWriter io.Writer
|
2020-04-22 18:42:54 +00:00
|
|
|
if systemLogPath != "" {
|
|
|
|
fd, err := os.OpenFile(systemLogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
2020-04-12 08:49:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create system logger: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
systemWriter = fd
|
|
|
|
} else {
|
|
|
|
systemWriter = os.Stderr
|
|
|
|
}
|
|
|
|
systemLogger = log.New(systemWriter, "", log.LstdFlags)
|
|
|
|
|
|
|
|
/* If both output to same, may as well use same logger for both */
|
|
|
|
if useSame {
|
|
|
|
accessLogger = systemLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup access logger to output to file, or stderr if none supplied */
|
|
|
|
var accessWriter io.Writer
|
2020-04-22 18:42:54 +00:00
|
|
|
if accessLogPath != "" {
|
|
|
|
fd, err := os.OpenFile(accessLogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
2020-04-12 08:49:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create access logger: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
accessWriter = fd
|
|
|
|
} else {
|
|
|
|
accessWriter = os.Stderr
|
|
|
|
}
|
|
|
|
accessLogger = log.New(accessWriter, "", log.LstdFlags)
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
/* Disable -- pipe logs to "discard". May as well use same for both */
|
|
|
|
systemLogger = log.New(ioutil.Discard, "", 0)
|
|
|
|
accessLogger = systemLogger
|
2020-04-11 10:42:34 +00:00
|
|
|
|
2020-04-12 08:49:13 +00:00
|
|
|
default:
|
2020-04-22 18:42:54 +00:00
|
|
|
log.Fatalf("Unrecognized logging type: %d\n", loggingType)
|
2020-04-11 10:42:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 18:42:54 +00:00
|
|
|
return systemLogger, accessLogger
|
2020-04-11 10:42:34 +00:00
|
|
|
}
|