about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2015-07-19T23·39+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2015-07-19T23·39+0200
commitdb55940d9e9fd502b17522d011dfd941e3f69c5d (patch)
tree3f00e36e8c2afe2ac797218d5ac7274f1dae142f
parentb3491c781cb4be55f981b7456fcdbe5c4f869f01 (diff)
Support systemd log severity prefixes
This is mostly useful for hydra-queue-runner.
-rw-r--r--src/libmain/shared.cc4
-rw-r--r--src/libutil/util.cc12
-rw-r--r--src/libutil/util.hh3
3 files changed, 18 insertions, 1 deletions
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc
index f300fbf1b463..575fa339a86c 100644
--- a/src/libmain/shared.cc
+++ b/src/libmain/shared.cc
@@ -90,6 +90,7 @@ static void setLogType(string lt)
     if (lt == "pretty") logType = ltPretty;
     else if (lt == "escapes") logType = ltEscapes;
     else if (lt == "flat") logType = ltFlat;
+    else if (lt == "systemd") logType = ltSystemd;
     else throw UsageError("unknown log type");
 }
 
@@ -116,6 +117,9 @@ void initNix()
 
     std::ios::sync_with_stdio(false);
 
+    if (getEnv("IN_SYSTEMD") == "1")
+        logType = ltSystemd;
+
     settings.processEnvironment();
     settings.loadConfFile();
 
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index f54fd5fb623f..7959b76f868d 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -477,12 +477,24 @@ void printMsg_(Verbosity level, const FormatOrString & fs)
 {
     checkInterrupt();
     if (level > verbosity) return;
+
     string prefix;
     if (logType == ltPretty)
         for (int i = 0; i < nestingLevel; i++)
             prefix += "|   ";
     else if (logType == ltEscapes && level != lvlInfo)
         prefix = "\033[" + escVerbosity(level) + "s";
+    else if (logType == ltSystemd) {
+        char c;
+        switch (level) {
+            case lvlError: c = '3'; break;
+            case lvlInfo: c = '5'; break;
+            case lvlTalkative: case lvlChatty: c = '6'; break;
+            default: c = '7';
+        }
+        prefix = string("<") + c + ">";
+    }
+
     string s = (format("%1%%2%\n") % prefix % fs.s).str();
     if (!isatty(STDERR_FILENO)) s = filterANSIEscapes(s);
     writeToStderr(s);
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 980cdf4cb774..b2fb59d6f2d7 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -122,7 +122,8 @@ T singleton(const A & a)
 typedef enum {
     ltPretty,   /* nice, nested output */
     ltEscapes,  /* nesting indicated using escape codes (for log2xml) */
-    ltFlat      /* no nesting */
+    ltFlat,     /* no nesting */
+    ltSystemd,  /* use systemd severity prefixes */
 } LogType;
 
 extern LogType logType;