about summary refs log tree commit diff
path: root/src/libutil/util.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2004-03-22T20·53+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2004-03-22T20·53+0000
commit777e13b94b2da466c16a5836b52413aa9d246cd5 (patch)
tree2d00fc590710fa8ecddaeda01f29db922266bb56 /src/libutil/util.cc
parent79bb0008ec9afa8d8cee9e6b807a579bcb1c92ab (diff)
* Nix now has three different formats for the log information it
  writes to stderr:
  
  - `pretty': the old nested style (default)
  - `escapes': uses escape codes to indicate nesting and message
    level; can be processed using `log2xml'
  - `flat': just plain text, no nesting

  These can be set using `--log-type TYPE' or the NIX_LOG_TYPE
  environment variable.  

Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r--src/libutil/util.cc30
1 files changed, 25 insertions, 5 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 3a6914cba149..768acf811c09 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -1,6 +1,7 @@
 #include <iostream>
 #include <cerrno>
 #include <cstdio>
+#include <sstream>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -223,6 +224,7 @@ void writeStringToFile(const Path & path, const string & s)
 }
 
 
+LogType logType = ltPretty;
 Verbosity verbosity = lvlError;
 
 static int nestingLevel = 0;
@@ -236,13 +238,28 @@ Nest::Nest()
 
 Nest::~Nest()
 {
-    if (nest) nestingLevel--;
+    if (nest) {
+        nestingLevel--;
+        if (logType == ltEscapes)
+            cerr << "\033[q";
+    }
+}
+
+
+static string escVerbosity(Verbosity level)
+{
+    int l = (int) level;
+    ostringstream st;
+    st << l;
+    return st.str();
 }
 
 
 void Nest::open(Verbosity level, const format & f)
 {
     if (level <= verbosity) {
+        if (logType == ltEscapes)
+            cerr << "\033[" << escVerbosity(level) << "p";
         printMsg_(level, f);
         nest = true;
         nestingLevel++;
@@ -254,10 +271,13 @@ void printMsg_(Verbosity level, const format & f)
 {
     checkInterrupt();
     if (level > verbosity) return;
-    string spaces;
-    for (int i = 0; i < nestingLevel; i++)
-        spaces += "|   ";
-    cerr << format("%1%%2%\n") % spaces % f.str();
+    string prefix;
+    if (logType == ltPretty)
+        for (int i = 0; i < nestingLevel; i++)
+            prefix += "|   ";
+    else if (logType == ltEscapes && level != lvlInfo)
+        prefix = "\033[" + escVerbosity(level) + "s";
+    cerr << format("%1%%2%\n") % prefix % f.str();
 }