aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xS/main.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/xS/main.go b/xS/main.go
index 826ea46..69df705 100644
--- a/xS/main.go
+++ b/xS/main.go
@@ -28,6 +28,7 @@ import (
"io"
"io/ioutil"
"log"
+ "log/syslog"
"net"
"os"
"os/signal"
@@ -48,6 +49,100 @@ const (
projectVersion = "0"
)
+// --- Logging -----------------------------------------------------------------
+
+type logPrio int
+
+const (
+ prioFatal logPrio = iota
+ prioError
+ prioWarning
+ prioStatus
+ prioDebug
+)
+
+func (lp logPrio) prefix() string {
+ switch lp {
+ case prioFatal:
+ return "fatal: "
+ case prioError:
+ return "error: "
+ case prioWarning:
+ return "warning: "
+ case prioStatus:
+ return ""
+ case prioDebug:
+ return "debug: "
+ default:
+ panic("unhandled log priority")
+ }
+}
+
+func (lp logPrio) syslogPrio() syslog.Priority {
+ switch lp {
+ case prioFatal:
+ return syslog.LOG_ERR
+ case prioError:
+ return syslog.LOG_ERR
+ case prioWarning:
+ return syslog.LOG_WARNING
+ case prioStatus:
+ return syslog.LOG_INFO
+ case prioDebug:
+ return syslog.LOG_DEBUG
+ default:
+ panic("unhandled log priority")
+ }
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+func logMessageStdio(prio logPrio, format string, args ...interface{}) {
+ // TODO: isatty-enabled colors based on prio.
+ os.Stderr.WriteString(time.Now().Format("2006-01-02 15:04:05 ") +
+ prio.prefix() + fmt.Sprintf(format, args...) + "\n")
+}
+
+func logMessageSystemd(prio logPrio, format string, args ...interface{}) {
+ if prio == prioFatal {
+ // There is no corresponding syslog severity.
+ format = "fatal: " + format
+ }
+ fmt.Fprintf(os.Stderr, "<%d>%s\n",
+ prio.syslogPrio(), fmt.Sprintf(format, args...))
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+var logMessage = logMessageStdio
+
+func printDebug(format string, args ...interface{}) {
+ if debugMode {
+ logMessage(prioDebug, format, args...)
+ }
+}
+
+func printStatus(format string, args ...interface{}) {
+ logMessage(prioStatus, format, args...)
+}
+func printWarning(format string, args ...interface{}) {
+ logMessage(prioWarning, format, args...)
+}
+func printError(format string, args ...interface{}) {
+ logMessage(prioError, format, args...)
+}
+
+// "fatal" is reserved for failures that would harm further operation.
+
+func printFatal(format string, args ...interface{}) {
+ logMessage(prioFatal, format, args...)
+}
+
+func exitFatal(format string, args ...interface{}) {
+ printFatal(format, args...)
+ os.Exit(1)
+}
+
// --- Utilities ---------------------------------------------------------------
// Split a string by a set of UTF-8 delimiters, optionally ignoring empty items.
@@ -3316,6 +3411,7 @@ func main() {
version := flag.Bool("version", false, "show version and exit")
writeDefaultCfg := flag.Bool("writedefaultcfg", false,
"write a default configuration file and exit")
+ systemd := flag.Bool("systemd", false, "log in systemd format")
flag.Parse()
@@ -3327,6 +3423,9 @@ func main() {
callSimpleConfigWriteDefault("", configTable)
return
}
+ if *systemd {
+ logMessage = logMessageSystemd
+ }
if flag.NArg() > 0 {
flag.Usage()
os.Exit(2)