From bb0113021a9f45bc8aed481058b3d85eb915bb4a Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Mon, 6 Aug 2018 14:12:45 +0200 Subject: hid: port logging facilities Though the regular mode now has timestamps and a new mode for systemd has been added. --- hid/main.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/hid/main.go b/hid/main.go index 826ea46..69df705 100644 --- a/hid/main.go +++ b/hid/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) -- cgit v1.2.3-70-g09d2