aboutsummaryrefslogtreecommitdiff
path: root/xS/main.go
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2018-08-06 14:12:45 +0200
committerPřemysl Janouch <p@janouch.name>2018-08-06 19:49:06 +0200
commite2c8fb6e33f536384985197675c2ef569b9a6fe2 (patch)
treecc420b04c462e51218c314a8dafa913d9f7b7130 /xS/main.go
parent5c7ac9a92bd86f76b770d7d3fcc8265a346d00c0 (diff)
downloadxK-e2c8fb6e33f536384985197675c2ef569b9a6fe2.tar.gz
xK-e2c8fb6e33f536384985197675c2ef569b9a6fe2.tar.xz
xK-e2c8fb6e33f536384985197675c2ef569b9a6fe2.zip
hid: port logging facilities
Though the regular mode now has timestamps and a new mode for systemd has been added.
Diffstat (limited to 'xS/main.go')
-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)